Spring-IOC容器-Bean管理-基于XML方式
Spring框架概述
1.Spring是轻量级的开源的JavaEE框架
2.Spring可以解决企业应用开发的复杂性
3.Spring有两个核心部分:IOC和AOP
1.IOC:控制反转,把创建对象过程交给Spring进行管理
2.AOP:面向切面,不修改源代码进行功能增强
4.Spring的特点
1.方便解耦,简化开发
2.AOP编程支持
3.方便程序测试
4.方便和其他框架进行整合
5.方便进行事务操作
6.降低API开发难度
IOC概念和原理
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方式的详解
IOC 操作 Bean 管理(set方式注入&&使用有参构造进行注入)
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 >
|
IOC 操作 Bean 管理(xml注入其他类型属性)
注入空值和特殊符号
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>
|
IOC 操作 Bean 管理(xml注入集合属性)
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 >
|
IOC 操作 Bean 管理(FactoryBean)
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);
}
|
IOC 操作 Bean 管理(bean 作用域)
在 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 方法时候创建多实例对象。
IOC 操作 Bean 管理(bean 生命周期)
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>
|
IOC 操作 Bean 管理(XML自动装配)
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>
|
IOC 操作 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