Spring profile function, and Spring Boot application.properties

Spring profile function

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 {
}

Specifying multiple profiles

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 {
}

Use of logical operators

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

Run-time profile specification

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.

  1. Add @ActiveProfiles ("profile name ") to the JUnit test class --When multiple specifications are specified, specify an array @ActiveProfiles ({"profile1 "," profile2 "})
  2. Add -Dspring.profiles.active = profile name to the java command --Comma separated when multiple are specified -Dspring.profiles.active = profile1, profile2
  3. Specify the profile name in the environment variable 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. ** **

Spring Boot application.properties

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

Run-time profile specification when using Spring Boot

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

スクリーンショット 2019-12-29 10.50.03.png

Sample code

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

Reference material

Recommended Posts

Spring profile function, and Spring Boot application.properties
HTTPS with Spring Boot and Let's Encrypt
Implement paging function with Spring Boot + Thymeleaf
Add spring boot and gradle to eclipse
Challenge Spring Boot
Spring Boot Form
Spring Boot Memorandum
gae + spring boot
About designing Spring Boot and unit test environment
Spring Boot Whitelabel Error Page and JSON Response
Output request and response log in Spring Boot
Various correspondence table of Spring Framework and Spring Boot
Try to implement login function with Spring Boot
[Introduction to Spring Boot] Authentication function with Spring Security
Create a portfolio app using Java and Spring Boot
Plans to support JDK 11 for Eclipse and Spring Boot
Testing JPA entities and repositories using Spring Boot @DataJpaTest
Spring Boot application built-in Tomcat, Apache and WebSocket integration
Try using DI container with Laravel and Spring Boot
SPRING BOOT learning record 01
Spring Boot + Heroku Postgres
[Java] [Spring Boot] Specify runtime profile --Spring Boot starting with NetBeans
Spring boot memo writing (1)
Spring Security usage memo: Cooperation with Spring MVC and Boot
First Spring Boot (DI)
SPRING BOOT learning record 02
Spring Boot2 cheat sheet
Spring Boot with Spring Security Filter settings and addictive points
Spring Boot exception handling
Spring boot development-development environment-
Spring Boot learning procedure
Learning Spring Boot [Beginning]
Spring boot memo writing (2)
Spring Boot 2.2 Document Summary
[Spring Boot] DataSourceProperties $ DataSourceBeanCreationException
Spring Boot 2.3 Application Availability
Spring boot tutorials Topics
Attempt to SSR Vue.js with Spring Boot and GraalJS
Connect Spring Boot and Angular type-safely with OpenAPI Generator
Hello World comparison between Spark Framework and Spring Boot
Download with Spring Boot
With Spring boot, password is hashed and member registration & Spring security is used to implement login function.
Spring Boot Mybatis SQL Snake Case Column and Camel Case Mapping
About the function of Spring Boot due to different versions
Implement REST API with Spring Boot and JPA (Application Layer)
Implement REST API with Spring Boot and JPA (Infrastructure layer)
Until INSERT and SELECT to Postgres with Spring boot and thymeleaf
How to call and use API in Java (Spring Boot)
Get error information using DefaultErrorAttributes and ErrorAttributeOptions in Spring Boot 2.3
Connect to database with spring boot + spring jpa and CRUD operation
Implement REST API with Spring Boot and JPA (domain layer)
Domain Driven Development with Java and Spring Boot ~ Layers and Modules ~
8 things to insert into DB using Spring Boot and JPA
Various switching application.properties for each environment when Spring Boot starts
[Spring Boot] Environment construction (macOS)
Set context-param in Spring Boot
Try Spring Boot from 0 to 100.
Generate barcode with Spring Boot
Spring validation and error code
Hello World with Spring Boot
Implement GraphQL with Spring Boot