IOC操作Bean管理(bean作用域)
1.在Spring里面,设置创建bean实例是单实例还是多实例
2.在Spring里面,默认情况下,bean是单实例对象
package com.atguigu.spring.test;import com.atguigu.spring.collectiontype.Book;
import com.atguigu.spring.collectiontype.Course;
import com.atguigu.spring.factoryBean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class testBook {@Testpublic void testCollection(){ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");Book book1 = context.getBean("book", Book.class);Book book2 = context.getBean("book",Book.class);System.out.println(book1);System.out.println(book2);// com.atguigu.spring.collectiontype.Book@6aaceffd
// com.atguigu.spring.collectiontype.Book@6aaceffd}
}
3.如何设置单实例还是多实例
(1)在spring配置文件bean标签里面有属性用于设置单实例还是多实例
(2)scope属性值
第一个值 默认值,singleton,表示是单实例对象
第二个值 prototype,表示是多实例对象
了解的两个值:
request:每次创建对象,会把对象放在Request域中
session:每次创建对象,会把对象放在Session域中
(3)singleton和prototype区别
第一 singleton单实例,prototype 多实例
第二 设置scope值是singleton的时候,加载spring配置文件的时候就会创建单实例对象
设置scope值是prototype的时候,不是在加载spring配置文件的时候创建对象,在调用getBean方法的时候创建多实例对象
<bean id = "book" class = "com.atguigu.spring.collectiontype.Book" scope="prototype" ><property name="list" ref = "bookList"></property></bean>
测试:
package com.atguigu.spring.test;import com.atguigu.spring.collectiontype.Book;
import com.atguigu.spring.collectiontype.Course;
import com.atguigu.spring.factoryBean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class testBook {@Testpublic void testCollection(){ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");Book book1 = context.getBean("book", Book.class);Book book2 = context.getBean("book",Book.class);System.out.println(book1);System.out.println(book2);// com.atguigu.spring.collectiontype.Book@6aaceffd
// com.atguigu.spring.collectiontype.Book@c86b9e3}
}
IOC操作Bean管理(bean生命周期)
1.生命周期
(1)从对象创建到对象销毁的过程
2.bean生命周期
(1)通过构造器创建bean实例(无参构造)
(2)为bean的属性设置值和其他bean引用(调用set方法)
(3)调用bean的初始化的方法(需要进行配置初始化的方法)
(4)bean可以使用了(对象获取到了)
(5)当容器关闭时候,调用bean的销毁的方法(需要进行配置销毁的方法)
3.演示bean生命周期
package com.atguigu.spring.collectiontype;public class Order {private String oname;//无参构造public Order(){System.out.println("1.order无参构造");}public void setOname(String oname){this.oname = oname;System.out.println("2.调用set方法设置属性值");}//创建执行的初始化的方法public void initMethod(){System.out.println("3.执行初始化方法");}//创建执行的销毁的方法public void destroyMethod(){System.out.println("5.执行销毁的方法");}}
<?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 = "order" class = "com.atguigu.spring.collectiontype.Order" init-method="initMethod" destroy-method="destroyMethod"><property name="oname" value="手机"></property></bean></beans>
测试:
package com.atguigu.spring.test;import com.atguigu.spring.collectiontype.Book;
import com.atguigu.spring.collectiontype.Course;
import com.atguigu.spring.collectiontype.Order;
import com.atguigu.spring.factoryBean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class testBook {@Testpublic void testCollection3(){ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");Order order = context.getBean("order", Order.class);System.out.println("4.获取创建bean实例对象");System.out.println(order);//手动让bean实例销毁((ClassPathXmlApplicationContext) context).close();}/*1.order无参构造
2.调用set方法设置属性值
3.执行初始化方法
4.获取创建bean实例对象
com.atguigu.spring.collectiontype.Order@3b0090a4
5.执行销毁的方法*/}
4.bean的后置处理器,bean生命周期有七步
(1)通过构造器创建bean实例(无参构造)
(2)为bean的属性设置值和其他bean引用(调用set方法)
(3)把bean实例传递给bean后置处理器的方法
@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return null;}
(4)调用bean的初始化的方法(需要进行配置初始化的方法)
(5)把bean实例传递给bean后置处理器的方法
@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {return null;}
(6)bean可以使用了(对象获取到了)
(7)当容器关闭时候,调用bean的销毁的方法(需要进行配置销毁的方法)
5.演示添加后置处理器效果
(1)创建类,实现接口BeanPostProcessor,创建后置处理器
package com.atguigu.spring.factoryBean;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;public class MyBeanPost implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("在初始化之前执行的方法");return null;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("在初始化之后执行的方法");return null;}
}
示例:
package com.atguigu.spring.factoryBean;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;public class MyBeanPost implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("在初始化之前执行的方法");return null;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("在初始化之后执行的方法");return null;}
}
<?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 = "order" class = "com.atguigu.spring.collectiontype.Order" init-method="initMethod" destroy-method="destroyMethod"><property name="oname" value="手机"></property></bean><!--配置后置处理器--><bean id = "myBeanPost" class = "com.atguigu.spring.factoryBean.MyBeanPost"></bean></beans>
测试:
package com.atguigu.spring.test;import com.atguigu.spring.collectiontype.Book;
import com.atguigu.spring.collectiontype.Course;
import com.atguigu.spring.collectiontype.Order;
import com.atguigu.spring.factoryBean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class testBook {@Testpublic void testCollection3(){ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");Order order = context.getBean("order", Order.class);System.out.println("4.获取创建bean实例对象");System.out.println(order);//手动让bean实例销毁((ClassPathXmlApplicationContext) context).close();// 1.order无参构造
// 2.调用set方法设置属性值
// 在初始化之前执行的方法
// 3.执行初始化方法
// 在初始化之后执行的方法
// 4.获取创建bean实例对象
// com.atguigu.spring.collectiontype.Order@74e52ef6
// 5.执行销毁的方法}}