栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring bean 初始化顺序

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring bean 初始化顺序

package org.example.spring.cycle.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.InitBinder;

import javax.annotation.PostConstruct;


//@Component
public class BeanInit implements
        BeanNameAware,
        BeanClassLoaderAware,
        EnvironmentAware,
        ApplicationContextAware,
        BeanPostProcessor,
        InitializingBean,
        DisposableBean {

    private ExampleInit exampleInit;
    private ApplicationContext applicationContext;
    private Environment environment;

    // 第一步 - 第二步
    public BeanInit() {
        System.out.println("第一步 - 第二步 -- BeanInit -- 构造函数");
    }

    // 第三步
    // 需要先初始化 ExampleInit 实例
    @Autowired
    public void setExampleInit(ExampleInit exampleInit) {
        System.out.println("第三步 -- BeanInit -- setExampleInit -- " + exampleInit.getBeanName());
        this.exampleInit = exampleInit;
    }

    // 第五步
    @PostConstruct
    public void init() {
        System.out.println("第五步 -- BeanInit -- init -- @PostConstruct");
    }

    public void destroy() throws Exception {
        System.out.println("BeanInit -- destroy -- DisposableBean");
    }

    // 第六步
    public void afterPropertiesSet() throws Exception {
        System.out.println("第六步 -- BeanInit -- afterPropertiesSet -- InitializingBean");
        // applicationContext.getBean(ExampleInit.class)
        // 如果容器中没有ExampleInit实例的话 先初始化ExampleInit实例 在返回实例 (有则直接返回实例)
        ExampleInit exampleInit = this.applicationContext.getBean(ExampleInit.class);
        System.out.println("第六步 -- BeanInit -- afterPropertiesSet -- InitializingBean -- " + exampleInit.getBeanName());
    }

    // 第四步
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("第四步 -- BeanInit -- setApplicationContext -- ApplicationContextAware");
        this.applicationContext = applicationContext;
    }

    // 第四步
    public void setEnvironment(Environment environment) {
        System.out.println("第四步 -- BeanInit -- setEnvironment -- EnvironmentAware");
        this.environment = environment;
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("第四步 -- BeanInit -- setBeanName -- BeanNameAware -- " + name);
    }

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        System.out.println("第四步 -- BeanInit -- setBeanClassLoader -- BeanClassLoaderAware -- " + classLoader);
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @InitBinder
    public void start(){
        System.out.println("BeanInit -- start -- 自定义方法");
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/820349.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号