Profile annotation with example
The `@Profile` annotation in Spring is used to define which beans should be loaded based on the environment or profile the application is running in. This allows for configuring different beans for different environments, such as development, testing, and production. Profiles help in managing environment-specific configurations and switching between them seamlessly.
Table of Contents
Purpose:
To activate or deactivate beans based on the active profile(s).
Usage:
Commonly used to segregate configuration or beans that are specific to different environments or conditions.
Example
- Step 1: Create a Spring Boot Application
- Ensure you have the necessary dependencies in your `pom.xml` (for Maven) or `build.gradle` (for Gradle).
Maven Dependency
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
```
Gradle Dependency
```groovy
implementation 'org.springframework.boot:spring-boot-starter'
```
- Step 2: Create the Main Application Class
- Create the main class for the Spring Boot application.
Example
```java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
Explanation:
- `@SpringBootApplication`: Indicates a configuration class that declares one or more `@Bean` methods and also triggers auto-configuration and component scanning.
- Step 3: Define Profile-Specific Beans
- Create different beans for different profiles.
Example
```java
package com.example.demo.service;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
@Service
@Profile("development")
public class DevelopmentService implements GreetingService {
@Override
public String greet() {
return "Hello from Development!";
}
}
@Service
@Profile("production")
public class ProductionService implements GreetingService {
@Override
public String greet() {
return "Hello from Production!";
}
}
```
Explanation:
- `@Profile(“development”)`: Indicates that the `DevelopmentService` bean will only be loaded if the “development” profile is active.
- `@Profile(“production”)`: Indicates that the `ProductionService` bean will only be loaded if the “production” profile is active.
- Step 4: Create a GreetingService Interface
- Define an interface that the profile-specific beans will implement.
Example
```java
package com.example.demo.service;
public interface GreetingService {
String greet();
}
```
Explanation:
- `GreetingService`: An interface with a method `greet` to be implemented by different profile-specific classes.
- Step 5: Create a Controller to Use the Service
- Create a REST controller to expose the greeting service.
Example
```java
package com.example.demo.controller;
import com.example.demo.service.GreetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/greet")
public class GreetingController {
private final GreetingService greetingService;
@Autowired
public GreetingController(GreetingService greetingService) {
this.greetingService = greetingService;
}
@GetMapping
public String greet() {
return greetingService.greet();
}
}
```
Explanation:
- `@RestController`: Indicates that this class is a REST controller and its methods return data directly to the HTTP response body.
- `@RequestMapping(“/greet”)`: Maps the URL `/greet` to the `GreetingController` methods.
- `@Autowired`: Injects the `GreetingService` bean, which will be selected based on the active profile.
- Step 6: Configure Active Profiles
- Set the active profile in `application.properties` or via command-line arguments.
in 'application.properties'
```properties
spring.profiles.active=development
```
Or, via command-line argument'
```sh
java -jar demo-application.jar --spring.profiles.active=production
```
- Step 7: Running the Application
- Run the application from the main class (`DemoApplication`). Test the endpoint using a tool like Postman or by navigating to the URL in your browser:
GET request to `http://localhost:8080/greet` should return:
- `”Hello from Development!”` if the “development” profile is active.
- `”Hello from Production!”` if the “production” profile is active.
Conclusion
- `@Profile`: Used to activate or deactivate beans based on the current environment or profile. It helps in segregating configuration and beans according to different environments.
- Example: Demonstrated how to use `@Profile` to define beans for different profiles and how to select the active profile to determine which bean to use.