iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔖

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.

DevBean.java
@Profile("dev")
@Component
public class DevBean {
}
ProductionBean.java
@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.

DefaultBean.java
@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).

Enabled for profiles other than profile1
@Profile("!profile1")
@Component
public class FooBean {
}
Enabled when both profile1 and profile2 are specified at runtime
@Profile("profile1 & profile2")
@Component
public class FooBean {
}
Enabled when either profile1 or profile2 is specified at runtime
@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).

  1. Add @ActiveProfiles("profile-name") to a JUnit test class.
    • For multiple profiles, use an array: @ActiveProfiles({"profile1", "profile2"})
  2. Add -Dspring.profiles.active=profile-name to the java command.
    • For multiple profiles, use a comma-separated list: -Dspring.profiles.active=profile1,profile2
  3. Set the SPRING_PROFILES_ACTIVE environment variable to the profile name.
    • For multiple profiles, use a comma-separated list: SPRING_PROFILES_ACTIVE=profile1,profile2

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).

application.properties
sample.value1=Default1
sample.value2=Default2
application-dev.properties
# 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.

Specifying profiles via 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

https://github.com/MasatoshiTada/spring-profile-sample

References

Discussion