- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Spring-IOC容器-Bean管理-基于XML方式超详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
1.Spring是轻量级的开源的JavaEE框架 。
2.Spring可以解决企业应用开发的复杂性 。
3.Spring有两个核心部分:IOC和AOP 。
1.IOC:控制反转,把创建对象过程交给Spring进行管理 。
2.AOP:面向切面,不修改源代码进行功能增强 。
4.Spring的特点 。
1.方便解耦,简化开发 。
2.AOP编程支持 。
3.方便程序测试 。
4.方便和其他框架进行整合 。
5.方便进行事务操作 。
6.降低API开发难度 。
1.什么是IOC?
1.控制反转,把对象创建和对象之间的调用过程,交给 Spring 进行管理使用 。
2. IOC 目的:为了耦合度降低 。
2.IOC底层原理 。
1.xml解析,工厂模式,反射 。
3.图解IOC底层原理 。
4.IOC(BeanFactory接口) 。
1.IOC 思想基于 IOC 容器完成,IOC 容器底层就是对象工厂 。
2.Spring 提供 IOC 容器实现两种方式:(两个接口) 。
①BeanFactory:IOC 容器基本实现,是 Spring 内部的使用接口,不提供开发人员进行使用加载配置文件时候不会创建对象,在获取对象(使用)才去创建对象 。
②ApplicationContext:BeanFactory接口的子接口,提供更多更强大的功能,提供给开发人员使用(加载配置文件时候就会把在配置文件对象进行创建)推荐使用! 。
5.IOC操作Bean管理 。
1.什么是Bean管理?
Bean管理指的是两个操作:
1.Spring创建对象 。
2.Spring注入属性 。
2.Bean管理操作的两种方式 。
1.基于xml配置文件方式实现 。
2.基于注解方式实现 。
接下来的部分详细讲解IOC操作Bean管理基于XML方式的详解 。
1.set方式注入 。
①创建类,定义属性和对应的 set 方法 。
1
2
3
4
5
6
7
8
9
10
11
12
|
public
class
Book {
private
String bName;
private
String bAuthor;
//创建属性
public
void
setbName(String bName) {
this
.bName = bName;
}
//创建属性对应的 set 方法
public
void
setbAuthor(String bAuthor) {
this
.bAuthor = bAuthor;
}
}
|
②在 spring 配置文件配置对象创建,配置属性注入 。
。
1
2
3
4
5
6
7
8
9
|
<!--使用set方法注入属性-->
<bean id=
"book"
class
=
"com.atguigu.spring5.testDemo.Book"
>
<!--使用property完成属性注入
name:类里面属性名称
value:向属性注入的值
-->
<property name=
"bAuthor"
value=
"发如雪"
></property>
<property name=
"bName"
value=
"周杰伦"
></property>
</bean>
|
使用有参数构造进行注入 。
①创建类,定义属性,创建属性对应有参数构造方法 。
1
2
3
4
5
6
7
8
|
public
class
Order {
private
String oName;
private
String address;
public
Order(String oName, String address) {
this
.oName = oName;
this
.address = address;
}
}
|
②在 spring 配置文件中进行配置 。
1
2
3
4
5
|
<!--有参数构造注入属性-->
<
bean
id
=
"order"
class
=
"com.atguigu.spring5.testDemo.Order"
>
<
constructor-arg
name
=
"address"
value
=
"东风破"
/>
<
constructor-arg
name
=
"oName"
value
=
"周杰伦"
/>
</
bean
>
|
注入空值和特殊符号 。
。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<
bean
id
=
"book"
class
=
"com.atguigu.spring5.Book"
>
<!--(1)null值-->
<
property
name
=
"address"
>
<
null
/>
<!--属性里边添加一个null标签-->
</
property
>
<!--(2)特殊符号赋值-->
<!--属性值包含特殊符号
a 把<>进行转义 < >
b 把带特殊符号内容写到CDATA
-->
<
property
name
=
"address"
>
<
value
>
<![CDATA[<<南京>>]]>
</
value
>
</
property
>
</
bean
>
|
注入属性-外部bean 。
①创建两个类service和dao类 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public
class
UserService {
//service类
//创建UserDao类型属性,生成set方法
private
UserDao userDao;
public
void
setUserDao(UserDao userDao) {
this
.userDao = userDao;
}
public
void
add() {
System.out.println(
"service add..............."
);
userDao.update();
//调用dao方法
}
}
public
class
UserDaoImpl
implements
UserDao {
//dao类
@Override
public
void
update() {
System.out.println(
"dao update..........."
);
}
}
|
②在spring配置文件中进行配置 。
。
1
2
3
4
5
6
7
8
9
|
<!--
1
service和dao对象创建-->
<bean id=
"userService"
class
=
"com.atguigu.spring5.service.UserService"
>
<!--注入userDao对象
name属性:类里面属性名称
ref属性:创建userDao对象bean标签id值
-->
<property name=
"userDao"
ref=
"userDaoImpl"
></property>
</bean>
<bean id=
"userDaoImpl"
class
=
"com.atguigu.spring5.dao.UserDaoImpl"
></bean>
|
注入属性-内部 bean 。
①在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
//部门类
public
class
Dept {
private
String dname;
public
void
setDname(String dname) {
this
.dname = dname;
}
}
//员工类
public
class
Emp {
private
String ename;
private
String gender;
//员工属于某一个部门,使用对象形式表示
private
Dept dept;
public
void
setDept(Dept dept) {
this
.dept = dept;
}
public
void
setEname(String ename) {
this
.ename = ename;
}
public
void
setGender(String gender) {
this
.gender = gender;
}
}
|
②在spring配置文件中配置 。
。
1
2
3
4
5
6
7
8
9
10
11
12
|
<!--内部bean-->
<bean id=
"emp"
class
=
"com.atguigu.spring5.bean.Emp"
>
<!--设置两个普通属性-->
<property name=
"ename"
value=
"周杰伦"
></property>
<property name=
"gender"
value=
"男"
></property>
<!--设置对象类型属性-->
<property name=
"dept"
>
<bean id=
"dept"
class
=
"com.atguigu.spring5.bean.Dept"
><!--内部bean赋值-->
<property name=
"dname"
value=
"宣传部门"
></property>
</bean>
</property>
</bean>
|
注入属性-级联赋值 。
1
2
3
4
5
6
7
8
9
10
11
|
<!--方式一:级联赋值-->
<bean id=
"emp"
class
=
"com.atguigu.spring5.bean.Emp"
>
<!--设置两个普通属性-->
<property name=
"ename"
value=
"Andy"
></property>
<property name=
"gender"
value=
"女"
></property>
<!--级联赋值-->
<property name=
"dept"
ref=
"dept"
></property>
</bean>
<bean id=
"dept"
class
=
"com.atguigu.spring5.bean.Dept"
>
<property name=
"dname"
value=
"公关部门"
></property>
</bean>
|
1
2
3
4
|
//方式二:生成dept的get方法(get方法必须有!!)
public
Dept getDept() {
return
dept;
}
|
1
2
3
4
5
6
7
8
9
10
11
|
<!--级联赋值-->
<bean id=
"emp"
class
=
"com.atguigu.spring5.bean.Emp"
>
<!--设置两个普通属性-->
<property name=
"ename"
value=
"jams"
></property>
<property name=
"gender"
value=
"男"
></property>
<!--级联赋值-->
<property name=
"dept"
ref=
"dept"
></property>
<property name=
"dept.dname"
value=
"技术部门"
></property>
</bean>
<bean id=
"dept"
class
=
"com.atguigu.spring5.bean.Dept"
>
</bean>
|
1.xml注入数组类型属性 。
2.xml注入List集合类型属性 。
3.xml注入Map集合类型属性 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
//(1)创建类,定义数组、list、map、set 类型属性,生成对应 set 方法
public
class
Stu {
//1 数组类型属性
private
String[] courses;
//2 list集合类型属性
private
List<String> list;
//3 map集合类型属性
private
Map<String,String> maps;
//4 set集合类型属性
private
Set<String> sets;
public
void
setSets(Set<String> sets) {
this
.sets = sets;
}
public
void
setCourses(String[] courses) {
this
.courses = courses;
}
public
void
setList(List<String> list) {
this
.list = list;
}
public
void
setMaps(Map<String, String> maps) {
this
.maps = maps;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<!--(
2
)在 spring 配置文件进行配置-->
<bean id=
"stu"
class
=
"com.atguigu.spring5.collectiontype.Stu"
>
<!--数组类型属性注入-->
<property name=
"courses"
>
<array>
<value>java课程</value>
<value>数据库课程</value>
</array>
</property>
<!--list类型属性注入-->
<property name=
"list"
>
<list>
<value>张三</value>
<value>小三</value>
</list>
</property>
<!--map类型属性注入-->
<property name=
"maps"
>
<map>
<entry key=
"JAVA"
value=
"java"
></entry>
<entry key=
"PHP"
value=
"php"
></entry>
</map>
</property>
<!--set类型属性注入-->
<property name=
"sets"
>
<set>
<value>MySQL</value>
<value>Redis</value>
</set>
</property>
</bean>
|
在集合里面设置对象类型值 。
1
2
3
4
5
|
//学生所学多门课程
private
List<Course> courseList;
//创建集合
public
void
setCourseList(List<Course> courseList) {
this
.courseList = courseList;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!--创建多个course对象-->
<bean id=
"course1"
class
=
"com.atguigu.spring5.collectiontype.Course"
>
<property name=
"cname"
value=
"Spring5框架"
></property>
</bean>
<bean id=
"course2"
class
=
"com.atguigu.spring5.collectiontype.Course"
>
<property name=
"cname"
value=
"MyBatis框架"
></property>
</bean>
<!--注入list集合类型,值是对象-->
<property name=
"courseList"
>
<list>
<ref bean=
"course1"
></ref>
<ref bean=
"course2"
></ref>
</list>
</property>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!--第一步:在 spring 配置文件中引入名称空间 util-->
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:util
=
"http://www.springframework.org/schema/util"
<!--添加util名称空间-->
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!--添加util名称空间-->
<!--第二步:使用 util 标签完成 list 集合注入提取-->
<!--把集合注入部分提取出来-->
<!--1 提取list集合类型属性注入-->
<
util:list
id
=
"bookList"
>
<
value
>易筋经</
value
>
<
value
>九阴真经</
value
>
<
value
>九阳神功</
value
>
</
util:list
>
<!--2 提取list集合类型属性注入使用-->
<
bean
id
=
"book"
class
=
"com.atguigu.spring5.collectiontype.Book"
scope
=
"prototype"
>
<
property
name
=
"list"
ref
=
"bookList"
></
property
>
</
bean
>
|
1.Spring 有两种类型 bean,一种普通 bean,另外一种工厂 bean(FactoryBean) 。
2.普通 bean:在配置文件中定义 bean 类型就是返回类型 。
3.工厂 bean:在配置文件定义 bean 类型可以和返回类型不一样 。
(1)创建类,让这个类作为工厂 bean,实现接口 FactoryBean.
(2)第二步 实现接口里面的方法,在实现的方法中定义返回的 bean 类型.
1
2
3
4
5
6
7
8
9
|
public
class
MyBean
implements
FactoryBean<Course> {
//定义返回bean
@Override
public
Course getObject()
throws
Exception {
Course course =
new
Course();
course.setCname(
"abc"
);
return
course;
}
}
|
1
2
|
<bean id=
"myBean"
class
=
"com.atguigu.spring5.factorybean.MyBean"
>
</bean>
|
1
2
3
4
5
6
7
|
@Test
public
void
test3() {
ApplicationContext context =
new
ClassPathXmlApplicationContext(
"bean3.xml"
);
Course course = context.getBean(
"myBean"
, Course.
class
);
//返回值类型可以不是定义的bean类型!
System.out.println(course);
}
|
在 Spring 里面,默认情况下,bean 是单实例对象,下面进行作用域设置:
1.在 spring 配置文件 bean 标签里面有属性(scope)用于设置单实例还是多实例.
2.scope 属性值 。
1.第一个值:默认值,singleton,表示是单实例对象 .
2.第二个值:prototype,表示是多实例对象.
。
1
2
3
|
<bean id=
"book"
class
=
"com.atguigu.spring5.collectiontype.Book"
scope=
"prototype"
><!--设置为多实例-->
<property name=
"list"
ref=
"bookList"
></property>
</bean>
|
3.singleton 和 prototype 区别:
1.singleton 单实例,prototype 多实例.
2.设置 scope 值是 singleton 时候,加载 spring 配置文件时候就会创建单实例对象。 设置 scope 值是 prototype 时候,不是在加载 spring 配置文件时候创建对象,在调用 getBean 方法时候创建多实例对象.
1.生命周期:从对象创建到对象销毁的过程.
2.bean 生命周期 。
①通过构造器创建 bean 实例(无参数构造) 。
②为 bean 的属性设置值和对其他 bean 引用(调用 set 方法) 。
③调用 bean 的初始化的方法(需要进行配置初始化的方法) 。
④bean 可以使用了(对象获取到了) 。
⑤当容器关闭时候,调用 bean 的销毁的方法(需要进行配置销毁的方法) 。
3.演示bean生命周期 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public
class
Orders {
//无参数构造
public
Orders() {
System.out.println(
"第一步---执行无参数构造创建bean实例"
);
}
private
String oname;
public
void
setOname(String oname) {
this
.oname = oname;
System.out.println(
"第二步---调用set方法设置属性值"
);
}
//创建执行的初始化的方法
public
void
initMethod() {
System.out.println(
"第三步---执行初始化的方法"
);
}
//创建执行的销毁的方法
public
void
destroyMethod() {
System.out.println(
"第五步---执行销毁的方法"
);
}
}
|
1
2
3
4
|
<bean id=
"orders"
class
=
"com.atguigu.spring5.bean.Orders"
init-method=
"initMethod"
destroy-method=
"destroyMethod"
>
<property name=
"oName"
value=
"周杰伦"
>
</property>
</bean>
|
1
2
3
4
5
6
7
8
|
@Test
public
void
test1(){
ApplicationContext context=
new
ClassPathXmlApplicationContext(
"bean1.xml"
);
Orders orders = context.getBean(
"orders"
, Orders.
class
);
System.out.println(
"第四步---获取创建bean实例对象"
);
System.out.println(orders);
((ClassPathXmlApplicationContext)context).close();
}
|
输出: 第一步---执行无参数构造创建bean实例 第二步---调用set方法设置属性值 第三步---执行初始化的方法 第四步---获取创建bean实例对象 。
4.bean 的后置处理器,bean 生命周期有七步 。
①通过构造器创建 bean 实例(无参数构造) 。
②为 bean 的属性设置值和对其他 bean 引用(调用 set 方法) 。
③把 bean 实例传递 bean 后置处理器的方法 postProcessBeforeInitialization 。
④调用 bean 的初始化的方法(需要进行配置初始化的方法) 。
⑤把 bean 实例传递 bean 后置处理器的方法 postProcessAfterInitialization 。
⑥bean 可以使用了(对象获取到了) 。
⑦当容器关闭时候,调用 bean 的销毁的方法(需要进行配置销毁的方法) 。
5.演示添加后置处理器效果 。
(1)创建类,实现接口BeanPostProcessor,创建后置处理器.
1
2
3
4
5
6
7
8
9
10
11
12
|
public
class
MyBeanPost
implements
BeanPostProcessor {
//创建后置处理器实现类
@Override
public
Object postProcessBeforeInitialization(Object bean, String beanName)
throws
BeansException {
System.out.println(
"在初始化之前执行的方法"
);
return
bean;
}
@Override
public
Object postProcessAfterInitialization(Object bean, String beanName)
throws
BeansException {
System.out.println(
"在初始化之后执行的方法"
);
return
bean;
}
}
|
(2)配置后置处理器 。
1
2
3
4
5
6
|
<!--配置文件的bean参数配置-->
<bean id=
"orders"
class
=
"com.atguigu.spring5.bean.Orders"
init-method=
"initMethod"
destroy-method=
"destroyMethod"
> <!--配置初始化方法和销毁方法-->
<property name=
"oname"
value=
"手机"
></property><!--这里就是通过set方式(注入属性)赋值-->
</bean>
<!--配置后置处理器-->
<bean id=
"myBeanPost"
class
=
"com.atguigu.spring5.bean.MyBeanPost"
></bean>
|
1.什么是自动装配?
根据指定装配规则(属性名称或者属性类型),Spring 自动将匹配的属性值进行注 。
2.演示自动装配过程 。
①根据属性名称自动注入 。
1
2
3
4
5
6
7
8
9
|
<!--实现自动装配
bean 标签属性 autowire,配置自动装配
autowire 属性常用两个值:
byName 根据属性名称注入 ,注入值 bean 的 id 值和类属性名称一样
byType 根据属性类型注入
-->
<bean id=
"emp"
class
=
"com.atguigu.spring5.autowire.Emp"
autowire=
"byName"
>
</bean>
<bean id=
"dept"
class
=
"com.atguigu.spring5.autowire.Dept"
></bean>
|
②根据属性类型自动注入 。
1
2
3
|
<bean id=
"emp"
class
=
"com.atguigu.spring5.autowire.Emp"
autowire=
"byType"
>
</bean>
<bean id=
"dept"
class
=
"com.atguigu.spring5.autowire.Dept"
></bean>
|
1.直接配置数据库信息 。
①配置Druid连接池 .
②引入Druid连接池依赖 jar 包.
1
2
3
4
5
6
7
|
<!--直接配置连接池-->
<bean id=
"dataSource"
class
=
"com.alibaba.druid.pool.DruidDataSource"
>
<property name=
"driverClassName"
value=
"com.mysql.jdbc.Driver"
></property>
<property name=
"url"
value=
"jdbc:mysql://localhost:3306/userDb"
></property>
<property name=
"username"
value=
"root"
></property>
<property name=
"password"
value=
"root"
></property>
</bean>
|
引入外部属性文件配置数据库连接池 。
①创建外部属性文件,properties 格式文件,写数据库信息(jdbc.properties) 。
1
2
3
4
|
prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/userDb
prop.userName=root
prop.password=root
|
②把外部 properties 属性文件引入到 spring 配置文件中 —— 引入 context 名称空间.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:context
=
"http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--引入context名称空间-->
<!--引入外部属性文件-->
<
context:property-placeholder
location
=
"classpath:jdbc.properties"
/>
<!--配置连接池-->
<
bean
id
=
"dataSource"
class
=
"com.alibaba.druid.pool.DruidDataSource"
>
<
property
name
=
"driverClassName"
value
=
"${prop.driverClass}"
></
property
>
<
property
name
=
"url"
value
=
"${prop.url}"
></
property
>
<
property
name
=
"username"
value
=
"${prop.userName}"
></
property
>
<
property
name
=
"password"
value
=
"${prop.password}"
></
property
>
</
bean
>
</
beans
>
|
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注我的更多内容! 。
原文链接:https://blog.csdn.net/weixin_43883917/article/details/119923955 。
最后此篇关于Spring-IOC容器-Bean管理-基于XML方式超详解的文章就讲到这里了,如果你想了解更多关于Spring-IOC容器-Bean管理-基于XML方式超详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
全称“Java Virtual Machine statistics monitoring tool”(statistics 统计;monitoring 监控;tool 工具) 用于监控虚拟机的各种运
主要是讲下Mongodb的索引的查看、创建、删除、类型说明,还有就是Explain执行计划的解释说明。 可以转载,但请注明出处。  
1>单线程或者单进程 相当于短链接,当accept之后,就开始数据的接收和数据的发送,不接受新的连接,即一个server,一个client 不存在并发。 2>循环服务器和并发服务器
详解 linux中的关机和重启命令 一 shutdown命令 shutdown [选项] 时间 选项: ?
首先,将json串转为一个JObject对象: ? 1
matplotlib官网 matplotlib库默认英文字体 添加黑体(‘SimHei')为绘图字体 代码: plt.rcParams['font.sans-serif']=['SimHei'
在并发编程中,synchronized关键字是常出现的角色。之前我们都称呼synchronized关键字为重量锁,但是在jdk1.6中对synchronized进行了优化,引入了偏向锁、轻量锁。本篇
一般我们的项目中会使用1到2个数据库连接配置,同程艺龙的数据库连接配置被收拢到统一的配置中心,由DBA统一配置和维护,业务方通过某个字符串配置拿到的是Connection对象。  
实例如下: ? 1
1. MemoryCahe NetCore中的缓存和System.Runtime.Caching很相似,但是在功能上做了增强,缓存的key支持object类型;提供了泛型支持;可以读缓存和单个缓存
argument是javascript中函数的一个特殊参数,例如下文,利用argument访问函数参数,判断函数是否执行 复制代码 代码如下: <script
一不小心装了一个Redis服务,开了一个全网的默认端口,一开始以为这台服务器没有公网ip,结果发现之后悔之莫及啊 某天发现cpu load高的出奇,发现一个minerd进程 占了大量cpu,googl
今天写这个是为了 提醒自己 编程过程 不仅要有逻辑 思想 还有要规范 代码 这样可读性 1、PHP 编程规范与编码习惯最主要的有以下几点: 1 文件说明 2 funct
摘要:虚拟机安装时一般都采用最小化安装,默认没有lspci工具。一台测试虚拟网卡性能的虚拟机,需要lspci工具来查看网卡的类型。本文描述了在一个虚拟机中安装lspci工具的具体步骤。 由于要测试
1、修改用户进程可打开文件数限制 在Linux平台上,无论编写客户端程序还是服务端程序,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统
目录 算术运算符 基本四则运算符 增量赋值运算符 自增/自减运算符 关系运算符 逻
如下所示: ? 1
MapperScannerConfigurer之sqlSessionFactory注入方式讲解 首先,Mybatis中的有一段配置非常方便,省去我们去写DaoImpl(Dao层实现类)的时间,这个
Linux的网络虚拟化是LXC项目中的一个子项目,LXC包括文件系统虚拟化,进程空间虚拟化,用户虚拟化,网络虚拟化,等等,这里使用LXC的网络虚拟化来模拟多个网络环境。 本文从基本的网络设备讲
? 1
我是一名优秀的程序员,十分优秀!