Spring boot's properties property value configuration application.properties and custom properties

  1. The configuration property value
    application.properties file is directly configured:
    com.ieen.super.name="MDD"

    Custom properties file configuration: src/main/resources/conf/boot.properties

    com.ieen.boot.name="123"
    com.ieen.boot.value="456"
  2. Call the method
    @Value annotation to call the application.properties property value:
    @Value("${com.ieen.super.name}")
    private String name;

    The @Value annotation calls the custom properties property value:

    @PropertySource("classpath:conf/boot.properties")
    public class Main{
        @Value("${com.ieen.boot.name}")
        private String bootName;
    }
    Note: The @PropertySource annotation introduced custom properties since 1.5
    // value is custom configuration
    // ignoreResourceNotFound defaults to false, the file does not exist and an error is reported
    // encoding sets the encoding
    // name is the beanname of the Resource object
    @PropertySource(value = { "classpath:boot.properties",
            "classpath:conf/boot.properties" }, ignoreResourceNotFound = false, encoding = "UTF-8", name = "boot-custom.properties")

    @ConfigurationProperties configure the properties properties object:

    // Configuration before spring boot 1.5
    //@ConfigurationProperties(prefix = "com.ieen.boot",locations = "classpath:conf/boot.properties")
    @ConfigurationProperties(prefix = "com.ieen.boot") 
    // If custom properties are added @PropertySource(value = "classpath:conf/boot.properties", encoding = "UTF-8") @Component public class BootPropertiesBean { private String name; private String value; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
        @Autowired
        private BootPropertiesBean bean;

     

     

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325026168&siteId=291194637