springboot源码之系统初始化器使用

系统初始化器介绍

 

1. 类名ApplicationContextInitializer

2. spring容器刷新之前执行的一个回调函数

3. 向springboot容器中注册属性

4. 继承接口自定义实现

 

springboot的初始化器有三种实现方式

第一种将写好的初始化器配置在spring.factories文件中,让springboot进行自动装配

package com.jun.lee.initializer;

import lombok.extern.slf4j.Slf4j;
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 org.springframework.stereotype.Component;

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

/**
 * 
 * 第一种实现方式
 * @author Jun Lee
 * @version 1.0
 * @createDate 2020/02/13 17:16
 * @see com.jun.lee.initializer.FistInitializer
 */
@Component
@Slf4j
@Order(1)
public class FistInitializer implements ApplicationContextInitializer {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        Map map = new HashMap();
        map.put("key1","value1");
        MapPropertySource source = new MapPropertySource("mapProperty",map);
        environment.getPropertySources().addLast(source);
        log.info("Run FistInitializer");
    }
}

 

第二种修改springboot启动类的代码,在启动类中手动添加初始化器

package com.jun.lee;

import com.jun.lee.initializer.SecondInitializer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

	public static void main(String[] args) {
		SpringApplication application = new SpringApplication(App.class);
		application.addInitializers(new SecondInitializer());
		application.run(args);
	}


}
package com.jun.lee.initializer;

import lombok.extern.slf4j.Slf4j;
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 org.springframework.stereotype.Component;

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

/**
 * 
 * 第二种实现方式
 * @author Jun Lee
 * @version 1.0
 * @createDate 2020/02/13 17:16
 * @see SecondInitializer
 */
@Component
@Slf4j
@Order(2)
public class SecondInitializer implements ApplicationContextInitializer {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        Map map = new HashMap();
        map.put("key2","value2");
        MapPropertySource source = new MapPropertySource("SecondInitializer",map);
        environment.getPropertySources().addLast(source);
        log.info("Run SecondInitializer");
    }
}

 

第三种在配置文件中添加配置

context.initializer.classes =com.jun.lee.initializer.ThirdInitializer
package com.jun.lee.initializer;

import lombok.extern.slf4j.Slf4j;
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 org.springframework.stereotype.Component;

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

/**
 * 
 * 第三种实现方式
 * @author Jun Lee
 * @version 1.0
 * @createDate 2020/02/13 17:16
 * @see ThirdInitializer
 */
@Component
@Slf4j
@Order(3)
public class ThirdInitializer implements ApplicationContextInitializer {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        Map map = new HashMap();
        map.put("key3","value3");
        MapPropertySource source = new MapPropertySource("ThirdInitializer",map);
        environment.getPropertySources().addLast(source);
        log.info("Run ThirdInitializer");
    }
}

 

然后我们启动项目得到下面的结果,打印了我们在初始化器中的输出

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.0.RELEASE)

2020-02-13 18:54:52.722  INFO 7128 --- [           main] c.jun.lee.initializer.ThirdInitializer   : Run ThirdInitializer
2020-02-13 18:54:52.726  INFO 7128 --- [           main] com.jun.lee.initializer.FistInitializer  : Run FistInitializer
2020-02-13 18:54:52.727  INFO 7128 --- [           main] c.jun.lee.initializer.SecondInitializer  : Run SecondInitializer
2020-02-13 18:54:52.739  INFO 7128 --- [           main] com.jun.lee.App                          : Starting App on 

 

我们写一个测试类来获取环境中的属性

package com.jun.lee.initializer;

import com.jun.lee.util.SpringContextHolder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * 
 *
 * @author Jun Lee
 * @version 1.0
 * @createDate 2020/02/13 18:44
 * @see com.jun.lee.initializer
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestInitializer {

    @Test
    public void test(){
        ApplicationContext applicationContext = SpringContextHolder.getApplicationContext();
        Environment environment = applicationContext.getEnvironment();
        String key1 = environment.getProperty("key1");
        String key2 = environment.getProperty("key2");
        String key3 = environment.getProperty("key3");
        System.out.println(key1);
        System.out.println(key2);
        System.out.println(key3);
    }
}

 

输出结果如下

value1
null
value3

有一个为null是因为测试类没有设置SecondInitializer 初始化器到环境中,所以为null

你可能感兴趣的:(springboot)