Spring Bean的生命周期
来源:互联网 发布:怎么做淘客软件 编辑:程序博客网 时间:2024/06/06 02:50
这篇文章里主要回顾Spring里Bean的生命周期,并通过一个例子来验证其生命周期。
Bean生命周期:
- Spring调用bean构造器,创建实例对象;
- Spring调用bean的setXxx()方法,将属性注入进该bean;
- 如果bean实现了BeanNameAware接口,则调用执行setBeanName(String beanId)方法;
- 如果bean实现了BeanFactoryAware接口,则调用执行setBeanFactory(BeanFactory factory)方法;
- 如果该bean实现了ApplicationContextAware这个接口,则Spring此时会调用执行setApplicationContext(ApplicationContext arg0)方法;
- 如果容器注册了BeanPostProcessor的实现类(注意这里是容器,而不是bean),则调用执行postProcessBeforeInitialization(Object bean, String beanName)方法;
- 如果bean实现了InitializingBean接口,Spring将调用afterPropertiesSet()方法;
- 如果bean在配置文件里配置了init-method属性,则执行init-method指定的方法;
- 如果容器注册了BeanPostProcessor的实现类(注意这里是容器,而不是bean),则调用执行postProcessorAfterInitialization(Object bean, String beanName)方法;
- 此时,bean的初始化准备就绪,可以被应用程序使用了,下面的例子里在Main主类里调用Service bean的业务方法service();
- 如果bean实现了DisposableBean接口,Spring将调用destroy()方法;
- 如果bean在配置文件里配置了destroy-method属性,则执行destroy-method指定的方法。
代码验证:
接口:
public interface IService {}
接口实现类:
import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.BeanFactoryAware;import org.springframework.beans.factory.BeanNameAware;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;import org.springframework.beans.factory.config.BeanPostProcessor;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;public class Service implements IService, BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean{ private String service; public Service(){ System.out.println("Constructor:构造器方法"); } public void setService(String arg0){ this.service = arg0; System.out.println("setXxx:setService方法"); } @Override public void setBeanName(String name) { // TODO Auto-generated method stub System.out.println("BeanNameAware:setBeanName方法"); } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { // TODO Auto-generated method stub System.out.println("BeanFactoryAware:setBeanFactory方法"); } @Override public void setApplicationContext(ApplicationContext arg0) throws BeansException { // TODO Auto-generated method stub System.out.println("ApplicationContextAware:setApplicationContext方法"); } @Override public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println("InitializingBean:afterPropertiesSet"); } public void serviceInit(){ System.out.println("init-method:serviceInit"); } public void service(){ System.out.println("service:业务方法"); } @Override public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println("DisposableBean:destroy"); } public void serviceDestroy(){ System.out.println("destroy-method:serviceDestroy"); }}
Bean后处理器类
import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("BeanPostProcessor:bean后处理器before方法"); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("BeanPostProcessor:bean后处理器after方法"); return bean; }}
配置文件:config.xml
<?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-3.2.xsd"> <bean id="service" class="bean.lifecycle.Service" init-method="serviceInit" destroy-method="serviceDestroy"> <property name="service" value="service"></property> </bean> <bean class="bean.lifecycle.MyBeanPostProcessor"/></beans>
主类:
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main { public static void main(String[] args){ ApplicationContext ac = new ClassPathXmlApplicationContext("config.xml"); Service service = (Service)ac.getBean("service"); service.service(); ((ClassPathXmlApplicationContext)ac).close(); }}
代码运行结果:
Constructor:构造器方法
setXxx:setService方法
BeanNameAware:setBeanName方法
BeanFactoryAware:setBeanFactory方法
ApplicationContextAware:setApplicationContext方法
BeanPostProcessor:bean后处理器before方法
InitializingBean:afterPropertiesSet
init-method:serviceInit
BeanPostProcessor:bean后处理器after方法
service:业务方法
DisposableBean:destroy
destroy-method:serviceDestroy
这段代码有几个需要注意的地方:
- BeanPostProcessor必须由另一个类实现,不能由Service类实现,否则的话,容器启动时并不会执行BeanPostProcessor里的两个方法,也就是说BeanPostProcessor的实现类所影响的范围是容器里没有实现BeanPostProcessor接口的类;
- ApplicationContext接口里并没有定义close()方法,但是其实现类里有这个方法,用来关闭容器,容器里的bean直到该容器应用上下文销毁才会被销毁。
阅读全文
1 0
- Spring Bean的生命周期
- Spring Bean的生命周期
- spring - bean的生命周期
- Spring Bean的生命周期
- spring bean 的生命周期
- Spring bean的生命周期
- spring bean的生命周期
- Spring Bean的生命周期
- Spring Bean的生命周期
- Spring bean的生命周期
- Spring Bean的生命周期
- Spring Bean的生命周期
- Spring bean的生命周期
- Spring Bean的生命周期
- Spring Bean的生命周期
- Spring Bean 的生命周期
- spring Bean的生命周期
- spring Bean的生命周期
- 2017ICPCECIC MJF wants to work
- Spring思维导图,让Spring不再难懂(cache篇)
- centos Nginx Django uwsgi踩坑之ModuleNotFoundError: No module named 'django'
- 求m和n的算法题
- 洛谷P1848 [USACO12OPEN]书架Bookshelf
- Spring Bean的生命周期
- Java编程思想之I/O系统
- Html5实现自己的分页加载
- python入门基础
- Spark系列1 Spark 安装
- Java并发编程艺术 5 Java中的锁
- springboot入門(一) helloworld
- session与cookie的区别与联系
- [Tensorflow]基于slim框架下inception模型的植物识别