什么是IOC
(1)控制反转,把我们对象创建和对象之间的调用过程,交给Spring进行管理
(2)使用IOC目的:为了耦合度降低
(3)做入门案例就是IOC实现
IOC底层原理
(1)xml解析,工厂模式,反射
(2) IOC过程:
IOC接口(BeanFactory)
-
IOC思想基于IOC容器完成,IOC容器底层就是对象工厂
-
spring提供IOC容器实现两种方式:(两个接口)
(1)BeanFactory:IOC容器基本实现,是spring内部使用的接口,不提供给开发人员使用。
- 加载配置文件时候不会创建对象,在获取对象(使用)才去创建对象
(2)ApplicationContext:BeanFactory接口的子接口,提供更过更强大的功能,一般
由开发人员使用。
- 加载配置文件的时候就会把在配置文件对象进行创建
-
ApplicationContext接口有实现类
IOC操作Bean管理(基于xml)
-
什么是bean管理
- Bean管理指的是两个操作
(1)Spring创建对象
(2)Spring注入属性
-
Bean管理操作有两种方式
(1)基于xml配置文件方式实现
复制代码1
2
3<!-- 配置User对象创建--> <bean id="user" class="spring5.User"></bean>
-
在配置文件中,使用bean标签,标签里面加对应属性,就可以实现对象创建
-
在bean标签有很多属性,介绍常用的属性
(1)id属性:唯一标识
(2)class:类全路径(包全路径)
(3)创建对象时候,默认也是执行无参构造方法完成对象创建
(2)基于xml方式注入属性
DI:依赖注入,就是注入属性
第一种注入方式:使用set方法进行注入
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20package spring5; /** * 演示使用set方法进行注入属性 */ public class Book { private String bname; private String bauthor; //set方法注入 public void setBname(String bname){ this.bname=bname; } public void setBauthor(String bauthor) { this.bauthor = bauthor; } public void testBook(){ System.out.println(bname+","+bauthor); } }
复制代码1
2
3
4
5<bean id="book" class="spring5.Book"> <property name="bname" value="天龙八部"></property> <property name="bauthor" value="金庸"></property> </bean>
复制代码1
2
3
4
5
6
7
8
9
10
11@Test public void testBook(){ // 加载spring配置文件 ApplicationContext context= new ClassPathXmlApplicationContext("bean1.xml"); // 获取配置创建对象 Book book=context.getBean("book", Book.class); System.out.println(book); book.testBook(); }
第二种注入方式:使用有参构造
复制代码1
2
3
4
5
6
7
8
9public class Orders { private String oname; private String address; public Orders(String oname, String address) { this.oname = oname; this.address = address; } }
复制代码1
2
3
4
5
6<!-- 使用有参构造注入属性--> <bean id="orders" class="spring5.Orders"> <constructor-arg name="oname" value="abc"></constructor-arg> <constructor-arg name="address" value="陕西省"></constructor-arg> </bean>
-
1
2
3
4
5
6
7
8
9
10
11@Test public void testOrders(){ // 加载spring配置文件 ApplicationContext context= new ClassPathXmlApplicationContext("bean1.xml"); // 获取配置创建对象 Orders orders=context.getBean("orders", Orders.class); System.out.println(orders); orders.testOrders(); }
第三种注入方式:使用p名称空间注入
(1)使用p名称空间注入,可以简化基于xml配置方式(底层也是set方法注入)
1
2
3
4
5
6<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
1
2<bean id="book" class="spring5.Book" p:bname="水浒传" p:bauthor="施耐庵"></bean>
- 两个尖括号的使用:
1
2
3
4
5
6
7
8
9
10<!-- << >> 两个尖括号--> <!-- <![CDATA[<<金庸>>]]>--> <bean id="book" class="spring5.Book"> <property name="bname" value="<<天龙八部>>"></property> <property name="bauthor"> <value><![CDATA[<<金庸>>]]></value> </property> </bean>
spring5.Book@173ed316
<<天龙八部>>,<<金庸>>
-
注入属性-外部bean
(1)创建两个类service类和dao类
复制代码1
2
3
4
5
6
7
8
9
10
11public interface UserDao { void update(); } public class UserDaoImpi implements UserDao{ @Override public void update() { System.out.println("dao update.............."); } }
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16public class UserService { //创建UserDao类型的属性,生成set方法 private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void add(){ System.out.println("service add..............."); //原始方式 // UserDaoImpi userDaoImpi=new UserDaoImpi(); // userDaoImpi.update(); userDao.update(); } }
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public class UserService { //创建UserDao类型的属性,生成set方法 private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void add(){ System.out.println("service add..............."); //原始方式 // UserDaoImpi userDaoImpi=new UserDaoImpi(); // userDaoImpi.update(); userDao.update(); } }
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- service和dao对象创建--> <bean id="userService" class="spring5.service.UserService"> <!-- 注入userDao对象--> <!-- name属性:类里面有属性名称--> <!-- ref属性:创建userDao对象bean标签id值--> <property name="userDao" ref="userDaoImpi"></property> </bean> <bean id="userDaoImpi" class="spring5.dao.UserDaoImpi"></bean> </beans>
(2)在service调用dao里面的方法
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13public class TestBean { @Test public void testUserService(){ // 加载spring配置文件 ApplicationContext context= new ClassPathXmlApplicationContext("bean2.xml"); // 获取配置创建对象 UserService userservice=context.getBean("userService", UserService.class); System.out.println(userservice); userservice.add(); } }
-
注入属性-内部bean和级联赋值
(1)一对多关系:部门和员工
一个部门有多个员工,一个员工属于一个部门
部门是一,员工是多
(2)在实体类之间表示一对多关系,员工表示所属部门,使用对象的属性进行表示
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
32
33
34
35//部门类 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; } } @Test public void testDept(){ // 加载spring配置文件 ApplicationContext context= new ClassPathXmlApplicationContext("bean3.xml"); // 获取配置创建对象 Emp emp=context.getBean("emp", Emp.class); System.out.println(emp); emp.add(); }
(3)在spring配置文件中进行配置(在属性property中还可以设置bean)
1
2
3
4
5
6
7
8
9
10
11
12
13<!-- 内部bean--> <bean id="emp" class="spring5.bean.Emp"> <!-- 先设置两个普通的属性--> <property name="ename" value="lucy"></property> <property name="gender" value="女"></property> <!-- 设置对象属性--> <property name="dept"> <bean id="dept" class="spring5.bean.Dept"> <property name="dname" value="保安部"></property> </bean> </property> </bean>
(4)注入属性-级联赋值(和内部bean是一样的)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21<!-- 内部bean--> <bean id="emp" class="spring5.bean.Emp"> <!-- 先设置两个普通的属性--> <property name="ename" value="lucy"></property> <property name="gender" value="女"></property> <!-- 级联赋值--> <property name="dept" ref="dept"></property> </bean> <bean id="dept" class="spring5.bean.Dept"> <property name="dname" value="财务部"></property> </bean> <bean id="emp" class="spring5.bean.Emp"> <!-- 先设置两个普通的属性--> <property name="ename" value="lucy"></property> <property name="gender" value="女"></property> <!-- 级联赋值--> <property name="dept" ref="dept"></property> <property name="dept.dname" value="技术部"></property> //设置get方法这里也可以 </bean>
IOC操作Bean管理(基于注解)
- 注入集合属性值
-
注入数组类型属性
-
注入List集合类型属性
-
注入Map集合类型属性
-
注入Set集合类型属性
(1)创建这些集合并生成set方法
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
29public class Stu { //1.数组类型的属性 private String[] courses; //2.List集合类型属性 private List<String> list; //3.创建一个Map集合属性 private Map<String,String> map; private Set<String> set; public void setSet(Set<String> set) { this.set = set; } public void setList(List<String> list) { this.list = list; } public void setMap(Map<String, String> map) { this.map = map; } public void setCourses(String[] courses) { this.courses = courses; } } @Test public void testCollection(){ ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml"); Stu stu=context.getBean("stu",Stu.class); stu.test(); }
(2)在spring配置文件中进行配置
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<!-- 集合类型属性的注入--> <bean id="stu" class="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="map"> <map> <entry key="张三" value="男"></entry> <entry key="李四" value="女"></entry> </map> </property> <!-- set集合--> <property name="set"> <set> <value>mysql</value> <value>redis</value> </set> </property> </bean>
-
注入集合对象值
(1)在集合里面设置对象类型值
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20<!-- 注入list集合类型,值是对象--> <property name="listCouse" > <list> <ref bean="couse1" ></ref> <ref bean="couse2" ></ref> <ref bean="couse3" ></ref> </list> </property> <!-- 创建多个couse对象--> <bean id="couse1" class="spring5.collectionType.Couse"> <property name="cname" value="spring框架"></property> </bean> <bean id="couse2" class="spring5.collectionType.Couse"> <property name="cname" value="mysql"></property> </bean> <bean id="couse3" class="spring5.collectionType.Couse"> <property name="cname" value="java"></property> </bean>
(2)把集合注入部分提取出来做一个公共部分
在spring的配置文件中先引入一个名称空间util
复制代码1
2
3
4
5
6
7
8
9
10<?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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/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"> <!-- 集合类型属性的注入--> </beans>
1
2
3
4
5
6
7
8
9
10
11
12<!-- 集合类型属性的注入--> <util:list id="bookList"> <value>易筋经</value> <value>九阴真经</value> <value>酒养生功</value> </util:list> <!-- 提取lsit集合属性注入使用--> <bean id="book" class="spring5.collectionType.Book"> <property name="list" ref="bookList"> </property> </bean>
IOC操作Bean管理(FactoryBean)
-
Spring有两种类型Bean,一种普通Bean,另外一种工厂Bean
-
普通bean :在配置文件中定义的类型就是文件中返回的类型
-
工厂bean:定义的类型可以和返回的类型不一样
- 第一步,创建类,让这个作为工厂bean,实现接口FactoryBean
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20public class Mybean implements FactoryBean<Couse> { //定义返回Bean @Override public Couse getObject() throws Exception { Couse couse=new Couse(); couse.setCname("aaaa"); return couse; } @Override public Class<?> getObjectType() { return null; } } @Test public void test3(){ ApplicationContext context=new ClassPathXmlApplicationContext("bean3.xml"); Couse mybean=context.getBean("myBean", Couse.class); System.out.println(mybean); }
- 第二步,实现接口里面的方法,在实现的方法中定义返回的bean类型
1
2<bean id="myBean" class="factorybean.Mybean.Mybean"></bean>
IOC操作Bean管理(Bean作用域)
- 在spring里面,设置创建Bean实例是单实例还是多实例
- 在spring里面,默认情况下,bean是单实例对象
拿到的地址是相同的
-
如何在设置是单实例还是多实例
(1)在spring配置文件中bean标签里面有
(2)scope属性值
第一个值 默认值,singleton,表示是单实例
第二个值 prototype,表示是多实例对象
复制代码1
2<bean id="myBean" class="factorybean.Mybean.Mybean" scope="prototype"></bean>
设置成多实例之后拿到的地址值是不相同的
(3)singleton和prototype区别
第一 singleton单实例,prototype是多实例
第二 设置singleton时候,加载spring配置文件时候就会创建实例对象
设置prototype时候,调用getBean方法创建实例对象
IOC操作bean的生命周期:
- 生命周期
(1)从对象创建到对象销毁的过程
-
bean生命周期
(1)通过构造器创建bena实例(无参构造)
(2)为bean的属性设置值和对其他bean引用(调用set方法)
(3)调用bean的初始化方法(需要进行配置)
(4)bean可以使用了(对象获取到了)
(5)当容器关闭时候,调用bean的销毁方法(需要进行配置销毁方法)
-
演示bean的生命周期
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19public 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="factorybean.Mybean.Orders" init-method="initMethod" destroy-method="destroyMethod"> <property name="oname" value="手机"></property> </bean>
1
2
3
4
5
6
7
8
9
10
11
12
13@Test public void testBean4(){ // ApplicationContext context=new ClassPathXmlApplicationContext("bean4.xml"); //这里直接调用子接口 ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("bean4.xml"); Orders orders=context.getBean("orders", Orders.class); System.out.println("第四步 获取创建bean实例对象"); System.out.println(orders); //手动bean实例销毁 // ((ClassPathXmlApplicationContext) context).close(); context.close(); }
运行结果:
第一步,执行 无参构造创建bean实例
第二步 调用set方法设置属性值
第三步 执行初始化的方法
第四步 获取创建bean实例对象
factorybean.Mybean.Orders@74d1dc36
第五步 执行销毁的方法
-
bean 的后置处理器,bean的生命周期有七步
(1)通过构造器创建bena实例(无参构造)
(2)为bean的属性设置值和对其他bean引用(调用set方法)
(3)把bean实例传递给bean后置处理器方法
复制代码1
2postProcessBeforeInitialization
(4)调用bean的初始化方法(需要进行配置)
(5)把bean实例传递bean后置处理器的方法
复制代码1
2postProcessAfterInitialization
(6)bean可以使用了(对象获取到了)
(7)当容器关闭时候,调用bean的销毁方法(需要进行配置销毁方法)
-
演示添加后置处理器效果
(1)创建类,实现接口BeanPostProcessor,创建后置处理器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public class MybeanPsot 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; } }
1
2
3<!-- 后置处理器--> <bean id="myBeanPost" class="factorybean.Mybean.MybeanPsot"></bean>
运行结果:
第一步,执行 无参构造创建bean实例
第二步 调用set方法设置属性值
在初始化之“前”执行的方法
第三步 执行初始化的方法
在初始化之“后”执行的方法
第四步 获取创建bean实例对象
factorybean.Mybean.Orders@53045c6c
第五步 执行销毁的方法
IOC操作Bean管理(xml自动装配)
-
什么是自动装配
(1)根据指定装配规则(属性名或者属性类型),Spring自动将匹配的属性值进行注入
-
演示自动装配过程
(1)根据属性名称自动注入
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21<!-- 后置处理器 实现自动装配 bean标签属性autowire,配置自动装配 autowire属性常用两个值: byName根据属性名称注入 ,注入的值和类属性名称一一样 byType根据属性类型注入, --> <bean id="emp" class="autowire.Emp" autowire="byName"> <!-- <property name="dept" ref="dept"></property>--> </bean> <!-- 引入外部bean--> <bean id="dept" class="autowire.Dept"></bean> // 根据属性类型进行注入,但是有多个类型会报错 <bean id="emp" class="autowire.Emp" autowire="byType"> <!-- <property name="dept" ref="dept"></property>--> </bean> <!-- 引入外部bean--> <bean id="dept" class="autowire.Dept"></bean> </beans>
IOC操作Bean管理(外部属性文件)
-
直接配置数据库信息
(1)配置德鲁伊连接池
(2)引入德鲁伊连接池依赖jar包
复制代码1
2
3
4
5
6
7
8
9<!-- 直接配置连接池--> <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>
-
引入外部属性配置数据库连接池
(1)创建外部属性文件,properties格式文件,写数据库信息
复制代码1
2
3
4
5prop.driverClass=com.mysql.jdbc.Driver prop.url=jdbc:mysql://localhost:3306/userDb prop.userName=root prop.password=123456
(2)把外部properties属性文件引入到spring配置文件中
*引入context名称空间
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<?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:p="http://www.springframework.org/schema/p" 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"> <!-- 直接配置连接池--> <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> </beans>
最后
以上就是狂野指甲油最近收集整理的关于spring5-ioc容器-xml解析-操作bean的全部内容,更多相关spring5-ioc容器-xml解析-操作bean内容请搜索靠谱客的其他文章。
发表评论 取消回复