Read the application file

Add to application.yml or properties file:

info.address=USA\

info.company=Spring\

info.degree=high

@value How to read an annotation

import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class InfoConfig1 { @Value("${info.address}") private String address; @Value("${info.company}") private String company; @Value("${info.degree}") private String degree; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public String getDegree() { return degree; } public void setDegree(String degree) { this.degree = degree; }}Copy the code

ConfigurationProperties How the annotation is read

@Component @ConfigurationProperties(prefix = "info") public class InfoConfig2 { private String address; private String company; private String degree; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public String getDegree() { return degree; } public void setDegree(String degree) { this.degree = degree; }}Copy the code

Read the specified file

Config /db-config.properties

db.username=root\

db.password=123456

@propertysource +@Value How an annotation is read

@Component @PropertySource(value = { "config/db-config.properties" }) public class DBConfig1 { @Value("${db.username}") private String username; @Value("${db.password}") private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}Copy the code

Note: @propertysource does not support yML file reading.

@propertysource +@ConfigurationProperties How annotations are read

@Component @ConfigurationProperties(prefix = "db") @PropertySource(value = { "config/db-config.properties" }) public class DBConfig2 { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}Copy the code

Environment read mode

All of the above loaded configurations are available via Environment injection.

@Autowired private Environment env; // Get the argument String getProperty(String key);Copy the code

conclusion

From the examples above, Spring Boot can bind variables via @propertysource,@Value,@Environment, and @ConfigurationProperties.

Recommended reading

Dry goods: 2TB architect four-stage video tutorial

Interview: the most complete Java multithreaded interview questions and answers

Interview: the most comprehensive ali advanced Java interview questions in history

Interview: The most complete Spring interview questions in history

Tutorial: The most complete Spring Boot complete video tutorial

Books: 15 must-read books for advanced Java architects

Tools: Recommended an online creation flow chart, mind mapping software

Share Java dry goods, high concurrency programming, hot technology tutorials, microservices and distributed technology, architecture design, blockchain technology, artificial intelligence, big data, Java interview questions, and cutting-edge hot news.