“This is the 14th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.
The company I work for now uses Spring.profiles. Active =@profiles. Active @ when I see this, I look confused, what does this @ mean?
In this case, the maven profile is used to select different profiles for development
Using the spring.profiles. Active parameter with the @profile annotation, it is possible to switch configuration parameters in different environments (development, test, production). In addition to application.properties, we can also name the format according to the naming convention: Application -{profile}.properties), if the active parameter does not match the file using the naming convention format, The app loads the configuration by default from a configuration file named application-default.properties. Such as: Spring.profiles. active=hello-world,sender,dev =hello-world,sender,dev =hello-world The project loads the configuration from application-dev.properties first and then from the application.properties configuration file. If there are duplicate configurations, the application.properties configuration prevails.
In actual combat
1. Build a SpringBoot project
Idea is used for construction here, and the process is omitted
2. Configure the POM file
<profiles> <profile> <! Production environment --> <id>prod</ ID > <properties> <profiles. Active >prod</profiles. Active > </properties> </profile> <profile> <! -- Local development environment --> < ID >dev</id> <properties> <profiles. Active >dev</profiles. Active > </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <! -- Test environment --> <id>test</ ID > <properties> <profiles. Active >test</profiles. Active > </properties> </profile> </profiles>Copy the code
- This is the default dev configuration
3. Configure multiple configuration files
application.properties
Note here that profiles. Active corresponds to the POM file
[email protected]@
Copy the code
application-dev.properties
name = "dev"
Copy the code
application-prod.properties
name = "prod"
Copy the code
application-test.properties
name = "test"
Copy the code
4. Write a test controller
/** * @author kevin * @date 2019/6/28 16:12 */ @RestController public class HelloController { @Value("${name}") private String name; @RequestMapping(value = {"/hello"},method = RequestMethod.GET) public String say(){ return name; }}Copy the code
5. Start the test
Start development using the IDEA tool
The default is dev. If you want to use the prod configuration file, select Prod as shown above, note the import below, and restart the project
D:\dev_code\profiles-demo\target>curl http://localhost:8080/hello
"prod"
Copy the code
6 pack
The idea package is not introduced here, if you use the command
mvn clean package -P dev
Copy the code
Use the dev configuration
If you now work with a registry, such as nacOS, you can use namespace
It can also be used to distinguish between different operations in different environments. In fact, it is necessary.
All right, have fun