需求:要将一个配置类的属性与配置文件中的一一对应,也就是说,拿配置文件中的配置给配置类的属性做初始化。

在 spring boot 1.5 以前的做法。

  1. 配置类:ScheduledTasksConfig.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package com.mycompany.restful;

    import org.springframework.boot.context.properties.ConfigurationProperties;

    @ConfigurationProperties(locations = "classpath:scheduled.properties")
    public class ScheduledTasksConfig
    {
    public static boolean isTopTmcMessageQueueShardingTableServiceProcess;

    public static boolean isIsTopTmcMessageQueueShardingTableServiceProcess()
    {
    return isTopTmcMessageQueueShardingTableServiceProcess;
    }

    public static void setIsTopTmcMessageQueueShardingTableServiceProcess(boolean isTopTmcMessageQueueShardingTableServiceProcess)
    {
    ScheduledTasksConfig.isTopTmcMessageQueueShardingTableServiceProcess = isTopTmcMessageQueueShardingTableServiceProcess;
    }
    }
  2. 配置文件:scheduled.properties

    1
    isTopTmcMessageQueueShardingTableServiceProcess=true
  3. spring boot 启动类添加注解:@EnableConfigurationProperties({ScheduledTasksConfig.class})

spring boot 1.5 以后的做法

spring boot 1.5 以后,上面的方式已经失效,而且启动过程没有任何错误信息,总之是没有初始化成功。

  1. 配置类,与之前的相比,仅是注解部分不一样。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    package com.mycompany.restful;

    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;

    @Component
    @ConfigurationProperties
    @PropertySource("classpath:scheduled.properties")
    public class ScheduledTasksConfig
    {
    public static boolean isTopTmcMessageQueueShardingTableServiceProcess;

    public static boolean isIsTopTmcMessageQueueShardingTableServiceProcess()
    {
    return isTopTmcMessageQueueShardingTableServiceProcess;
    }

    public static void setIsTopTmcMessageQueueShardingTableServiceProcess(boolean isTopTmcMessageQueueShardingTableServiceProcess)
    {
    ScheduledTasksConfig.isTopTmcMessageQueueShardingTableServiceProcess = isTopTmcMessageQueueShardingTableServiceProcess;
    }
    }
  2. 配置文件与之前的完全一样。

  3. spring boot 启动类,去掉 1.5 以前版本的注解:@EnableConfigurationProperties({ScheduledTasksConfig.class}),注意,是不要这个注解,否则会报错。
  4. 在 mvcConfig.xml 中添加 component-scan:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <mvc:annotation-driven/>
    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="crm.task.sdk.common"/>
    </beans>
  5. 在 spring boot 启动类添加注解:@ImportResource(locations = {“classpath*:mvcConfig.xml”})

参考

@ConfigurationProperties locations #6726