Your First Spring Profiles Based Program

@Profile annotation allow you to register components that will be selected only if its associated profile is active. You can selectively activate a set of beans in a class or all beans .

We will see how to do this using a Java configuration file using the @Profile annotation, and then we will also discuss about how to do it using XML configuration.

 

Important points about Spring Profiles

  1. You can have @Profile at bean level or a configuration class level (classes annotated with @Configuration). You may group different set of beans into different configuration files. 

  2. You can also specify @Profile along with @Component

  3. If you are using an xml configuration, you can use the beans element along with its attributes profile and primary, and enclose the beans to do the same as in this example.

  4. You can specify the profile name as "default" to enable it as default:

    • @Profile({"default","dbprofile"})

  5. Profiles can be activates using different ways such as system property, annotation, web application context parameter etc. 

 

Example Code: Bean components

JJWriter.java

package com.javajee.spring;

public interface JJWriter {

    public void write();

}

 

JJFileWriter.java

package com.javajee.spring;

public class JJFileWriter implements JJWriter {

    public void write() {
        System.out.println("Writing to file!!!");
    }

}

 

JJDatabaseWriter.java

package com.javajee.spring;

public class JJDatabaseWriter implements JJWriter {

    public void write() {
        System.out.println("Writing to Database!!!");
    }
}

 

Example Code: Java config file

DemoConfig.java

package com.javajee.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
public class DemoConfig {

    @Bean (name = "writer")  
    @Profile({"default","dbprofile"})
    public JJWriter getJJDatabaseWriter()
    {
        return new JJDatabaseWriter();
    }
    
    @Bean (name = "writer")
    @Profile("fileprofile")
    public JJWriter getJJFileWriter()
    {
        return new JJFileWriter();
    }
}

 

Example Code: Test Class

JJWriterMain.java

package com.javajee.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class JJWriterMain {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.getEnvironment().setActiveProfiles("fileprofile");
        //Set profile and register config class
        context.register(DemoConfig.class);
        context.refresh();

        JJWriter writer = context.getBean("writer", JJWriter.class);

        writer.write();
    }
}

 

Different Ways to Activate Profiles

setActiveProfiles method

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.getEnvironment().setActiveProfiles("fileprofile");

See example above.

 

spring.profiles.active context parameter

For a web application, we can define a context parameter in web.xml:

<context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>dbprofile</param-value>
</context-param>

 

spring.profiles.active property

For a Spring boot application, we can define a property 'spring.profiles.active' in application.properties file. For non-boot spring applications, we can set the property in any properties file and then import the propertie file using @PropertySource on your Java configuration class.

 

@ActiveProfiles annotation

From within JUnit tests, we can enable profile using @ActiveProfiles:

@ActiveProfiles("dbprofile")

 

System property

You can also activate profile using System property as:

System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dbprofile, fileprofile");

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!