自定义系统初始化器

import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;

import java.util.HashMap;
import java.util.Map;

/**
 * 初始化器
 * @see SpringApplication#applyInitializers(ConfigurableApplicationContext)
 */
@Order
public class FirstInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        Map<String,Object> map = new HashMap<>();
        map.put("key1","value1");
        MapPropertySource mapPropertySource = new MapPropertySource("firstInitializer", map);
        environment.getPropertySources().addLast(mapPropertySource);
        System.out.println("run firstInitializer");
    }
}

@SpringBootApplication
public class SpringBootDemoApplication {

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(SpringBootDemoApplication.class);
        //手动添加初始化器
        springApplication.addInitializers(new SecondInitializer());
        springApplication.run(args);
    }

}

application.properties

# 自定义初始化器,这种方式优先其他方式加载 具体查看org.springframework.boot.context.config.DelegatingApplicationContextInitializer
# 会被DelegatingApplicationContextInitializer发现注册
context.initializer.classes=cn.springbootdemo.initializer.ThirdInitializer

META-INF/spring.factories

# Initializers 会被SpringFactoriesLoader发现注册,推荐使用这种
org.springframework.context.ApplicationContextInitializer=\
cn.springbootdemo.initializer.FirstInitializer

测试

@Component
public class DemoService implements ApplicationContextAware {
    @Autowired
    ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public String getEnv(String key){
        Environment environment = applicationContext.getEnvironment();
        return environment.getProperty(key);
    }
}
@RestController
@RequestMapping("/demo")
public class DemoController {
    @Autowired
    private DemoService demoService;

    @GetMapping("/env/{key}")
    public String getKey(@PathVariable("key") String key){
        return Optional.ofNullable(demoService.getEnv(key)).orElse("null");
    }
}
/**
 * Callback interface for initializing a Spring {@link ConfigurableApplicationContext}
 * prior to being {@linkplain ConfigurableApplicationContext#refresh() refreshed}. //回调接口在 refreshed之前
 *
 * <p>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 {@code ContextLoader} and {@code FrameworkServlet} support
 * for declaring a "contextInitializerClasses" context-param and init-param, respectively.
 *
 * <p>{@code ApplicationContextInitializer} processors are encouraged to detect          //order排序
 * whether Spring's {@link org.springframework.core.Ordered Ordered} interface has been
 * implemented or if the @{@link org.springframework.core.annotation.Order Order}
 * annotation is present and to sort instances accordingly if so prior to invocation.
 */
public interface ApplicationContextInitializer<C extends ConfigurableApplicationContext> {

猜你喜欢

转载自www.cnblogs.com/fly-book/p/12686313.html