Enabling Spring Profiles Through Property Example

We can use the property 'spring.profiles.active' within a properties file to activate a profile and is the most preferred approach. 

In last note, we had seen enabling Spring profiles from the main method itself by calling getEnvironment().setActiveProfiles method on the AnnotationConfigApplicationContext object.

We will define this property in a file called application.properties and then import this properties file using @PropertySource on the Java configuration class. If you are using Spring Boot, then you don't have to import this file as Spring Boot automatically import a file names application.properties.

I have made some refactoring to our main class to make it look more cleaner. I also added toString implementation to our bean classes.

 

Example Code: Bean Components

JJWriter.java

This class has not changed.

package com.javajee.spring;

public interface JJWriter {

    public void write();

}

 

JJDatabaseWriter.java

package com.javajee.spring;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component("writer")
@Profile({"default","dbprofile"})
public class JJDatabaseWriter implements JJWriter {

    public void write() {
        System.out.println("Writing to Database!!!");
    }
    
    @Override
    public String toString(){
        return "Writing to Database!!!";
    }
}

 

JJFileWriter.java

package com.javajee.spring;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component("writer")
@Profile("fileprofile")
public class JJFileWriter implements JJWriter {

    public void write() {
        System.out.println("Writing to File!!!");
    }
    
    @Override
    public String toString(){
        return "Writing to File!!!";
    }

}

 

Example Code: Defining Property

Create a properties file 'application.properties' in classpath with the property 'spring.profiles.active':

spring.profiles.active=dbprofile

 

Example Code: Java Config File

DemoConfig.java

package com.javajee.spring;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:/application.properties")
@ComponentScan(basePackages="com.javajee.spring")
public class DemoConfig {

}

 

Example Code: Test Class

JJWriterMain.java

package com.javajee.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;

public class JJWriterMain {

    public static void main(String[] args) {

        AbstractApplicationContext context = new AnnotationConfigApplicationContext(DemoConfig.class);

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

        writer.write();
        
        context.close();
    }
}

Now change the profiles within properties file to test. If you use a profile that does not exist, you get a error that the bean could not be found. If you remove the profile property, then you can test the default profile.

If you have come to this page directly, please also refer to the previous note @ http://javajee.com/creating-and-enabling-spring-profiles-in-a-standalone....

Search the Web

Custom Search

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