Spring “spring.profiles.include” overrides

We implemented the Spring active profiles in a slightly different way. Let’s say the default properties file, application.yml, contains all default values which is same in both production and development environments. Create separate properties for production and development files named application-prd.yml and application-dev.yml respectively. These files may contain extra properties or override some of the … Read more

Including profiles in spring boot 2.4.0 version

In case your configuration processing has changed in incompatible ways and you wish to use the “legacy” processing way, you can re-enable it by setting: spring.config.use-legacy-processing=true or alternatively, using YAML: spring: config: use-legacy-processing: true which should revert the configuration processing to the 2.3.x equivalent. Do note, however, that this property exists solely to ease the … Read more

Spring: How to do AND in Profiles?

Since Spring 5.1 (incorporated in Spring Boot 2.1) it is possible to use a profile expression inside profile string annotation. So: In Spring 5.1 (Spring Boot 2.1) and above it is as easy as: @Component @Profile(“TEST & CONFIG1”) public class MyComponent {} Spring 4.x and 5.0.x: Approach 1: answered by @Mithun, it covers perfectly your … Read more

Spring Profiles on method level?

For future readers who don’t want to have multiple @Beans annotated with @Profile, this could also be a solution: class MyService { @Autowired Environment env; void run() { if (Arrays.asList(env.getActiveProfiles()).contains(“dev”)) { log(); } } void log() { //only during dev } }

SpringBoot: Unable to find a single main class from the following candidates

If you have more than one main class, you need to explicitly configure the main class in each profile: <profiles> <profile> <id>profile1</id> <properties> <spring.boot.mainclass>com.SomeClass</spring.boot.mainclass> </properties> </profile> <profile> <id>profile2</id> <properties> <spring.boot.mainclass>com.SomeOtherClass</spring.boot.mainclass> </properties> </profile> </profiles> … <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.2.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <mainClass>${spring.boot.mainclass}</mainClass> </configuration> </execution> </executions> … </plugin> See spring-boot:repackage

What is the order of precedence when there are multiple Spring’s environment profiles as set by spring.profiles.active

The order of the profiles in the spring.profiles.active system property doesn’t matter. “Precedence” is defined by the declaration order of the beans, including beans specific to a profile, and the last bean definition wins. Using your example, if -Dspring.profiles.active=”default,dev” is used, the props bean in the default profile would be used here, simply because it’s … Read more

How to set spring active profiles with maven profiles

There is a more elegant way to switch between 2 maven+spring profiles simultaneously. First, add profiles to POM (pay attention – maven+spring profile is activated by single system variable): <profiles> <profile> <id>postgres</id> <activation> <activeByDefault>true</activeByDefault> <property> <name>spring.profiles.active</name> <value>postgres</value> </property> </activation> <dependencies> <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.1-901.jdbc4</version> </dependency> </dependencies> </profile> <profile> <id>h2</id> <activation> <property> <name>spring.profiles.active</name> <value>h2</value> </property> </activation> … Read more

Including bean definition when a profile is NOT active

You can use a not (!) operator… but you have to use Spring 3.2 M1. The following syntax is now supported <beans profile=”A,!B”> @Profile({“A”, “!B”}) indicating that the element or annotated component should be processed only if profile ‘A’ is active or profile ‘B’ is not active. See change announced here: Spring Framework 3.2 M1 … Read more

How to disable flyway in a particular Spring profile?

FYI, for anybody who comes here looking for this, the property name has changed for Spring Boot 2.0: For application.properties format: spring.flyway.enabled=false For application.yml format: spring: flyway: enabled: false Update: To disable flyway in a specific profile, you can put that property in the properties file specific to that profile. For instance, if your profile … Read more

Setting Spring Profile variable

You can simply set a system property on the server as follows… -Dspring.profiles.active=test Edit: To add this to tomcat in eclipse, select Run -> Run Configurations and choose your Tomcat run configuration. Click the Arguments tab and add -Dspring.profiles.active=test at the end of VM arguments. Another way would be to add the property to your … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)