Spring Boot supports the use of different configuration files in different environments .
Spring boot allows you to define multiple configuration files in a certain format ( application-{profile}.properties ) through naming conventions ,
Then one or more profiles are specifically activated through the configuration item spring.profiles.active in application.properties,
Such as
spring.profiles.active=dev,test
If no profile configuration file is specified, spring boot will start application-default.properties by default.
Profile mode:
The contents of application-dev.properties are as follows:
name=development environment
The contents of application-prod.properties are as follows:
name=production environment
Annotation mode:
org.springframework.context.annotation.Profile
Spring Boot uses the @Profile annotation to switch configuration parameters in different environments.
Any @Component or @Configuration annotated class can be annotated with @Profile.
@Profile(value = "prod")
@Service
public class UserProdServiceImpl implements UserService{
//----------------------
}
@Profile(value = "dev")
@Service
public class UserDevServiceImpl implements UserService{
//----------------------
}
@Profile("production")
@Configuration
public class ProductionConfiguration {
//----------------------
}
@Profile("test")
@Configuration
public class TestConfiguration {
//----------------------
}
specified at startup
java -jar springbootApp.jar --spring.profiles.active=dev
specified in the configuration file
You can add profile activation items in SpringBoot's default or specified configuration files (such as application.properties). Such as:
spring.profiles.active=dev
spring.profiles.active=prod