我不是100%,但是我认为您@PropertySource
不太正确。代替
@PropertySource(value = "classpath:application.properties")
应该只是:
@PropertySource("classpath:application.properties")
基于此:
另外,基于上面的链接,并且由于您已经提到过您正在转换为Java配置方法而不是xml,所以我认为以下可能是解决您问题的方法:
在$和$ Value批注中解析$ {…}占位符为了使用PropertySource中的属性在定义或@Value批注中解析$ {…}占位符,必须注册一个PropertySourcesPlaceholderConfigurer。在XML中使用时会自动发生,但是在使用@Configuration类时必须使用静态@Bean方法显式注册。有关详细信息和示例,请参见@Configuration Javadoc的“使用外部化的值”部分和@Bean Javadoc的“关于beanfactoryPostProcessor-返回@Bean方法的说明”。
上面链接中的示例是我通常的操作方式:
@Configuration
@PropertySource("classpath:/com/myco/app.properties")
public class AppConfig {
@Autowired
Environment env;
@Bean
public TestBean testBean() {
TestBean testBean = new TestBean();
testBean.setName(env.getProperty("testbean.name"));
return testBean;
}
}
因此,在顶部添加:
@Autowired
Environment env;
然后在您的方法中使用:
tr.setPassword(env.getProperty("rt.setPassword"));