Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Profiles: Simple example of ActiveProfilesResolver?

Tags:

java

spring

maven

what am I doing?
I have an app that I want to test across different environments - dev, staging, etc

What I do?
I am using maven cargo plugin to deploy application war to run integration tests.

What I need?
I need to my test to infer the spring.profiles.active based on environment variable set by cargo like

                    <container>
                        <containerId>tomcat7x</containerId>
                        <systemProperties>
                            <spring.profiles.active>development</spring.profiles.active>
                        </systemProperties>
                    </container>

Why?
so that I can remove the hard coding in my Integration Test @ActiveProfiles("development") and the test can infer what active profile is from environment variable

Question
- I found Spring integration tests with profile which mentions about using ActiveProfilesResolver
- I tried to find how can I make use of it, but could not find any resources
I am looking for guidance/recommendations on how to use ActiveProfilesResolver

like image 706
daydreamer Avatar asked May 26 '14 13:05

daydreamer


People also ask

How many profiles can be defined for a spring application?

In the following application, we have three profiles (local, dev, prod) and two profile-specific property files. We use the spring. profiles. active to set active profiles and SpringApplicationBuilder's profiles method to add new active profiles.

How do you define beans for a specific profile?

Use @Profile on a Bean Let's start simple and look at how we can make a bean belong to a particular profile. We use the @Profile annotation — we are mapping the bean to that particular profile; the annotation simply takes the names of one (or multiple) profiles.

Why do we need spring profiles?

Spring Profiles provide a way to segregate parts of your application configuration and make it only available in certain environments. Any @Component or @Configuration can be marked with @Profile to limit when it is loaded: @Configuration @Profile("production") public class ProductionConfiguration { // ... }

Where are spring profiles stored?

Profiles work in conjunction with Spring Boot properties files. By default, Spring Boot parses a file called application. properties – located in the src/main/resources directory – to identify configuration information.


1 Answers

Try this. I learned from different places and created the following. It works for me

public class SpringActiveProfileResolver implements ActiveProfilesResolver {

    @Override
    public String[] resolve(final Class<?> aClass) {
        final String activeProfile = System.getProperty("spring.profiles.active");
        return new String[] { activeProfile == null ? "integration" : activeProfile };
    }
}

and on you test class do this

@ActiveProfiles(resolver = SpringActiveProfileResolver.class)
public class IT1DataSetup {
......
}
like image 81
daydreamer Avatar answered Nov 15 '22 17:11

daydreamer