zoukankan      html  css  js  c++  java
  • 写一个完整的Spring生命周期例子

    一共三个类:

    @不能被Spring管理,因为需要通过@Bean的方法加上initMethod和destroyMethod
    public
    class Car implements InitializingBean, DisposableBean { public Car() { System.out.println("构造函数"); } @PreDestroy public void aa() { System.out.println("@PreDestroy"); } @PostConstruct public void bb() { System.out.println("@PostConstruct"); } @Override public void destroy() throws Exception { System.out.println("重写的DisposableBean方法"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("重写的InitializingBean方法"); } public void initMethod() { System.out.println("initMethod"); } public void destroyMethod() { System.out.println("destroyMethod"); } }
    @Component
    public class MainConfigOfLifeCycle {
    
        @Bean(initMethod = "initMethod", destroyMethod = "destroyMethod")
        public Car car() {
            return  new Car();
        }
    }
    @Component
    public class MyBeanPostProcessor implements BeanPostProcessor {
    
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("postProcessBeforeInitialization  " + bean + "  " + beanName);
    #这里return bean和return null是有区别的
    return bean;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("postProcessAfterInitialization  " + bean + "  " + beanName);
            return bean;
        }
    }

    结果如下:

    构造函数
    postProcessBeforeInitialization  com.config.Car@3ad847f8  car
    @PostConstruct  #@PostConstruct:postProcessBeforeInitializatin中return 了bean则有这个输入,但如果return null则没有这个输出
    重写的InitializingBean方法
    initMethod
    postProcessAfterInitialization  com.config.Car@3ad847f8  car
    @PreDestroy
    重写的DisposableBean方法
    destroyMethod
  • 相关阅读:
    数据结构——算法之(029)( 字符串原地压缩)
    hihoCoder #1174:拓扑排序·一
    POJ 3026 Borg Maze
    Remove Duplicates from Sorted List II--LeetCode
    mach-o格式分析
    otool -l 可执行文件结构
    mach-o可执行文件结果
    ios 编译版本 最低版本 运行版本 动态链接库
    关于__IPHONE_OS_VERSION_MAX_ALLOWED和__IPHONE_OS_VERSION_MIN_ALLOWED的用法
    OO真经——关于面向对象的哲学体系及科学体系的探讨(下)
  • 原文地址:https://www.cnblogs.com/woyujiezhen/p/13789907.html
Copyright © 2011-2022 走看看