1. Profile
- 특정 환경에서만 등록될 빈(Bean) 정의의 논리적 그룹
@Profile 애너테이션
- 특정 프로파일이 활성화될 때만 해당 빈이 등록되도록 합니다
- 표현식 사용 가능 (&, |, !)
예제) @Profile
더보기
@Configuration
@Profile("development")
public class StandaloneDataConfig {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
.addScript("classpath:com/bank/config/sql/schema.sql")
.addScript("classpath:com/bank/config/sql/test-data.sql")
.build();
}
}
@Configuration
@Profile("production")
public class JndiDataConfig {
@Bean(destroyMethod = "")
public DataSource dataSource() throws Exception {
Context ctx = new InitialContext();
return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
}
}
예제) 표현식
더보기
@Profile("production & !test")
Default Profile
- 아무 프로파일도 활성화되지 않았을 떄 활성화되는 프로파일
예제
더보기
@Configuration
@Profile("default")
public class DefaultDataConfig {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
.addScript("classpath:com/bank/config/sql/schema.sql")
.build();
}
}
활성화
- spring.properties: spring.profiles.active 속성
- Programmatically: Environment 객체
예제) Environment 객체를 활용한 활성화
더보기
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles("development");
ctx.refresh();
2. PropertySource Abstraction
구분 | 설명 | 예시 |
Property | 애플리케이션에서 사용하는 속성 값. 여러 소스에서 제공될 수 있으며, key-value 형태로 구성된다. |
server.port=8080
my.property=value |
PropertySource | 속성 값이 저장되는 소스. 여러 가지 소스에서 Property 값을 제공하며, 이들을 추상화하여 관리 |
application.properties
JVM 시스템 속성 OS 환경 변수 |
Environment | Spring에서 애플리케이션의 속성 소스를 관리하는 객체. 여러 PropertySource를 통합하여, 속성 값을 읽고 설정함. |
Environment.getProperty("my.property")
|
예제
더보기
public class PropertySourceExample {
public static void main(String[] args) {
// Spring ApplicationContext 생성
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
// Environment 객체 가져오기
Environment env = context.getEnvironment();
// 속성 값 읽기
String propertyValue = env.getProperty("my.property");
System.out.println("Property value: " + propertyValue);
// 속성 존재 여부 확인
boolean exists = env.containsProperty("my.property");
System.out.println("Property exists: " + exists);
}
}
3. @PropertySource
- 외부 속성 파일을 명시적으로 등록하고 PropertySource를 Environment에 추가하는 애너테이션
- 정의된 속성들이 Environment에 자동으로 로드되고, 이후에 Environment를 통해 해당 속성들을 읽을 수 있음
예제
더보기
@Configuration
@PropertySource("classpath:application.properties")
public class AppConfig {
@Bean
public SomeBean someBean() {
// SomeBean에서 application.properties의 값을 사용할 수 있음
return new SomeBean();
}
}
@Component
public class SomeBean {
@Value("${some.property}")
private String someProperty;
public void printProperty() {
System.out.println("Property value: " + someProperty);
}
}
@Component
public class SomeBean {
private final Environment environment;
@Autowired
public SomeBean(Environment environment) {
this.environment = environment;
}
public void printProperty() {
String someProperty = environment.getProperty("some.property");
System.out.println("Property value: " + someProperty);
}
}
출처
'Spring > Spring' 카테고리의 다른 글
[Spring][Field] 1. Type Conversion (0) | 2025.04.06 |
---|---|
[Spring][Core] 3-5. Container: Additional Capabilities of the ApplicationContext (0) | 2025.04.06 |
[Spring][Core] 3-3. Container: Using JSR 330 Standard Annotations (0) | 2025.04.06 |
[Spring][Core] 3-2. Container: Classpath Scanning and Managed Components (0) | 2025.04.06 |
[Spring][Core] 3-1. Container: Annotation-based Configuration (0) | 2025.04.06 |