A profile is a group of beans in a DI container. You can specify any group name.
To specify a profile for a bean, use the @ Profile
annotation.
DevBean.java
@Profile("dev")
@Component
public class DevBean {
}
ProductionBean.java
@Profile("production")
@Component
public class ProductionBean {
}
Beans without @ Profile
do not belong to a particular profile (also known as the default profile). ** A bean without a profile is always enabled at runtime no matter what profile you specify. ** **
DefaultBean.java
@Component
public class DefaultBean {
}
Since the value
element of @ Profile
is an array, you can specify multiple profiles for one bean. If you specify more than one, it will be enabled if any profile is specified at run time (not all profiles need to be specified at run time).
@Profile({"profile1", "profile2"})
@Component
public class FooBean {
}
You can also use !
(Negation), &
(and), and |
(or).
Enabled for anything other than profile1
@Profile("!profile1")
@Component
public class FooBean {
}
Enabled if both profile1 and profile2 are specified at run time
@Profile("profile1 & profile2")
@Component
public class FooBean {
}
Enabled if profile1 or profile2 is specified at run time
@Profile("profile1 | profile2")
@Component
public class FooBean {
}
If you have a mixture of
&
and|
, you must always use the parentheses()
. ❌@Profile("profile1 | profile2 & profile3")
⭕@Profile("profile1 | (profile2 & profile3)")
There are various ways to specify "I'll use this profile this time!" At runtime. The following three are often used. The higher the priority, the higher the priority.
@ActiveProfiles ("profile name ")
to the JUnit test class
--When multiple specifications are specified, specify an array @ActiveProfiles ({"profile1 "," profile2 "})
-Dspring.profiles.active = profile name
to the java
command
--Comma separated when multiple are specified -Dspring.profiles.active = profile1, profile2
SPRING_PROFILES_ACTIVE
--Comma separated when multiple are specified SPRING_PROFILES_ACTIVE = profile1, profile2
As mentioned above, the first caveat is that ** no matter what profile you specify at runtime, unprofiled beans will always be used. ** **
When using Spring Boot, the profile can also be used in the configuration file application.properties. application.properties has no profile, and application-profile name.properties is the setting used only when the profile is specified.
This means that the settings you write in ** application.properties will be used no matter what profile you specify. ** **
If you specify a profile and there are properties with the same name in application.properties and application-profile name.properties, the latter value is given priority (= overwrite) **.
application.properties
sample.value1=Default1
sample.value2=Default2
application-dev.properties
#This value is used when specifying the dev profile
sample.value1=Dev1
If you are using Spring Boot, you can specify it with a command line argument in addition to the above specification method.
Profile specification with command line arguments
$ java -jar target/spring-boot-profiles-sample-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
For IntelliJ IDEA (Ultimate Edition only), it can be specified in the execution settings (added with -D
at runtime).
https://github.com/MasatoshiTada/spring-profile-sample
Recommended Posts