aes:
key: "secreyKey"
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
@Configuration
@Data
@ConfigurationProperties(prefix = "aes")
public class ApplicationSetting {
private String key;
}
@Component
@RequiredArgsConstructor
public class AES256 {
private final ApplicationSetting applicationSetting;
PropertySource에 yml을 read할 수 있는 factory가 없기 때문에 임의로 생성
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(@Nullable 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) {
// for ignoreResourceNotFound
Throwable cause = e.getCause();
if (cause instanceof FileNotFoundException)
throw (FileNotFoundException) e.getCause();
throw e;
}
}
}
@Configuration
@Data
@PropertySource(value = "classpath:aes.yml", factory = YamlPropertySourceFactory.class)
@ConfigurationProperties(prefix = "aes")
public class ApplicationSetting {
private String key;
}