spring bean 生命周期事件

整体流程图

spring bean 生命周期事件

程序示例

maven依赖

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.12.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

</dependencies>

配置类

@Configuration
@ComponentScan("per.ym.processor, per.ym.service")
public class LifeCycleConfig {

    @Bean(initMethod = "init", destroyMethod = "destroy")
    public LifeCycleBean lifeCycleBean() {
        return new LifeCycleBean();
    }
}

实例bean

public class LifeCycleBean implements InitializingBean, BeanNameAware, BeanFactoryAware, BeanClassLoaderAware {

    public LifeCycleBean() {
        System.out.println("consturctor......");
    }

    public void init() {
        System.out.println("init......");
    }

    @Autowired
    public void setService(PersonService service) {
        System.out.println("setService......");
    }

    public void destroy() {
        System.out.println("destroy......");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet......");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("setBeanFactory......");
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("setBeanName......");
    }

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        System.out.println("setBeanClassLoader......");
    }

}

InstantiationAwareBeanPostProcessor

package per.ym.processor;

@Component
public class MyInstantiationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {

    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if (beanName.equals("lifeCycleBean")) {
            System.out.println("postProcessBeforeInstantiation......");
        }
        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if (beanName.equals("lifeCycleBean")) {
            System.out.println("postProcessAfterInstantiation......");
        }
        return true;
    }

    @Override
    public PropertyValues postProcessPropertyValues(
            PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
        if (beanName.equals("lifeCycleBean")) {
            System.out.println("postProcessPropertyValues......");
        }
        return pvs;
    }

}

BeanPostProcessor

package per.ym.processor;

@Component
public class MyPostProcess implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (beanName.equals("lifeCycleBean")) {
            System.out.println("postProcessBeforeInitialization......");
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (beanName.equals("lifeCycleBean")) {
            System.out.println("postProcessAfterInitialization......");
        }
        return bean;
    }

}

service

package per.ym.service;

@Service
public class Service {
}

测试类

public class Test_Normal {

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(LifeCycleConfig.class);

    @Test
    public void test() {
        applicationContext.getBean("lifeCycleBean");
        applicationContext.close();
    }
}

测试结果:

postProcessBeforeInstantiation......
consturctor......
postProcessAfterInstantiation......
postProcessPropertyValues......
setService......
setBeanName......
setBeanClassLoader......
setBeanFactory......
postProcessBeforeInitialization......
afterPropertiesSet......
init......
postProcessAfterInitialization......
destroy......

转载于:https://blog.51cto.com/9587581/2401791

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值