SpringBootspringboot工作总结

ApplicationContextInitializer接口

2017-07-24  本文已影响1770人  二月_春风

ApplicationContextInitializer接口是在spring容器刷新之前执行的一个回调函数。

发现这个接口是定义在org.springframework.context包下的。看看其javadoc:

Callback interface for initializing a Spring ConfigurableApplicationContext
prior to being ConfigurableApplicationContext#refresh() refreshed.
spring上下文初始化的回调函数在上下文(ConfigurableApplicationContext)刷新(refresh)之前调用。

Typically used within web applications that require some programmatic initialization of the application context. For example, registering property sources or activating profiles against the {@linkplain ConfigurableApplicationContext#getEnvironment() context's environment}. See ContextLoader and FrameworkServlet support for declaring a "contextInitializerClasses" context-param and init-param, respectively.
通常被用作web应用,在一些程序设计在spring容器初始化使用。比如说注册一些熟悉配置或者激活一些配置文件针对(ConfigurableApplicationContext的getEnvironment()方法)。参考ContextLoader和FrameworkServlet支持定义一个"contextInitializerClasses"上下文参数或者初始化参数。

ApplicationContextInitializer processors are encouraged to detect whether Spring's org.springframework.core.Ordered Ordered} interface has been implemented or if the org.springframework.core.annotation.Order Order annotation is present and to sort instances accordingly if so prior to invocation.
支持Order注解。并且代表着执行顺序。

demo
定义MyApplicationContextInitializer继承ApplicationContextInitializer接口

package com.zhihao.miao;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.Order;

@Order(2)
public class MyApplicationContextInitializer implements ApplicationContextInitializer{
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        System.out.println("bean count====="+applicationContext.getBeanDefinitionCount());
    }
}

定义另外一个MyApplicationContextInitializer2继承ApplicationContextInitializer接口:

package com.zhihao.miao;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.Order;

@Order(1)
public class MyApplicationContextInitializer2 implements ApplicationContextInitializer {

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        System.out.println("app name : " + applicationContext.getDisplayName());
    }
}

主类测试:

package com.zhihao.miao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.addInitializers(new MyApplicationContextInitializer());
        application.addInitializers(new MyApplicationContextInitializer2());
        ConfigurableApplicationContext context =application.run(args);
        context.close();
    }
}

执行顺序和@Order注解的顺序相反,Order的value值越小表示越早执行。

上面是关于自定义ApplicationContextInitializer的一种注册方法,还有二种注册方法,

第二种方式
在application.properties中定义:

context.initializer.classes=com.zhihao.miao.MyApplicationContextInitializer,com.zhihao.miao.MyApplicationContextInitializer2

主类测试:

package com.zhihao.miao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
       SpringApplication.run(Application.class,args);
    }
}

第三种方式
新建core-baen(名字自己定义)项目,新建一个MyApplicationContextInitializer实现ApplicationContextInitializer接口

package core.bean;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;

public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext>{

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        System.out.println("========initialize=======");
    }
}

然后在core-bean项目下的resources下新建META-INF文件夹,文件夹下新建spring.factories文件

org.springframework.context.ApplicationContextInitializer=core.bean.MyApplicationContextInitializer

在项目中引入

<dependency>
    <groupId>com.zhihao.miao</groupId>
    <artifactId>core-bean</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

启动类:

package com.zhihao.miao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
       SpringApplication.run(Application.class,args);
    }
}

启动启动类并执行:

总结

自定义ApplicationContextInitializer接口的方式:
1)写一个类,实现ApplicationContextInitializer接口
2)注册ApplicationContextInitializer

上一篇 下一篇

猜你喜欢

热点阅读