(1)-springboot2-ApplicationContextInitializer系统初始化器3种实现

第一种实现方式 META-INF/spring.factories

step1
: 实现 ApplicationContextInitializer
接口

step2
: resources资源文件目录下创建: META-INF/spring.factories
, 配置keyvalue分别为: 接口类=实现类

实例:

(1). 实现 ApplicationContextInitializer

package com.niewj.fileman.initialized;

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;

/**
 * @Author weijun.nie
 * @Date 2020/4/27 8:00
 * @Version 1.0
 */
@Order(1)
public class FirstACInitializer implements ApplicationContextInitializer {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        Map<String,Object> map = new HashMap<>();
        map.put("k1", "keyfirst");

        MapPropertySource firstInitializer = new MapPropertySource("firstInitializer", map);
        environment.getPropertySources().addLast(firstInitializer);
        System.out.println("......FirstInitializer[ApplicationContextInitializer]......");
    }
}

(2). 配置(创建文件): spring.factories
: src/main/resources/META-INF/spring.factories

org.springframework.context.ApplicationContextInitializer=com.niewj.fileman.initialized.FirstACInitializer

提供一个service中提供获取方法:

package com.niewj.fileman.service;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Author weijun.nie
 * @Date 2020/4/27 7:50
 * @Version 1.0
 */
@Component
public class ACInitializerService implements ApplicationContextAware {

    private ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public String findKey(String key){
        return applicationContext.getEnvironment().getProperty(key);
    }
}

控制器:

package com.niewj.fileman.controller;

import com.niewj.fileman.service.ACInitializerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/acInitializer")
public class ACInitializerController {

    @Autowired
    private ACInitializerService ACInitializerService;

    @RequestMapping("/props/{key}")
    @ResponseBody
    public String getByKey(@PathVariable("key") String key) {
        return ACInitializerService.findKey(key);
    }
}

第二种实现方式 自定义 SpringApplication
加初始化器:

step1
: 实现 ApplicationContextInitializer
接口

step2
: main方法中 自己实例化 SpringApplication 设置初始化器

step1: 实现:

package com.niewj.fileman.initialized;

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;

/**
 * @Author weijun.nie
 * @Date 2020/4/27 8:00
 * @Version 1.0
 */
@Order(2)
public class SecondACInitializer implements ApplicationContextInitializer {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        // 1. 获取到 Environment
        ConfigurableEnvironment environment = applicationContext.getEnvironment();

        // PropertySource: MapPropertySource
        Map<String, Object> map = new HashMap<>();
        map.put("2key1", "2value1");
        MapPropertySource mapPropSource = new MapPropertySource("SecondACInitializer-map", map);

        // 2. 调用 environment获取属性源(PropertySources),
        //  将 map 的PropertySource 加入 environment:
        environment.getPropertySources().addLast(mapPropSource);

        System.out.println("......SecondACInitializer[ApplicationContextInitializer]......");
    }
}

step2: main方法:

package com.niewj.fileman;

import com.niewj.fileman.initialized.SecondACInitializer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Sb2Application {

    public static void main(String[] args) {
//        SpringApplication.run(FilemanApplication.class, args);

        SpringApplication application = new SpringApplication(Sb2Application.class);
        application.addInitializers(new SecondACInitializer());
        application.run(args);
    }
}

控制台输出:

……FirstInitializer[ApplicationContextInitializer]……

浏览器访问: http://localhost
:9999/acInitializer/props/2key1

2value1

第三种实现方式 在 application.properties
中配置初始化器:

step1
: 实现 ApplicationContextInitializer
接口

step2
: 在application.properties中配置 context.initializer.classes
属性设置初始化器

package com.niewj.fileman.initialized;

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;

/**
 * @Author weijun.nie
 * @Date 2020/4/27 8:00
 * @Version 1.0
 */
@Order(3)
public class ThirdACInitializer implements ApplicationContextInitializer {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        // 1. 获取到 Environment
        ConfigurableEnvironment environment = applicationContext.getEnvironment();

        // PropertySource: MapPropertySource
        Map<String, Object> map = new HashMap<>();
        map.put("3key1", "3value1");
        MapPropertySource mapPropSource = new MapPropertySource("ThirdACInitializer-map", map);

        // 2. 调用 environment获取属性源(PropertySources),
        //  将 map 的PropertySource 加入 environment:
        environment.getPropertySources().addLast(mapPropSource);

        System.out.println("......ThirdACInitializer[ApplicationContextInitializer]......");
    }
}

控制台输出:

……ThirdACInitializer[ApplicationContextInitializer]……

……FirstInitializer[ApplicationContextInitializer]……

……SecondACInitializer[ApplicationContextInitializer]……

浏览器访问: http://localhost
:9999/acInitializer/props/3key1:

3value1

小结(1): 设置初始化器的3种方法:

首先, 都要实现 ApplicationContextInitializer接口:

方法1:

在src/main/resources下创建 META-INF/spring.factories, 然后配置属性 org.springframework.context.ApplicationContextInitializer=com.niewj.fileman.initialized.FirstACInitializer

方法2:

在main方法中替换 SpringApplication.run(args)
为:

SpringApplication application = new SpringApplication(App.class); 
application.addInitializers(new SecondACInitializer());
application.run(args);

方法3:

application.properties
中配置

context.initializer.classes=context.initializer.classes=com.niewj.fileman.initialized.ThirdACInitializer

小结(2): @Order(x):

Order对application.properties中的配置无感: application.properties中的会优先加载;

然后另外两种方式设置的, x 值小的先加载
!

原文 

https://segmentfault.com/a/1190000022480847

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » (1)-springboot2-ApplicationContextInitializer系统初始化器3种实现

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址