Development environment: IntelliJ IDEA 2019.2.2
Spring Boot version: 2.1.8
Create a new Spring Boot project named demo.
One, the default configuration file
Spring Boot will read the name application.properties( yml) configuration file.
If there are multiple files with the same name, by default, they are read in the following order:
(1)The config directory of the project root directory
(2)The project root directory
(3)Under the project classpath
(4)Project classpath root directory
If the same configuration item appears in multiple configuration files, the value read later will not overwrite the previous one.
Test:
Create application.properties in each of the four locations of the project, with the content as follows:
(1)config/application.properties
test = config/application.properties
test1 = test1
(2)application.properties
test = application.properties
test2 = test2
(3)src/main/resources/config/application.properties
test = src /main/resources/config/application.properties
test3 = test3
(4)src/main/resources/application.properties
test = src/main /resources/application.properties
test4 = test4
Modify the default startup class DemoApplication.cs code:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Autowired
private Environment env;
@RequestMapping("/")
public String getProp(){
String test = env.getProperty("test");
String test1 = env.getProperty("test1");
String test2 = env.getProperty("test2");
String test3 = env.getProperty("test3");
String test4 = env.getProperty("test4");
return test + "," + test1 + "," + test2 + "," + test3 + "," +< span style="color: #000000;"> test4;
}
}
Visit http://localhost:8080/
Output: config/application.properties,test1,test2,test3,test4
Two. Specify the configuration file
Read the specified configuration file, do not use the default Application.properties.
Test:
(1)src/main/resources/application.properties Content:
test1 = application.properties
(2) Create a new directory config in the project’s src/main/resources, create a new configuration file myConfig.properties, content:
test2= myConfig. properties
Modify default The generated startup class DemoApplication.cs code:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
//SpringApplication.run(DemoApplication.class, args);< /span>
new SpringApplicationBuilder(DemoApplication.class).properties(
"spring.config.location=classpath:/config/myConfig.properties"
).run(args);
}
@Autowired
private Environment env;
@RequestMapping("/")
public String getProp(){
String test1 = env.getProperty("test1");
String test2 = env.getProperty("test2");
return test1 + "," + test2;
}
}
Visit http://localhost:8080/
Output: null,myConfig.properties
It can be seen that application.properties has not been read To, successfully read the configuration file myConfig.properties.
You can also use spring.config.name to specify the name of the configuration file. For example, if the following code specifies myConfig, Spring Boot will look for myConfig.properties(yml) under the classpath.
public static < span style="color: #0000ff;">void main(String[] args) {
//SpringApplication.run(DemoApplication.class, args);< /span>
/*new SpringApplicationBuilder(DemoApplication.class).properties(
"spring.config.location=classpath:/config/myConfig.properties"
).run(args);*/
new SpringApplicationBuilder(DemoApplication.class).properties(
"spring.config.name=myConfig").run(args);
}
Three, use profile to specify configuration
Use profile to activate different configurations according to a specific environment.
The content of src/main/resources/application.yml is as follows:
spring:
profiles: mysql
jdbc:
driver:
com.mysql.jdbc.Driver
---
spring:
profiles: oracle
jdbc:
driver:
oracle.jdbc.driver.OracleDriver
Modify the default generated startup class DemoApplication.cs code:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Scanner;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
//SpringApplication.run(DemoApplication.class, args);< /span>
Scanner scan = new Scanner(System.in);
String profile = scan.nextLine();
new SpringApplicationBuilder(DemoApplication.class).properties(
"spring.config.location=classpath:/application.yml"
).profiles(profile).run(args);
}
@Autowired
private Environment env;
@RequestMapping("/")
public String getProp(){
String res = env.getProperty("jdbc.driver");
return res;
}
}
After clicking the Run button in IDEA, press Enter in the console and then enter oracle,
Visit http://localhost:8080 / Output: oracle.jdbc.driver.OracleDriver
Re-Run, press Enter in the console and enter mysql,
Visit http://localhost:8080/ Output: com.mysql.jdbc.Driver p>
You can also set the profile through the names of different configuration files and create the following 3 files.
(1)src/main/resources/application.yml content:
spring:
profiles:
active: oracle
(2)src/main/resources/application-mysql.yml Content:
jdbc :
driver:
com.mysql.jdbc.Driver
(3)src/main/resources/application-oracle.yml Content:
jdbc:
driver:
oracle.jdbc.driver.OracleDriver
Modify the default startup class DemoApplication.cs code:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Scanner;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Autowired
private Environment env;
@RequestMapping("/")
public String getProp(){
String res = env.getProperty("jdbc.driver");
return res;
}
}
Visit http://localhost:8080/ Output: oracle.jdbc.driver.OracleDriver
< pre>test = config/application.properties
test1 = test1
test = application.properties
test2 = test2
test = src/main/resources/config/application.properties
test3 = test3
test = src/main/resources/application.properties
test4 = test4
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Autowired
private Environment env;
@RequestMapping("/")
public String getProp(){
String test = env.getProperty("test");
String test1 = env.getProperty("test1");
String test2 = env.getProperty("test2");
String test3 = env.getProperty("test3");
String test4 = env.getProperty("test4");
return test + "," + test1 + "," + test2 + "," + test3 + "," +< span style="color: #000000;"> test4;
}
}
test1 = application.properties
test2 = myConfig.properties
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
//SpringApplication.run(DemoApplication.class, args);< /span>
new SpringApplicationBuilder(DemoApplication.class).properties(
"spring.config.location=classpath:/config/myConfig.properties"
).run(args);
}
@Autowired
private Environment env;
@RequestMapping("/")
public String getProp(){
String test1 = env.getProperty("test1");
String test2 = env.getProperty("test2");
return test1 + "," + test2;
}
}
public static void main(String[] args) {
//SpringApplication.run(DemoApplication.class, args);< /span>
/*new SpringApplicationBuilder(DemoApplication.class).properties(
"spring.config.location=classpath:/config/myConfig.properties"
).run(args);*/
new SpringApplicationBuilder(DemoApplication.class).properties(
"spring.config.name=myConfig").run(args);
}
spring:
profiles: mysql
jdbc:
driver:
com.mysql.jdbc.Driver
---
spring:
profiles: oracle
jdbc:
driver:
oracle.jdbc.driver.OracleDriver
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Scanner;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
//SpringApplication.run(DemoApplication.class, args);< /span>
Scanner scan = new Scanner(System.in);
String profile = scan.nextLine();
new SpringApplicationBuilder(DemoApplication.class).properties(
"spring.config.location=classpath:/application.yml"
).profiles(profile).run(args);
}
@Autowired
private Environment env;
@RequestMapping("/")
public String getProp(){
String res = env.getProperty("jdbc.driver");
return res;
}
}
spring:
profiles:
active: oracle
jdbc:
driver:
com.mysql.jdbc.Driver
jdbc:
driver:
oracle.jdbc.driver.OracleDriver
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Scanner;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Autowired
private Environment env;
@RequestMapping("/")
public String getProp(){
String res = env.getProperty("jdbc.driver");
return res;
}
}