SpringBoot configuration file

Configuration File Loading

SpringBoot loads the configuration file application.properties (or application.yml) by default from the following locations:

  1. A /config subdirectory of the current directory
  2. The current directory
  3. A classpath /config package
  4. The classpath root

If the name of the configuration file is not the default, you can specify the configuration name through the spring.config.name attribute

If the path of the configuration file is not the default path, it can be specified by the spring.config.location attribute

profile

See: Profile Multi-environment Running

List of supported attributes

See: List of official configuration attributes

Use placeholders with maven

Attributes are automatically expanded at build time and can be automatically extended using existing build configurations rather than hard-coded attributes specified in the project's build configuration.
You can use resource filtering to automatically extend attributes from Maven projects. If you use spring-boot-starter-parent, you can use the @. @ placeholder to refer to Maven's Project Properties, as shown in the following example:

profile runs in multiple environments

spring.profiles.active=@profileName@

If you do not use the startup parent node, you need to include the following elements in the pom.xml element:

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <!-- 
            	Here you can define multiple parameters, and the name of the tag corresponds. properties In the document key
                For example:<profileName>Write in the configuration file@profileName@
             -->
            <profileName>dev</profileName>
        </properties>
    </profile>
</profiles>

You also need to include the following elements in it:

<!-- 
	maven resource Plug-in unit:
    Function: Mainly related to operating configuration files.
    For example, use maven Replace placeholders in configuration files with variables in the project
    Display specified non maven standard resources The directory is resources
 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.7</version>
    <configuration>
        <!-- The placeholder in the specified configuration file is@ -->
        <delimiters>
            <delimiter>@</delimiter>
        </delimiters>
        <useDefaultDelimiters>false</useDefaultDelimiters>
    </configuration>
</plugin>

Type-safe Properties read

When there are many parameter values in the system, we tend to categorize the configuration parameters, and then encapsulate the read values directly into a JavaBean via the @Configuration Properties annotation

Examples:

New config.properties under src/main/resources

demo.phone=10086
demo.wife=self

Create the ConfigBeanProp class and inject the values in config.properties:

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

@Component
@ConfigurationProperties(prefix = "demo")
@PropertySource(value = "config.properties")
public class ConfigBeanProp {

    private String phone;

    private String wife;

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }
}

@ Value annotation reads application.properties parameters

configuration file

application.properties

com.name="Zhang San"
com.age=23
bean class

People.java

/**
 * Read custom properties in application.properties
 */
@Component
public class People {
    @Value("${com.name}")
    private String name;
    @Value("${com.age}")
    private int age;

	public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}
Test class

TestRead.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRead {
    @Autowired
    private People people;

    @Test
    public void testReadConf() {
		System.out.println("Full name:" + people.getName());
		System.out.println("Age:" + people.getAge());
    }
}

Keywords: Maven Spring Attribute Java

Added by Vaclavious on Tue, 23 Jul 2019 12:25:47 +0300