补习系列(21)-SpringBoot初始化之7招式

背景

在平常开发时,咱们经常须要 在SpringBoot 应用启动时执行某一段逻辑,以下面的场景:java

  • 获取一些当前环境的配置或变量
  • 向数据库写入一些初始数据
  • 链接某些第三方系统,确认对方能够工做..

在实现这些功能时,咱们可能会遇到一些"坑"。 为了利用SpringBoot框架的便利性,咱们不得不将整个应用的执行控制权交给容器,因而形成了你们对于细节是一无所知的。
那么在实现初始化逻辑代码时就须要当心了,好比,咱们并不能简单的将初始化逻辑在Bean类的构造方法中实现,相似下面的代码:spring

@Component
public class InvalidInitExampleBean {
 
    @Autowired
    private Environment env;
 
    public InvalidInitExampleBean() {
        env.getActiveProfiles();
    }
}

这里,咱们在InvalidInitExampleBean的构造方法中试图访问一个自动注入的env字段,当真正执行时,你必定会获得一个空指针异常(NullPointerException)。
缘由在于,当构造方法被调用时,Spring上下文中的Environment这个Bean极可能尚未被实例化,同时也仍未注入到当前对象,因此并不能这样进行调用。数据库

下面,咱们来看看在SpringBoot中实现"安全初始化"的一些方法:安全

一、 @PostConstruct 注解

@PostConstruct 注解实际上是来自于 javax的扩展包中(大多数人的印象中是来自于Spring框架),它的做用在于声明一个Bean对象初始化完成后执行的方法
来看看它的原始定义:springboot

The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.

也就是说,该方法会在全部依赖字段注入后才执行,固然这一动做也是由Spring框架执行的。app

下面的代码演示了使用@PostConstruct的例子:框架

@Component
public class PostConstructExampleBean {
 
    private static final Logger LOG 
      = Logger.getLogger(PostConstructExampleBean.class);
 
    @Autowired
    private Environment environment;
 
    @PostConstruct
    public void init() {
        //environment 已经注入
        LOG.info(Arrays.asList(environment.getDefaultProfiles()));
    }
}

二、 InitializingBean 接口

InitializingBean 是由Spring框架提供的接口,其与@PostConstruct注解的工做原理很是相似。
若是不使用注解的话,你须要让Bean实例继承 InitializingBean接口,并实现afterPropertiesSet()这个方法。ide

下面的代码,展现了这种用法:post

@Component
public class InitializingBeanExampleBean implements InitializingBean {
 
    private static final Logger LOG 
      = Logger.getLogger(InitializingBeanExampleBean.class);
 
    @Autowired
    private Environment environment;
 
    @Override
    public void afterPropertiesSet() throws Exception {
        //environment 已经注入
        LOG.info(Arrays.asList(environment.getDefaultProfiles()));
    }
}

三、 @Bean initMethod方法

咱们在声明一个Bean的时候,能够同时指定一个initMethod属性,该属性会指向Bean的一个方法,表示在初始化后执行。

以下所示:

@Bean(initMethod="init")
public InitMethodExampleBean exBean() {
    return new InitMethodExampleBean();
}

而后,这里将initMethod指向init方法,相应的咱们也须要在Bean中实现这个方法:

public class InitMethodExampleBean {
 
    private static final Logger LOG = Logger.getLogger(InitMethodExampleBean.class);
 
    @Autowired
    private Environment environment;
 
    public void init() {
        LOG.info(Arrays.asList(environment.getDefaultProfiles()));
    }
}

上面的代码是基于Java注解的方式,使用Xml配置也能够达到一样的效果:

<bean id="initMethodExampleBean"
  class="org.baeldung.startup.InitMethodExampleBean"
  init-method="init">
</bean>

该方式在早期的 Spring版本中大量被使用

四、 构造器注入

若是依赖的字段在Bean的构造方法中声明,那么Spring框架会先实例这些字段对应的Bean,再调用当前的构造方法。
此时,构造方法中的一些操做也是安全的,以下:

@Component
public class LogicInConstructorExampleBean {
 
    private static final Logger LOG 
      = Logger.getLogger(LogicInConstructorExampleBean.class);
 
    private final Environment environment;
 
    @Autowired
    public LogicInConstructorExampleBean(Environment environment) {
        //environment实例已初始化
        this.environment = environment;
        LOG.info(Arrays.asList(environment.getDefaultProfiles()));
    }
}

五、 ApplicationListener

ApplicationListener 是由 spring-context组件提供的一个接口,主要是用来监听 "容器上下文的生命周期事件"。
它的定义以下:

public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
    /**
     * Handle an application event.
     * @param event the event to respond to
     */
    void onApplicationEvent(E event);
}

这里的event能够是任何一个继承于ApplicationEvent的事件对象。 对于初始化工做来讲,咱们能够经过监听ContextRefreshedEvent这个事件来捕捉上下文初始化的时机。
以下面的代码:

@Component
public class StartupApplicationListenerExample implements
  ApplicationListener<ContextRefreshedEvent> {
 
    private static final Logger LOG 
      = Logger.getLogger(StartupApplicationListenerExample.class);
 
    public static int counter;
 
    @Override public void onApplicationEvent(ContextRefreshedEvent event) {
        LOG.info("Increment counter");
        counter++;
    }
}

在Spring上下文初始化完成后,这里定义的方法将会被执行。
与前面的InitializingBean不一样的是,经过ApplicationListener监听的方式是全局性的,也就是当全部的Bean都初始化完成后才会执行方法。
Spring 4.2 以后引入了新的 @EventListener注解,能够实现一样的效果:

@Component
public class EventListenerExampleBean {
 
    private static final Logger LOG 
      = Logger.getLogger(EventListenerExampleBean.class);
 
    public static int counter;
 
    @EventListener
    public void onApplicationEvent(ContextRefreshedEvent event) {
        LOG.info("Increment counter");
        counter++;
    }
}

六、 CommandLineRunner

SpringBoot 提供了一个CommanLineRunner接口,用来实如今应用启动后的逻辑控制,其定义以下:

public interface CommandLineRunner {

    /**
     * Callback used to run the bean.
     * @param args incoming main method arguments
     * @throws Exception on error
     */
    void run(String... args) throws Exception;

}

这里的run方法会在Spring 上下文初始化完成后执行,同时会传入应用的启动参数。
以下面的代码:

@Component
public class CommandLineAppStartupRunner implements CommandLineRunner {
    private static final Logger LOG =
      LoggerFactory.getLogger(CommandLineAppStartupRunner.class);
 
    public static int counter;
 
    @Override
    public void run(String...args) throws Exception {
        //上下文已初始化完成
        LOG.info("Increment counter");
        counter++;
    }
}

此外,对于多个CommandLineRunner的状况下可使用@Order注解来控制它们的顺序。

七、 ApplicationRunner

与 CommandLineRunner接口相似, Spring boot 还提供另外一个ApplicationRunner 接口来实现初始化逻辑。
不一样的地方在于 ApplicationRunner.run()方法接受的是封装好的ApplicationArguments参数对象,而不是简单的字符串参数。

@Component
public class AppStartupRunner implements ApplicationRunner {
    private static final Logger LOG =
      LoggerFactory.getLogger(AppStartupRunner.class);
 
    public static int counter;
 
    @Override
    public void run(ApplicationArguments args) throws Exception {
        LOG.info("Application started with option names : {}", 
          args.getOptionNames());
        LOG.info("Increment counter");
        counter++;
    }
}

ApplicationArguments对象提供了一些很是方便的方法,能够用来直接获取解析后的参数,好比:

java -jar application.jar --debug --ip=xxxx

此时经过 ApplicationArguments的getOptionNames就会获得["debug","ip"]这样的值

测试代码

下面,经过一个小测试来演示几种初始化方法的执行次序,按以下代码实现一个复合式的Bean:

@Component
@Scope(value = "prototype")
public class AllStrategiesExampleBean implements InitializingBean {
 
    private static final Logger LOG 
      = Logger.getLogger(AllStrategiesExampleBean.class);
 
    public AllStrategiesExampleBean() {
        LOG.info("Constructor");
    }
 
    @Override
    public void afterPropertiesSet() throws Exception {
        LOG.info("InitializingBean");
    }
 
    @PostConstruct
    public void postConstruct() {
        LOG.info("PostConstruct");
    }
 
    //在XML中定义为initMethod
    public void init() {
        LOG.info("init-method");
    }
}

执行这个Bean的初始化,会发现日志输出以下:

[main] INFO o.b.startup.AllStrategiesExampleBean - Constructor
[main] INFO o.b.startup.AllStrategiesExampleBean - PostConstruct
[main] INFO o.b.startup.AllStrategiesExampleBean - InitializingBean
[main] INFO o.b.startup.AllStrategiesExampleBean - init-method

因此,这几种初始化的顺序为:

  1. 构造器方法
  2. @PostConstruct 注解方法
  3. InitializingBean的afterPropertiesSet()
  4. Bean定义的initMethod属性方法

参考文档

https://www.baeldung.com/running-setup-logic-on-startup-in-spring

美码师的 SpringBoot 补习系列

相关文章
相关标签/搜索