Many times, our project in the development environment and the environment configuration of the build environment is not the same, for example, the database configuration, in development, we generally use the test database, while in the production environment, we are using formal data, this time, We can use profile to configure different configuration files or different configurations in different environments.
Spring boot allows you to define multiple configuration files in a format (application-{profile}.properties) by naming conventions. You can then activate one or more configuration files by using spring.profiles.active in Application.properyies, and if there is no profile configuration file specified, spring Boot will start application-default.properties by default.
Profile configuration file can be placed in the same location as Application.properyies, placed in the following four locations, the current directory "/config" subdirectory under the current directory classpath root directory under the "/config" package The root directory of the Classpath
Here we define two profile files, Application-cus1.properties and application-cus2.properties, and write the variables Cusvar=cus1 and cusvar= in each of the two files. Cus2
We also write in application.properyies and switch profile to application-cus1.properties configuration file.
CUSVAR=CUS3
Spring.profiles.active=cus1
You can test it this way.
@RestController
@RequestMapping ("/task") public
class Taskcontroller {
@RequestMapping (value = {"/", ""} Public
string Helltask (@Value ("${cusvar}") string Cusvar) {
return ' Hello Task!! Myage is "+ Cusvar;
}
}
Here you can see that the spring.profiles.active activated profile is different, and the printed results are not the same.
In addition to using profile configuration files to configure our environment variables, in the code, we can also directly use @profile annotations to configure, such as database configuration, here we first define an interface
Public interface Dbconnector {public
void Configure ();
}
Define two implementation classes separately to implement it
/**
* Test Database
*
/@Component @Profile ("TestDB") Public
class Testdbconnector implements Dbconnector {
@Override public
Void Configure () {
System.out.println ("TestDB");
}
}
/**
* Production Database
*
/@Component @Profile ("Devdb") Public
class Devdbconnector implements Dbconnector {
@Override public
Void Configure () {
System.out.println ("Devdb");
}
}
Which implementation class is specifically used by activating the configuration file
Spring.profiles.active=testdb
And then you can use it.
@RestController
@RequestMapping ("/task") public
class Taskcontroller {
@Autowired dbconnector connector;
@RequestMapping (value = {"/", ""}) public
String Helltask () {
connector.configure ();//FINAL print TestDB
Return "Hello Task!!" Myage is "+ myage;
}
}
In addition to spring.profiles.active to activate one or more profiles, you can use Spring.profiles.include to overlay the profile
Spring.profiles:testdb
SPRING.PROFILES.INCLUDE:PRODDB,PRODMQ
The above is the role of spring boot with profile