本文代码GitHub地址

Bean的生命周期是开始创建到销毁的过程。需要实现相关的类BeanNameAware   ,DisposableBean, InitializingBean ,并注册InstantiationAwareBeanPostProcessor。

Bean类实现BeanNameAware   ,DisposableBean, InitializingBean 接口

package com.bean.life.entity;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.stereotype.Component; @Component
public class User implements BeanFactoryAware
, BeanNameAware
, InitializingBean
, DisposableBean
{
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware setBeanFactory");
} @Override
public void setBeanName(String s) {
System.out.println("BeanNameAware接口: setBeanName = " + s);
} @Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean接口: afterPropertiesSet");
} @Override
public void destroy() throws Exception {
System.out.println("DisposableBean接口: destroy"); }
@PostConstruct
public void init(){
System.out.println("PostConstruct");
} @PreDestroy
public void destory(){
System.out.println("PreDestroy");
}
}

注册InstantiationAwareBeanPostProcessor接口。

这里的InstantiationAwareBeanPostProcessorAdapter是 InstantiationAwareBeanPostProcessor 的子孙类。

package com.bean.life.entity;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.stereotype.Component; import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor; @Component
public class MyInstantiationAwareBeanPostProcessor extends
InstantiationAwareBeanPostProcessorAdapter { public MyInstantiationAwareBeanPostProcessor() {
super();
} @Override
public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException { if(beanName.equals("user")){
System.out.println("MyInstantiationAwareBeanPostProcessor接口: determineCandidateConstructors: " + beanName);
} return super.determineCandidateConstructors(beanClass, beanName);
} @Override
public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
if(beanName.equals("user")){
System.out.println("MyInstantiationAwareBeanPostProcessor接口: getEarlyBeanReference: " + beanName);
} return super.getEarlyBeanReference(bean, beanName);
} @Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
if(beanName.equals("user")){
System.out.println("MyInstantiationAwareBeanPostProcessor接口: postProcessBeforeInstantiation: " + beanName);
} return super.postProcessBeforeInstantiation(beanClass, beanName);
} @Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
if(beanName.equals("user")){
System.out.println("MyInstantiationAwareBeanPostProcessor接口: postProcessAfterInstantiation : " + beanName);
} return super.postProcessAfterInstantiation(bean, beanName);
} @Override
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { if(beanName.equals("user")){
System.out.println("MyInstantiationAwareBeanPostProcessor接口: postProcessPropertyValues: " + beanName);
}
return super.postProcessPropertyValues(pvs, pds, bean, beanName);
} @Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if(beanName.equals("user")){
System.out.println("MyInstantiationAwareBeanPostProcessor接口: postProcessBeforeInitialization: " + beanName);
} return super.postProcessBeforeInitialization(bean, beanName);
} @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if(beanName.equals("user")){
System.out.println("MyInstantiationAwareBeanPostProcessor接口: postProcessAfterInitialization: " + beanName);
} return super.postProcessAfterInitialization(bean, beanName);
}
}

输出

MyBeanFactoryPostProcessor ---  BeanFactoryPostProcessor
MyInstantiationAwareBeanPostProcessor接口: postProcessBeforeInstantiation: user
MyInstantiationAwareBeanPostProcessor接口: determineCandidateConstructors: user
MyInstantiationAwareBeanPostProcessor接口: postProcessAfterInstantiation : user
MyInstantiationAwareBeanPostProcessor接口: postProcessPropertyValues: user
BeanNameAware接口: setBeanName = user
BeanFactoryAware setBeanFactory
MyInstantiationAwareBeanPostProcessor接口: postProcessBeforeInitialization: user
PostConstruct
InitializingBean接口: afterPropertiesSet
MyInstantiationAwareBeanPostProcessor接口: postProcessAfterInitialization: user PreDestroy
DisposableBean接口: destroy

生命周期图解

Spring 实现按 init-method 和destory-method的三种方式

  • 方式1:xml配置方式
package com.bean.life.entity;

public class User
{ public void init(){
System.out.println("PostConstruct");
} public void destory(){
System.out.println("PreDestroy");
}
}
<bean id="user" class="com.bean.life.entity" init-method="init" destroy-method="destroy">
</bean>
  • 方式2:注解配置

添加@PostConstruct 和@PreDestroy进行指定

package com.bean.life.entity;

@Component
public class User
{ @PostConstruct
public void init(){
System.out.println("PostConstruct");
} @PreDestroy
public void destory(){
System.out.println("PreDestroy");
}
}
  • 方式3: 使用注解@Bean
public @interface Bean {
@AliasFor("name")
String[] value() default {}; @AliasFor("value")
String[] name() default {}; Autowire autowire() default Autowire.NO; String initMethod() default ""; String destroyMethod() default "(inferred)";
}
@Configuration
public class UserConfig{ @Bean(initMethod="init",destoryMethod="destory")
public User user(){
return new User();
} }

Spring Bean 生命周期测试的更多相关文章

  1. Spring点滴四:Spring Bean生命周期

    Spring Bean 生命周期示意图: 了解Spring的生命周期非常重要,我们可以利用Spring机制来定制Bean的实例化过程. -------------------------------- ...

  2. 大厂高频面试题Spring Bean生命周期最详解

    Spring作为当前Java最流行.最强大的轻量级框架.Spring Bean的生命周期也是面试高频题,了解Spring Bean周期也能更好地帮助我们解决日常开发中的问题.程序员应该都知道Sprin ...

  3. Spring Bean生命周期,好像人的一生。。

    大家好,我是老三,上节我们手撸了一个简单的IOC容器五分钟,手撸一个Spring容器!,这节我们来看一看Spring中Bean的生命周期,我发现,和人的一生真的很像. 简单说说IoC和Bean IoC ...

  4. spring bean 生命周期和 ? 作用域? spirng bean 相互依赖? jvm oom ? jvm 监控工具? ThreadLocal 原理

    1. spring bean 生命周期 1. 实例化一个bean ,即new 2. 初始化bean 的属性 3. 如果实现接口 BeanNameAware ,调用 setBeanName 4. Bea ...

  5. Spring Bean 生命周期之destroy——终极信仰

    上一篇文章 Spring Bean 生命周期之我从哪里来 说明了我是谁? 和 我从哪里来? 的两大哲学问题,今天我们要讨论一下终极哲学我要到哪里去? 初始化 Spring Bean 有三种方式: @P ...

  6. 常见问题:Web/Servlet生命周期与Spring Bean生命周期

    Servlet生命周期 init()初始化阶段 Servlet容器加载Servlet(web.xml中有load-on-startup=1;Servlet容器启动后用户首次向Servlet发请求;Se ...

  7. 睡前聊一聊"spring bean 生命周期"

    spring bean 生命周期=实属初销+2个常见接口+3个Aware型接口+2个生命周期接口 实属初销:spring bean生命周期只有四个阶段,即实例化->属性赋值->初始化-&g ...

  8. Spring Bean 生命周期2

    在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一个实例,而不是每次都产生一个新的对象使用Sin ...

  9. Spring bean 生命周期验证

    一.从源码注释看bean生命周期 从JDK源码上看,BeanFactory实现类需要支持Bean的完整生命周期,完整的初始化方法及其标准顺序(格式:接口 方法)为: 1.BeanNameAware s ...

随机推荐

  1. synchronized(this) 与synchronized(class) 之间的区别

    一.概念 synchronized 是 Java 中的关键字,是利用锁的机制来实现同步的. 锁机制有如下两种特性: 互斥性:即在同一时间只允许一个线程持有某个对象锁,通过这种特性来实现多线程中的协调机 ...

  2. 五种开源协议(GPL,LGPL,BSD,MIT,Apache)介绍

    商业化的软件应该主要选用MIT或者Apache license的开源系统作为插件. 什么是许可协议? 什么是许可,当你为你的产品签发许可,你是在出让自己的权利,不过,你仍然拥有版权和专利(如果申请了的 ...

  3. Spring Boot ConfigurationProperties validate

    使用@Query可以在自定义的查询方法上使用@Query来指定该方法要执行的查询语句,比如:@Query("select o from UserModel o where o.uuid=?1 ...

  4. 浅谈服务间通信【MQ在分布式系统中的使用场景】

    解决的问题 一项技术的产生必然是为了解决问题而生,了解了一项技术解决的问题,就能够很轻松的理解这项技术的设计根本,从而更好地理解与使用这项技术. 消息中间件和RPC从根本上来说都是为了解决分布式系统的 ...

  5. 玩转spring mvc(六)---自定义异常跳转页面

    本文主要是关于如何在出现异常 如404时,跳转到自定义的异常页面,当然这不是spring的知识,但可以整合进去. 在web.xml中新增如下代码,里边的路径可以根据实际情况进行修改 <!-- 7 ...

  6. PACKAGE-INFO.JAVA 作用及用法详解

    转自http://strong-life-126-com.iteye.com/blog/806246 及http://blog.sina.com.cn/s/blog_93dc666c0101gzlr. ...

  7. Java元编程及其应用

    首先,我们且不说元编程是什么,他能做什么.我们先来谈谈生产力. 同样是实现一个投票系统,一个是python程序员,基于django-framework,用了半小时就搭建了一个完整系统,另外一个是标准的 ...

  8. consoleWriter.go

    package blog4go import ( "fmt" "os" "time" ) // ConsoleWriter is a con ...

  9. monkey-----停止正在测试的monkey

    第一步:adb   shell ps | grep monkey:查找到正在测试的monkey包名   第二步:kill   pid:删除查找出的monkey进程   以上完美的停止monkey测试

  10. 分布式缓存技术redis学习系列

    分布式缓存技术redis学习系列(一)--redis简介以及linux上的安装以及操作redis问题整理 分布式缓存技术redis学习系列(二)--详细讲解redis数据结构(内存模型)以及常用命令 ...