Spring part 2 :Bean的生命周期

本文详细介绍了Spring框架中Bean的生命周期,包括从实例化到销毁的各个阶段,并通过具体示例展示了如何利用BeanPostProcessor进行方法增强。此外还涉及了BeanNameAware、ApplicationContextAware等接口的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

Spring中Bean的完整生命周期

1) instantiate bean对象实例化

2) populate properties 封装属性

3) 如果Bean实现BeanNameAware 执行 setBeanName

4) 如果Bean实现BeanFactoryAware 或者 ApplicationContextAware 设置工厂 setBeanFactory 或者上下文对象 setApplicationContext

5) 如果存在类实现 BeanPostProcessor(后处理Bean) ,执行postProcessBeforeInitialization

6) 如果Bean实现InitializingBean 执行 afterPropertiesSet 

7) 调用<bean init-method="XXX"> 指定初始化方法 

8) 如果存在类实现 BeanPostProcessor(处理Bean) ,执行postProcessAfterInitialization

9) 执行业务处理

10)如果Bean实现 DisposableBean 执行 destroy

11)调用<bean destroy-method="XXX"> 指定销毁方法 

 

接口

public interface ILifecycle {
	public void method();
}

实现类

public class LifecycleImpl implements ILifecycle, BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean {

	public LifecycleImpl() {
		System.out.println("1) instantiate bean对象实例化");
	}

	public void myInit() {
		System.out.println("7) 调用<bean init-method=\"myInit\"> 指定初始化方法 ");
	}
	public void myDestroy(){
		System.out.println("11)调用<bean destroy-method=\"myDestroy\"> 指定销毁方法 ");
	}

	@Override
	public void method() {
		System.out.println("9) 执行业务处理");
	}

	public void setProperty(String info) {
		System.out.println("2) populate properties 封装属性");
	}

	@Override
	public void setBeanName(String name) {
		System.out.println("3) 如果Bean实现BeanNameAware 执行 setBeanName.--->" + name);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		System.out.println("4) 如果Bean实现BeanFactoryAware 或者 ApplicationContextAware 设置工厂 setBeanFactory 或者上下文对象 setApplicationContext.--->" + applicationContext);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("6) 如果Bean实现InitializingBean 执行 afterPropertiesSet ");
	}

	@Override
	public void destroy() throws Exception {
		System.out.println("10)如果Bean实现 DisposableBean 执行 destroy");
	}

}

  后处理bean,对容器内所有bean有效

public class BeanProcessor implements BeanPostProcessor {

	@Override
	public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
		System.out.println("5) 如果存在类实现 BeanPostProcessor(后处理Bean) ,执行postProcessBeforeInitialization");
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("8) 如果存在类实现 BeanPostProcessor(处理Bean) ,执行postProcessAfterInitialization");
		return bean;
	}

}

 配置

<bean id="lifecycle" class="demo02.LifecycleImpl" init-method="myInit" destroy-method="myDestroy">
		<property name="property" value="注入"></property>
	</bean>
	<bean class="demo02.BeanProcessor"></bean>

测试

	@Test
	public void demo1(){
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		LifecycleImpl bean = context.getBean("lifecycle",LifecycleImpl.class);
		bean.method();
		context.close();
	}
/**

1) instantiate bean对象实例化
2) populate properties 封装属性
3) 如果Bean实现BeanNameAware 执行 setBeanName.--->lifecycle
4) 如果Bean实现BeanFactoryAware 或者 ApplicationContextAware 设置工厂 setBeanFactory 或者上下文对象 setApplicationContext.--->org.springframework.context.support.ClassPathXmlApplicationContext@6fc5f743: startup date [Mon Sep 07 22:27:22 CST 2015]; root of context hierarchy
5) 如果存在类实现 BeanPostProcessor(后处理Bean) ,执行postProcessBeforeInitialization
6) 如果Bean实现InitializingBean 执行 afterPropertiesSet 
7) 调用<bean init-method="myInit"> 指定初始化方法 
8) 如果存在类实现 BeanPostProcessor(处理Bean) ,执行postProcessAfterInitialization
9) 执行业务处理
22:27:22,535  INFO ClassPathXmlApplicationContext:1042 - Closing org.springframework.context.support.ClassPathXmlApplicationContext@6fc5f743: startup date [Mon Sep 07 22:27:22 CST 2015]; root of context hierarchy
22:27:22,535  INFO DefaultListableBeanFactory:444 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@731d2572: defining beans [lifecycle,demo02.BeanProcessor#0]; root of factory hierarchy
10)如果Bean实现 DisposableBean 执行 destroy
11)调用<bean destroy-method="myDestroy"> 指定销毁方法 
*/

 

 可以在实现BeanPostProcessor类里对原有业务方法进行增强处理

public class BeanProcessor implements BeanPostProcessor {

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("5) 如果存在类实现 BeanPostProcessor(后处理Bean) ,执行postProcessBeforeInitialization");
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
		System.out.println("8) 如果存在类实现 BeanPostProcessor(处理Bean) ,执行postProcessAfterInitialization");
		if (beanName.equals("lifecycle")) {
			return Proxy.newProxyInstance(bean.getClass().getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
				@Override
				public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
					if (method.getName().equals("method")) {
						System.out.println("增强method=======================================");
					}
					return method.invoke(bean, args);
				}
			});
		}
		return bean;
	}
}

 输出:

/**
1) instantiate bean对象实例化
2) populate properties 封装属性
3) 如果Bean实现BeanNameAware 执行 setBeanName.--->lifecycle
4) 如果Bean实现BeanFactoryAware 或者 ApplicationContextAware 设置工厂 setBeanFactory 或者上下文对象 setApplicationContext.--->org.springframework.context.support.ClassPathXmlApplicationContext@2dec8909: startup date [Tue Sep 08 09:10:45 CST 2015]; root of context hierarchy
5) 如果存在类实现 BeanPostProcessor(后处理Bean) ,执行postProcessBeforeInitialization
6) 如果Bean实现InitializingBean 执行 afterPropertiesSet 
7) 调用<bean init-method="myInit"> 指定初始化方法 
8) 如果存在类实现 BeanPostProcessor(处理Bean) ,执行postProcessAfterInitialization
增强method=======================================
9) 执行业务处理
09:10:45,408  INFO ClassPathXmlApplicationContext:1042 - Closing org.springframework.context.support.ClassPathXmlApplicationContext@2dec8909: startup date [Tue Sep 08 09:10:45 CST 2015]; root of context hierarchy
09:10:45,408  INFO DefaultListableBeanFactory:444 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7e859a68: defining beans [lifecycle,demo02.BeanProcessor#0]; root of factory hierarchy
10)如果Bean实现 DisposableBean 执行 destroy
11)调用<bean destroy-method="myDestroy"> 指定销毁方法 
*/

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值