스프링 부트(auto configuration)에서는 설정 정보들을 application.properties(yml) 파일에서 관리한다.
혹은 스프링부트가 관리하지 않지만 프로젝트에서 사용하는 커넥션 정보나 인증 정보 등등 코드에서 숨기고 싶거나 동적으로 변경하고 싶은 값들을 넣어서 사용한다.
이 설정파일에 있는 값들을 어떻게 가져와서 사용할 수 있을까?
test:
username: kim
password: 1234
@Component
public class ApplicationStartupEnvironment implements ApplicationListener<ApplicationStartedEvent> {
private final Environment env;
@Autowired
public ApplicationStartupEnvironment(Environment env) {
this.env = env;
}
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
String username = env.getProperty("test.username");
String password = env.getProperty("test.password");
System.out.println("username = " + username);
System.out.println("password = " + password);
}
}
@Component
public class ApplicationStartupValue implements ApplicationListener<ApplicationStartedEvent> {
@Value("${test.username}")
private String username;
@Value("${test.password}")
private int password;
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
System.out.println("username = " + username);
System.out.println("password = " + password);
}
}
@Component
@ConfigurationProperties("test")
public class TestProperties {
private String username;
private int password;
// getter, setter
}
@Component
public class ApplicationStartupCustom implements ApplicationListener<ApplicationStartedEvent> {
private final TestProperties testProperties;
public ApplicationStartupCustom(TestProperties testProperties) {
this.testProperties = testProperties;
}
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
System.out.println("username = " + testProperties.getUsername());
System.out.println("password = " + testProperties.getPassword());
}
}
test.email=email
test.address=address
@SpringBootApplication
@PropertySource(value = {"test.properties", "test2.properties"})
public class PropertiesApplication {
public static void main(String[] args) {
SpringApplication.run(PropertiesApplication.class, args);
}
}
@Component
public class ApplicationStartupValue implements ApplicationListener<ApplicationStartedEvent> {
@Value("${test.username}")
private String username;
@Value("${test.password}")
private int password;
@Value("${test.email}")
private String email;
@Value("${test.address}")
private String address;
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
System.out.println("username = " + username);
System.out.println("password = " + password);
System.out.println("email = " + email);
System.out.println("address = " + address);
}
}
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
Properties properties = factory.getObject();
return new PropertiesPropertySource(resource.getResource().getFilename(), properties);
}
}
@SpringBootApplication
@PropertySource(value = {"test.yml", "test2.yml"}, factory = YamlPropertySourceFactory.class)
public class PropertiesApplication {
public static void main(String[] args) {
SpringApplication.run(PropertiesApplication.class, args);
}
}