Spring Boot不支持bootstrap命名的配置文件的加载

背景

之前搞Spring Cloud项目,为了使用Spring Cloud,工程级别的配置文件必须配置在bootstrap命名的配置文件中。
今天搞一个单独的Spring Boot工程,发现不识别bootstrap命名的配置文件。

解决

研究了一下原来Spring Boot本身并不支持该命名的配置文件的加载。该配置文件的加载是Spring Cloud完成的。
需要引入以下jar

<!--需要引入该jar才能使bootstrap配置文件生效--> 
<dependency> 
    <groupId>org.springframework.cloud</groupId> 
    <artifactId>spring-cloud-context</artifactId> 
</dependency>

加载该配置的源代码:

@Configuration
@EnableConfigurationProperties(PropertySourceBootstrapProperties.class)
public class PropertySourceBootstrapConfiguration implements
        ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {

    public static final String BOOTSTRAP_PROPERTY_SOURCE_NAME = BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME
            + "Properties";

    private static Log logger = LogFactory
            .getLog(PropertySourceBootstrapConfiguration.class);

    private int order = Ordered.HIGHEST_PRECEDENCE + 10;

    @Autowired(required = false)
    private List<PropertySourceLocator> propertySourceLocators = new ArrayList<>();

    @Override
    public int getOrder() {
        return this.order;
    }

    public void setPropertySourceLocators(
            Collection<PropertySourceLocator> propertySourceLocators) {
        this.propertySourceLocators = new ArrayList<>(propertySourceLocators);
    }

BOOTSTRAP_PROPERTY_SOURCE_NAME名字如下:

public class BootstrapApplicationListener
        implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {

    public static final String BOOTSTRAP_PROPERTY_SOURCE_NAME = "bootstrap";
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容