2
我使用註解的配置,而不是XML我的應用程序...非簧內獲取配置值託管Bean
@Configuration
@ComponentScan(basePackages = {
"com.production"
})
@PropertySource(value= {
"classpath:/application.properties",
"classpath:/environment-${COMPANY_ENVIRONMENT}.properties"
})
@EnableJpaRepositories("com.production.repository")
@EnableTransactionManagement
@EnableScheduling
public class Config {
@Value("${db.url}")
String PROPERTY_DATABASE_URL;
@Value("${db.user}")
String PROPERTY_DATABASE_USER;
@Value("${db.password}")
String PROPERTY_DATABASE_PASSWORD;
@Value("${persistenceUnit.default}")
String PROPERTY_DEFAULT_PERSISTENCE_UNIT;
在這個文件中,我發現我可以從配置值@PropertySource
文件。我怎樣才能獲得春天管理bean以外的這些值?
我可以使用我的ApplicationContextProvider
來獲取這些值嗎?
public class ApplicationContextProvider implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext (ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
}
感謝您的回覆。爲什麼必須將其定義爲Spring bean?在我的應用程序中,我總是使用這種靜態。 – Webnet
@Webnet它並不是必須的,但如果是這樣,Spring將負責調用和設置'ApplicationContext'。否則,您需要在完成初始化後自行調用它。 –