728x90
728x90
`@Profile` 어노테이션은 Spring Framework 에서 빈(Bean)이나 설정 클래스가 특정 환경 프로파일(profile)에서만 활성화되도록 지정하는 데 사용됩니다. 프로파일은 특정 환경 (예: 개발, 테스트, 운영)에 대한 설정을 분리하여 관리할 때 유용합니다.
예를 들어, 개발 환경에서 사용되는 설정을 분리하고 싶을 때 `@Profile("dev")` 어노테이션을 사용하여 해당 빈이나 설정 클래스를 특정 프로파일에서만 활성화 할 수 있습니다.
@Configuration
@Profile("dev")
public class DevelopmentConfig {
// 개발 환경에서 사용할 빈들의 설정
}
위의 코드에서 `DevelopmentConfg` 클래스는 `@Profile("dev")` 어노테이션을 가지고 있기 때문에 개발 프로파일에서만 활성화됩니다. 개발 이외의 환경에서는 이 빈은 로드되지 않습니다.
또한 `@Profile` 어노테이션은 빈(Bean) 메서드에도 적용할 수 있습니다.
@Configuration
public class AppConfig {
@Bean
@Profile("dev")
public DataSource dataSourceForDev() {
// 개발 환경용 DataSource 빈 설정
}
@Bean
@Profile("prod")
public DataSource dataSourceForProd() {
// 운영 환경용 DataSource 빈 설정
}
@Bean
@Profile({ "dev", "test", "prod" })
public DataSource dataSourceCommon() {
// 다중으로 지정도 가능
}
}
위의 코드에서 dataSourceForDev 메서드는 dev 프로파일에서만 활성화되고, dataSourceForProd 메서드는 prod 프로파일에서만 활성화됩니다.
이렇게 함으로써 각각의 프로파일에 따라 다른 빈 구성을 할 수 있습니다. 프로파일은 application.properties 또는 application.yml 파일에서 spring.profiles.active 속성을 통해 지정할 수 있습니다. 설정 파일에서 지정한 프로파일에 따라 해당 프로파일에 속한 빈들이 활성화됩니다.
728x90
300x250
'Spring' 카테고리의 다른 글
[Spring; 스프링] HttpServletRequest (0) | 2023.11.08 |
---|---|
[Spring; 스프링] Spring Boot JPA Hibernate 초기화 전략 (0) | 2023.10.25 |
[Spring; 스프링] Swagger 설정 (0) | 2023.10.24 |
[Spring; 스프링] Scheduler Cron ( 크론 / 스케줄러 ) (0) | 2023.10.20 |
[Spring; 스프링] RequestRejectedException - Spring Security (0) | 2023.10.18 |