1、IOC操作Bean管理(FactoryBean)
1.1 两种类型bean
(1)普通bean
在配置文件中定义bean类型就是返回类型
(2)工厂bean
在配置文件定义bean类型可以和返回类型不一样
1.2 工厂bean
(1)创建类,让这个类作为工厂bean,实现接口FactoryBean
(2)实现接口里面的方法,在实现的方法中定义返回的bean类型
(3)样例
工厂bean
package com.example.spring5.factorybean;
import com.example.spring5.collectiontype.Course;
import org.springframework.beans.factory.FactoryBean;
public class MyBean implements FactoryBean<Course> {
//定义返回bean
@Override
public Course getObject() throws Exception {
Course course = new Course();
course.setCname("abc");
return course;
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return FactoryBean.super.isSingleton();
}
}
配置类
<?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">
<bean id="myBean" class="com.example.spring5.factorybean.MyBean">
</bean>
</beans>
测试类
package com.example.spring5;
import com.example.spring5.collectiontype.Course;
import com.example.spring5.factorybean.MyBean;
import com.example.spring5.service.UserService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:bean1.xml")
class MyBeanTest {
@Test
public void testAdd(){
//1 加载spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("mybean.xml");
//2 获取配置创建的对象
Course course = context.getBean("myBean", Course.class);
System.out.println(course);
}
}
2、IOC操作Bean管理(bean作用域)
2.1 在Spring里面,设置创建bean实例是单实例还是多实例
默认情况下,bean是单实例对象
@Test
public void testCollection3() {
ApplicationContext context = new ClassPathXmlApplicationContext("util.xml");
Book book1 = context.getBean("book", Book.class);
Book book2 = context.getBean("book", Book.class);
System.out.println(book1);
System.out.println(book2);
}
结果是
2.2 设置单实例还是多实例(scope)
属性scope用于设置单实例还是多实例
(1)singleton(表示单实例对象)
(2)prototype(表示多实例对象)
<!--1 提取list集合类型属性注入使用-->
<bean id="book" class="com.example.spring5.collectiontype.Book" scope="prototype">
<property name="list" ref="bookList"></property>
</bean>
结果
2.3 singleton 和 prototype区别
(1)singleton单实例,prototype多实例
(2)设置scope值是singleton时候,加载spring配置文件时候就会创建单实例对象。
设置scope值是prototype时候,不是在加载spring配置文件时候创建对象,在调用getBean方法时候创建多实例对象。
3、IOC操作Bean管理(bean生命周期)
3.1 生命周期
(1)从对象创建到对象销毁的过程
3.2 bean生命周期
(1)通过构造器创建bean实例(无参数构造)
(2)为bean的属性设置值和对其他bean引用(调用set方法)
(3)调用bean的初始化的方法(需要进行配置初始化的方法)
(4)bean可以使用(对象获取到了)
(5)当容器关闭时候,调用bean的销毁方法(需要进行配置销毁的方法)
3.3 演示bean生命周期
实体类
package com.example.spring5.bean;
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("第五步 执行销毁的方法");
}
}
配置类
<?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">
<bean id="orders" class="com.example.spring5.bean.Orders" init-method="initMethod" destroy-method="destroyMethod">
<property name="oname" value="电脑"/>
</bean>
</beans>
测试类
package com.example.spring5;
import com.example.spring5.bean.Orders;
import com.example.spring5.service.UserService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:bean1.xml")
class OrderBeanTest {
@Test
public void testAdd(){
//1 加载spring配置文件
// ApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml");
//2 获取配置创建的对象
Orders orders = context.getBean("orders", Orders.class);
System.out.println("第四步 获取创建bean实例对象");
System.out.println(orders);
//手动让bean实例销毁
context.close();
}
}
结果
3.4 如果bean生命周期加上bean的后置处理器,bean生命周期有七步
(1)通过构造器创建bean实例(无参数构造)
(2)为bean的属性设置值和对其他bean引用(调用set方法)
(3)把bean实例传递bean后置处理器的方法
(4)调用bean的初始化的方法(需要进行配置初始化的方法)
(5)把bean实例传递bean后置处理器的方法
(6)bean可以使用(对象获取到了)
(7)当容器关闭时候,调用bean的销毁方法(需要进行配置销毁的方法)
实体类
package com.example.spring5.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.Nullable;
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;
}
}
配置类
<?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">
<bean id="orders" class="com.example.spring5.bean.Orders" init-method="initMethod" destroy-method="destroyMethod">
<property name="oname" value="电脑"/>
</bean>
<!--配置后置处理器-->
<bean id="myBeanPost" class="com.example.spring5.bean.MyBeanPost">
</bean>
</beans>
测试样例
package com.example.spring5;
import com.example.spring5.bean.Orders;
import com.example.spring5.service.UserService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:bean1.xml")
class OrderBeanTest {
@Test
public void testAdd(){
//1 加载spring配置文件
// ApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean7.xml");
//2 获取配置创建的对象
Orders orders = context.getBean("orders", Orders.class);
System.out.println("第四步 获取创建bean实例对象");
System.out.println(orders);
//手动让bean实例销毁
context.close();
}
}
结果