spring-boot ConfigurationProperties

添加生成元数据信息的依赖

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.1.6.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <version>2.1.6.RELEASE</version>
      <optional>true</optional>
    </dependency>

配置类

@ConfigurationProperties(prefix = "feilong.hello.format",ignoreUnknownFields = true)
public class HelloFormatProperties {

    private  int port;

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public final static String HELLO_FORMAT_PROPERTITIES = "feilong.hello.format";
    private Map<String, Object> info;

    public Map<String, Object> getInfo() {
        return info;
    }

    public void setInfo(Map<String, Object> info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "HelloFormatProperties{" +
                "info=" + info +
                '}';
    }
}

作为自动化配置项, [启动@EnableConfigurationProperties]

@Import(feilong.stater.autoconfiguration.FormatAutoConfiguration.class)
@EnableConfigurationProperties(feilong.stater.autoconfiguration.HelloFormatProperties.class)
@Configuration
public class HellowAutoConfiguration {

    @Bean(name="feilonghelloFormatTemplate")
    public feilong.stater.HelloFormatTemplate helloFormatTemplate(feilong.stater.autoconfiguration.HelloFormatProperties helloProperties, feilong.stater.format.IFormatProcessor formatProcessor){
        return new feilong.stater.HelloFormatTemplate(helloProperties,formatProcessor);
    }
}

 在resources/META-INF/spring.factories下配置自动注入类

org.springframework.boot.autoconfigure.EnableAutoConfiguration=  
  feilong.stater.autoconfiguration.HellowAutoConfiguration

  

最后一步打包成jar,放到私服上.供其他项目引用

注意,打包时会在spring.factories下生成元数据:spring-configuration-metadata.json

这样在其他springboot项目中,application.properties中便可配置.

原文地址:https://www.cnblogs.com/snow-man/p/11169777.html