iTranslated by AI
Spring Profiles and Spring Boot's application.properties
Spring's Profile Feature
A profile is a group of beans within the DI container. You can specify any group name.
To specify a profile for a bean, use the @Profile annotation.
@Profile("dev")
@Component
public class DevBean {
}
@Profile("production")
@Component
public class ProductionBean {
}
Beans that are not annotated with @Profile do not belong to a specific profile (also referred to as the default profile). Beans without a profile are always enabled, regardless of which profile is specified at runtime.
@Component
public class DefaultBean {
}
Specifying Multiple Profiles
The value element of @Profile is an array, so you can specify multiple profiles for a single bean. If multiple profiles are specified, the bean will be enabled if any of the specified profiles is active at runtime (it is not necessary for all profiles to be active).
@Profile({"profile1", "profile2"})
@Component
public class FooBean {
}
Using Logical Operators
You can also use ! (NOT), & (AND), and | (OR).
@Profile("!profile1")
@Component
public class FooBean {
}
@Profile("profile1 & profile2")
@Component
public class FooBean {
}
@Profile("profile1 | profile2")
@Component
public class FooBean {
}
If
&and|are mixed, parentheses()are required.
❌@Profile("profile1 | profile2 & profile3")
⭕@Profile("profile1 | (profile2 & profile3)")
Specifying Profiles at Runtime
There are various ways to specify which profile to use at runtime. The following three are commonly used, listed in order of priority (highest priority first).
- Add
@ActiveProfiles("profile-name")to a JUnit test class.- For multiple profiles, use an array:
@ActiveProfiles({"profile1", "profile2"})
- For multiple profiles, use an array:
- Add
-Dspring.profiles.active=profile-nameto thejavacommand.- For multiple profiles, use a comma-separated list:
-Dspring.profiles.active=profile1,profile2
- For multiple profiles, use a comma-separated list:
- Set the
SPRING_PROFILES_ACTIVEenvironment variable to the profile name.- For multiple profiles, use a comma-separated list:
SPRING_PROFILES_ACTIVE=profile1,profile2
- For multiple profiles, use a comma-separated list:
As mentioned earlier, the most important point is that beans without a profile will always be used, regardless of which profile is specified at runtime.
Spring Boot's application.properties
When using Spring Boot, you can also use profiles for the configuration file application.properties. application.properties is used when no profile is active, while application-{profile-name}.properties is used only when that specific profile is specified.
In other words, the settings written in application.properties will be used regardless of which profile is specified.
If a profile is specified and properties with the same name exist in both application.properties and application-{profile-name}.properties, the value in the latter takes precedence (i.e., it overrides the former).
sample.value1=Default1
sample.value2=Default2
# This value is used when the dev profile is specified
sample.value1=Dev1
Specifying Runtime Profiles When Using Spring Boot
When using Spring Boot, in addition to the methods described above, there is a way to specify profiles using command-line arguments.
$ java -jar target/spring-boot-profiles-sample-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
In the case of IntelliJ IDEA (Ultimate Edition only), you can specify it in the Run Configuration (it will be added via -D at runtime).
<img width="1073" alt="Screenshot 2019-12-29 10.50.03.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/58650/405484bd-2fef-2136-2c7d-3a59c826268f.png">
Sample Code
Discussion