Spring – Multiple Profiles active

We Are Going To Discuss About Spring – Multiple Profiles active. So lets Start this Java Article.

Spring – Multiple Profiles active

  1. Spring – Multiple Profiles active

    Spring version 5.1 and above offers additional functionality for specifying more complex profile string expressions. In your case desired functionality can be achieved in the following way:

  2. Spring – Multiple Profiles active

    Spring version 5.1 and above offers additional functionality for specifying more complex profile string expressions. In your case desired functionality can be achieved in the following way:

Solution 1

Spring version 5.1 and above offers additional functionality for specifying more complex profile string expressions. In your case desired functionality can be achieved in the following way:

@Profile({"Tomcat & Linux"})
@Configuration
public class AppConfigMongodbLinux {...}

Please read Using @Profile chapter from Spring reference documentation for more info.

Update (method level profile expressions):
Actually I’ve tested some @Bean method level profile expressions and everything works like a charm:

/**
 * Shows basic usage of {@link Profile} annotations applied on method level.
 */
@Configuration
public class MethodLevelProfileConfiguration {

    /**
     * Point in time related to application startup.
     */
    @Profile("qa")
    @Bean
    public Instant startupInstant() {
        return Instant.now();
    }

    /**
     * Point in time related to scheduled shutdown of the application.
     */
    @Bean
    public Instant shutdownInstant() {
        return Instant.MAX;
    }

    /**
     * Point in time of 1970 year.
     */
    @Profile("develop & production")
    @Bean
    public Instant epochInstant() {
        return Instant.EPOCH;
    }
}

Integration tests:

/**
 * Verifies {@link Profile} annotation functionality applied on method-level.
 */
public class MethodLevelProfileConfigurationTest {

    @RunWith(SpringRunner.class)
    @ContextConfiguration(classes = MethodLevelProfileConfiguration.class)
    @ActiveProfiles(profiles = "qa")
    public static class QaActiveProfileTest {

        @Autowired
        private ApplicationContext context;

        @Test
        public void shouldRegisterStartupAndShutdownInstants() {
            context.getBean("startupInstant", Instant.class);
            context.getBean("shutdownInstant", Instant.class);

            try {
                context.getBean("epochInstant", Instant.class);
                fail();
            } catch (NoSuchBeanDefinitionException ex) {
                // Legal to ignore.
            }
        }
    }

    @RunWith(SpringRunner.class)
    @ContextConfiguration(classes = MethodLevelProfileConfiguration.class)
    @ActiveProfiles(profiles = {"develop", "production"})
    public static class MethodProfileExpressionTest {

        @Autowired
        private ApplicationContext context;

        @Test
        public void shouldRegisterShutdownAndEpochInstants() {
            context.getBean("epochInstant", Instant.class);
            context.getBean("shutdownInstant", Instant.class);

            try {
                context.getBean("startupInstant", Instant.class);
                fail();
            } catch (NoSuchBeanDefinitionException ex) {
                // Legal to ignore.
            }
        }
    }
}

Spring 5.1.2 version was tested.

Original Author Laplas Of This Content

Solution 2

Unfortunately, @Profile activates if any listed profile is active. There are a couple of ways around this.

  • Apply the common @Profile("Tomcat") annotation to a top-level configuration class, and then apply @Profile("Windows") to a nested configuration class (or @Bean method).
  • If Spring Boot is acceptable as a dependency, use @AllNestedConditions to create an annotation that’s the AND instead of the OR.

It looks like what you’re trying to do would be clean to write if you were using Spring Boot autoconfiguration classes; if it’s practical to introduce autoconfiguration at this stage of your application’s lifecycle, I recommend considering it.

Original Author chrylis -cautiouslyoptimistic- Of This Content

Solution 3

@ConditionalOnExpression("#{environment.acceptsProfiles('Tomcat') && environment.acceptsProfiles('Linux')}")

Credits: Spring Source Code. Look up the @ConditionalOnExpression with your IDE then ‘find usages’ to see relevant examples within the source code. This will enable you to become a better developer.

Original Author Miguel Pereira Of This Content

Solution 4

The first profile is on the Top level.
The second I checked like this:

@Autowired
private Environment env;
...
final boolean isMyProfile = Arrays.stream(env.getActiveProfiles()).anyMatch("MyProfile"::equals);

Original Author user2695723 Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment