这是我的应用程序:
public static void main( String[] args ) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
//run the importer
final ImportNewOrders importer = (ImportNewOrders) ApplicationContextProvider.getApplicationContext().getBean("importNewOrders");
importer.run();
//importer.runInBackground();
}
这是我的配置:
@Configuration
@ComponentScan(basePackages = {
"com.production"
})
@PropertySource(value = {
"classpath:/application.properties",
"classpath:/environment-${MY_ENVIRONMENT}.properties"
})
@EnableJpaRepositories("com.fettergroup.production.repositories")
@EnableTransactionManagement
public class Config {
.... skipping things that aren't relevant
@Bean
public ImportNewOrders importNewOrders() {
return new ImportNewOrders();
}
这是我的班级......
@Component
public class ImportNewOrders implements Task {
private final static Logger logger = Logger.getLogger(ImportNewOrders.class.getName());
@Autowired
private OrderService orderService;
@Autowired
private ImportOrderRequest importOrderRequest;
@Value("${api.user}")
private String apiUser;
@Value("${api.password}")
private String apiPassword;
@Value("${api.orders.pingFrequency}")
private String pingFrequency;
最后是application.properties
:
# ------------------- Application settings -------------------
#Base URL to the API application
api.baseUrl=http://localhost:9998
#Unique token used to authenticate this vendor
api.vendor.token=asdf
#API credentials
api.user=myuser
api.password=mypassword
#How often to check for new orders; frequency is in seconds
api.orders.pingFrequency=60
这工作在一两个小时前,现在它决定它不喜欢这些价值观。我不知道为什么。一切看起来都对我不错。
更新
@Configuration
@ComponentScan(basePackages = {
"com.production"
})
@PropertySource(value = {
"classpath:/application.properties",
"classpath:/environment-${MY_ENVIRONMENT}.properties"
})
@EnableJpaRepositories("com.production.repositories")
@EnableTransactionManagement
public class Config {
@Value("${db.url}")
private static String PROPERTY_DATABASE_URL;
@Bean
public DataSource dataSource() {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUrl(PROPERTY_DATABASE_URL); //is null
/*dataSource.setUser(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_USER));
dataSource.setPassword(environment.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));*/
return dataSource;
}
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer () {
return new PropertySourcesPlaceholderConfigurer();
}
}
答案 0 :(得分:6)
您的@Configuration
找到了您的属性文件,并且因为@PropertySource
而将其用于该类中的数据库属性。但@Value
字段和${}
评估需要更多。
为了解决定义中的$ {...}占位符或 @Value注释使用PropertySource中的属性,必须使用 注册PropertySourcesPlaceholderConfigurer。有时候是这样的 在XML中使用时自动,但是 必须在使用时使用静态@Bean方法显式注册 @Configuration类。请参阅“使用外部化值” @Configuration Javadoc的一节和“关于。的注释 BeanFactoryPostProcessor-返回@Bean Javadoc的@Bean方法 细节和例子。
所以宣布一个
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();
p.setLocation(new ClassPathResource("your properties path"));
// other properties
return p;
}
在您的配置类中,或者如果您使用@PropertySource
,则ach已在评论中恰当地提及您可以完全省略setLocation
:
@Configuration
@PropertySource(value="classpath:your_file.properties")
public class MyConfiguration{
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer p = new PropertySourcesPlaceholderConfigurer();
return p;
}
}
拥有PropertySourcesPlaceholderConfigurer
但是,在大多数情况下,应用程序级bean不需要> 直接与环境交互,但可能必须 $ {...}属性值由属性占位符配置器替换 例如PropertySourcesPlaceholderConfigurer,它本身就是 EnvironmentAware和Spring 3.1默认情况下注册时 使用<上下文:属性占位符/>