【发布时间】:2018-04-19 20:21:40
【问题描述】:
我有这样的课程,如下所示:
@Configuration
@PropertySource("classpath:sample.properties")
public class SampleConfig {
@Value("${attr1.prop}")
private String attr1;
@Value("${attr2.prop}")
private String attr2;
@Bean
public SampleService sampleService() {
return new SampleService(attr1);
}
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
我创建了带有参数 attr1 的 SampleService bean。是否可以访问我稍后使用 @PropertySource 加载的属性?例如在@Autowired 之后。
这是使用该 bean 的代码:
@Service
public class SuperHotServiceImpl {
@Autowired
SampleService sammpleService;
public void fooFunc() {
// here I need some magic to get value of attr2.prop
sammpleService.setAttr(attr2);
}
}
你能告诉它是否可能吗?谢谢
【问题讨论】:
-
是什么让您无法在服务中简单地使用
@Value("${attr2.prop}") String attr2字段? -
@CoderinoJavarino 电子邮件通知,我需要在运行时在 fooFunction 中更改。
标签: java spring properties javabeans