[Spring]初始化服务的几种实现方式

package com.github.java.learning.spring;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;

/**
 * created by guanjian on 2020/11/17 9:22
 */
@Component("initListener")
public class InitListener implements ApplicationContextAware, ServletContextAware, BeanFactoryPostProcessor, BeanPostProcessor,
        InitializingBean, ApplicationListener<ContextRefreshedEvent> {

    private final static Logger LOGGER = LoggerFactory.getLogger(InitListener.class);

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        LOGGER.info("BeanFactoryPostProcessor====postProcessBeanFactory");
    }

    @Override
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        LOGGER.info("BeanPostProcessor====postProcessBeforeInitialization");
        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
        LOGGER.info("BeanPostProcessor====postProcessAfterInitialization");
        return null;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        LOGGER.info("ApplicationContextAware====setApplicationContext");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        LOGGER.info("InitializingBean====afterPropertiesSet");
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        LOGGER.info("ApplicationListener====onApplicationEvent");
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        LOGGER.info("ServletContextAware====setServletContext");
    }
}

执行顺序如下:

  • [main] INFO com.github.java.learning.spring.InitListener - ApplicationContextAware====setApplicationContext
  • [main] INFO com.github.java.learning.spring.InitListener - InitializingBean====afterPropertiesSet
  • [main] INFO com.github.java.learning.spring.InitListener - BeanFactoryPostProcessor====postProcessBeanFactory
  • [main] INFO com.github.java.learning.spring.InitListener - BeanPostProcessor====postProcessBeforeInitialization
  • [main] INFO com.github.java.learning.spring.InitListener - BeanPostProcessor====postProcessBeforeInitialization
  • [main] INFO com.github.java.learning.spring.InitListener - BeanPostProcessor====postProcessBeforeInitialization
  • [main] INFO com.github.java.learning.spring.InitListener - ApplicationListener====onApplicationEvent
  • [main] INFO com.github.java.learning.spring.InitListener - BeanPostProcessor====postProcessBeforeInitialization
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大摩羯先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值