🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
The @Profile annotation in Spring Boot is used to define which components should be registered based on the active application profiles. This feature is crucial for maintaining different configurations for various environments, such as development, testing, and production, without changing the code.
Key Points
1. @Profile specifies the environment under which a bean should be included in the application context.
2. Multiple profiles can be activated simultaneously, allowing for complex configuration scenarios.
3. Profiles can be specified in application properties or programmatically set.
2. Development Steps
1. Create a configuration class with beans specific to different profiles.
2. Use the @Profile annotation to designate which profiles each configuration applies to.
3. Activate profiles through application properties or command line.
3. Implementation Example
// Step 1: Define configuration classes
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class AppConfiguration {
@Bean
@Profile("dev")
public String devDatabaseConnection() {
return "Development Database Connection";
}
@Bean
@Profile("test")
public String testDatabaseConnection() {
return "Test Database Connection";
}
@Bean
@Profile("prod")
public String prodDatabaseConnection() {
return "Production Database Connection";
}
}
// Step 2: Set active profile in `application.properties`
# Active profiles
spring.profiles.active=dev,test
// Step 3: Alternatively, activate profiles via command line
# Command to run Spring Boot application with specific profiles
java -jar myapplication.jar --spring.profiles.active=dev,test
Explanation:
1. AppConfiguration contains methods annotated with @Profile. Each method defines a bean that is only loaded into the Spring Context when its corresponding profile is active. For example, devDatabaseConnection is only available when the 'dev' profile is active.
2. The @Profile("dev"), @Profile("test"), and @Profile("prod") annotations ensure that beans are conditionally loaded based on the active profiles, allowing for targeted functionality in different environments.
3. Profiles can be activated in application.properties by setting spring.profiles.active to the desired profiles, or they can be activated via the command line when starting the application. This flexibility allows developers and operations teams to adapt the application behavior without changing the codebase.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment