[Spring] @PropertySource and Environment 사용시 Property 값이 null 로 들어오는 경우
Spring에서 Property 값을 아래와 같이 가져올 수 있다. @Configuration @PropertySource("classpath:myProperties.properties") public class MyConfiguration { @Autowired Environment environment; public DataSource getDataSource(){ dataSource.setDriverClassName(environment.get("jdbc.driverClassName")); return dataSource; } } 하지만, Autowiring이 늦게 발생하는 경우 environment 에는 값이 없는 상태로 MyConfiguration 의 getDataSource() 메서드가 먼저 실행된다. 일종의 트릭이지만, org.springframwork.context.EnvironmentAware 를 implement 하여 해결할 수 있다. @Configuration @PropertySource("classpath:myProperties.properties") public class MyConfiguration implements EnvironmentAware { private Environment environment; @Override public void setEnvironment(final Environment environment) { this.environment = environment; } public void myMethod() { final String myPropertyValue = environment.getProperty("myProperty"); // ... } } refs : http://sta...