我有这个课程,你可以在下面看到:
@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);
}
}
你能判断一下是否可能?感谢
答案 0 :(得分:0)
如果我理解你的问题,那么这应该会有所帮助:
@Service
public class SuperHotServiceImpl {
@Autowired
private SampleConfig sammpleConfig;
public void fooFunc() {
System.out.println(sampleConfig.getAttr2());
}
}
假设SampleConfig
注释为@Component
并且您提供了获取者。