您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

bean中的Spring JavaConfig属性未设置?

bean中的Spring JavaConfig属性未设置?

我不是100%,但是我认为您@PropertySource不太正确。代替

@PropertySource(value = "classpath:application.properties")

应该只是:

@PropertySource("classpath:application.properties")

基于此:

Spring PropertySource文档

另外,基于上面的链接,并且由于您已经提到过您正在转换为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"));

其余属性值等等。我只是不熟悉您的操作方式。我知道上述方法会起作用。

java 2022/1/1 18:15:20 有469人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶