☰ See All Chapters |
Spring @Profile Annotation
Spring @Profile annotation allows developers to register beans of any configuration class by condition. For example, register beans based on what operating system (Windows, Linux, etc...) your application is running, or load a database, logging configurations based on the application running in development, test, and preproduction or production environment. @Profile annotation has value attribute which accepts the name for the profile. We can then activate different profiles by using profile names in different environments to bootstrap just the beans we need. @Profile annotation can be used with @Configuration and @Component annotation.
@Profile with @Configuration
When we use @profile with @Configuration annotation spring will just bootstrap all the beans configured inside that configuration class. In the below example we activate the profile based on operating system so that spring just bootstrap the beans and configurations for that operating system.
@Profile with @Configuration Example
pom.xml
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.java4coding</groupId> <artifactId>Spring3_Profile_with_Configuration</artifactId> <version>0.0.1-SNAPSHOT</version>
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> </dependencies>
<properties> <spring.version>4.2.4.RELEASE</spring.version> </properties>
</project> |
OperatingSystem.java
package com.java4coding.os;
public interface OperatingSystem { public void printOSName(); } |
LinuxBean.java
package com.java4coding.os.linux;
import com.java4coding.os.OperatingSystem;
public class LinuxBean implements OperatingSystem { public void printOSName(){ System.out.println("Linux"); } } |
LinuxConfigurations.java
package com.java4coding.os.linux;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile;
@Configuration @Profile("linuxProfile") public class LinuxConfigurations { @Bean public LinuxBean getLinuxBean() { return new LinuxBean(); } } |
WindowsBean.java
package com.java4coding.os.winodws;
import com.java4coding.os.OperatingSystem;
public class WindowsBean implements OperatingSystem { public void printOSName(){ System.out.println("Windows"); } } |
WindowsConfigurations.java
package com.java4coding.os.winodws;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile;
@Configuration @Profile("windowsProfile") public class WindowsConfigurations {
@Bean public WindowsBean getWindowsBean() { return new WindowsBean(); } } |
Demo.java
package com.java4coding;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.java4coding.os.OperatingSystem; import com.java4coding.os.linux.LinuxConfigurations; import com.java4coding.os.winodws.WindowsBean; import com.java4coding.os.winodws.WindowsConfigurations;
public class Demo { public static void main(String[] args) {
OperatingSystem operatingSystem = null; AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
if (context.getEnvironment().getProperty("os.name").contains("Windows")) { //Profile should be set before registering configuration class to spring AnnotationConfigApplicationContext System.setProperty("spring.profiles.active", "windowsProfile"); context.register(WindowsConfigurations.class);//This should be done after we have set profile context.refresh(); operatingSystem = (WindowsBean) context.getBean("getWindowsBean"); }
if (context.getEnvironment().getProperty("os.name").contains("Linux")) { //Profile should be set before registering configuration class to spring AnnotationConfigApplicationContext System.setProperty("spring.profiles.active", "linuxProfile"); context.register(LinuxConfigurations.class);//This should be done after we have set profile context.refresh(); operatingSystem = (WindowsBean) context.getBean("getLinuxBean"); }
if (operatingSystem != null ) { operatingSystem.printOSName(); }
context.close(); } } |
Output:
We have executed the example program in Windows OS, Hence Windows profile got activated, spring has bootstrapped windows configurations and beans.
@Profile with @ Component
@Profile annotation can also be used with @Component annotation. Later based on profile set autowiring will inject the bean of that profile. In the below example we have used @Profile annotation with @Component annotation in LinuxBean and WindowsBean. Both LinuxBean and WindowsBean implements OperatingSystem interface. Hence spring throws NoSuchBeanDefinitionException as it has two beans of OperatingSystem type. When we active only one profile spring will bootstrap just the bean we need and inject that bean while autowiring.
@Profile with @ Component Example
pom.xml
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.java4coding</groupId> <artifactId>Spring3_Profile_with_Component</artifactId> <version>0.0.1-SNAPSHOT</version>
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> </dependencies>
<properties> <spring.version>4.2.4.RELEASE</spring.version> </properties>
</project> |
OperatingSystem.java
package com.java4coding.os;
public interface OperatingSystem { public void printOSName(); } |
LinuxBean.java
package com.java4coding.os;
import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component;
import com.java4coding.os.OperatingSystem;
@Component @Profile("linux") public class LinuxBean implements OperatingSystem { public void printOSName(){ System.out.println("Linux"); } } |
WindowsBean.java
package com.java4coding.os;
import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component;
import com.java4coding.os.OperatingSystem;
@Component @Profile("windows") public class WindowsBean implements OperatingSystem { public void printOSName(){ System.out.println("Windows"); } } |
Demo.java
package com.java4coding;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration;
import com.java4coding.os.OperatingSystem;
@Configuration public class Demo {
@Autowired OperatingSystem operatingSystem;
public static void main(String[] args) {
System.setProperty("spring.profiles.active", "linux"); //System.setProperty("spring.profiles.active", "winodws");
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.java4coding");
Demo demo = (Demo)context.getBean("demo"); demo.operatingSystem.printOSName();
context.close(); } } |
Output:
All Chapters