- 클래스 설정
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
Properties propertiesFromYaml = loadYamlIntoProperties(resource);
String sourceName = name != null ? name : resource.getResource().getFilename();
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
}
private Properties loadYamlIntoProperties(EncodedResource resource) throws FileNotFoundException {
try {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
} catch (IllegalStateException e) {
Throwable cause = e.getCause();
if (cause instanceof FileNotFoundException)
throw (FileNotFoundException) e.getCause();
throw e;
}
}
}
- application.yml
spring:
profiles:
active: key
- application-key.yml (.ignore에 추가할것)
jwt:
secretKey: {}
spring:
datasource:
url: {}
driver-class-name: {}
username: {}
password: {}
- 적용 및 설정
@PropertySource(value = "classpath:application-key.yml", factory = YamlPropertySourceFactory.class, ignoreResourceNotFound = true)
public class JwtService {
@Value("${jwt.secret-key}")
private String secretKey;
...
'공부 > Tools' 카테고리의 다른 글
[H2] Intellij IDE에 embedded H2 연동 (0) | 2024.02.01 |
---|---|
[Docker] docker-compose.yml 기록용 (0) | 2024.02.01 |
[MySQL] utf8mb4 (ut8mb4_0900 사용시 주의점) (1) | 2024.01.21 |
[hibernate] properties/yml 설정 (0) | 2024.01.20 |