bean中的Spring JavaConfig属性未设置?


问题内容

我正在使用带有某些属性文件的Spring JavaConfig,但bean中的属性未设置?bean中未设置?

这是我的WebConfig:

@Configuration
@EnableWebMvc
@PropertySource(value = "classpath:application.properties")
@Import(DatabaseConfig.class)
@ImportResource("/WEB-INF/applicationContext.xml")
public class WebMVCConfig extends WebMvcConfigurerAdapter {

    private static final String MESSAGE_SOURCE = "/WEB-INF/classes/messages";

    private static final Logger logger = LoggerFactory.getLogger(WebMVCConfig.class);

    @Value("${rt.setPassword}")
    private String RTPassword;

    @Value("${rt.setUrl}")
    private String RTURL;

    @Value("${rt.setUser}")
    private String RTUser;


    @Bean
    public  ViewResolver resolver() {
        UrlBasedViewResolver url = new UrlBasedViewResolver();
        url.setPrefix("/WEB-INF/view/");
        url.setViewClass(JstlView.class);
        url.setSuffix(".jsp");
        return url;
    }


    @Bean(name = "messageSource")
    public MessageSource configureMessageSource() {
        logger.debug("setting up message source");
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename(MESSAGE_SOURCE);
        messageSource.setCacheSeconds(5);
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver lr = new SessionLocaleResolver();
        lr.setDefaultLocale(Locale.ENGLISH);
        return lr;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        logger.debug("setting up resource handlers");
        registry.addResourceHandler("/resources/").addResourceLocations("/resources/**");
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        logger.debug("configureDefaultServletHandling");
        configurer.enable();
    }

    @Override
    public void addInterceptors(final InterceptorRegistry registry) {
        registry.addInterceptor(new LocaleChangeInterceptor());
    }

    @Bean
    public SimpleMappingExceptionResolver simpleMappingExceptionResolver() {
        SimpleMappingExceptionResolver b = new SimpleMappingExceptionResolver();

        Properties mappings = new Properties();
        mappings.put("org.springframework.web.servlet.PageNotFound", "p404");
        mappings.put("org.springframework.dao.DataAccessException", "dataAccessFailure");
        mappings.put("org.springframework.transaction.TransactionException", "dataAccessFailure");
        b.setExceptionMappings(mappings);
        return b;
    }

    @Bean
    public RequestTrackerConfig requestTrackerConfig()
    {
        RequestTrackerConfig tr = new RequestTrackerConfig();
        tr.setPassword(RTPassword);
        tr.setUrl(RTURL);
        tr.setUser(RTUser);

        return tr;
    }


}

tr.url中的值是“ rt.setUrl”,而不是application.properties中的值?


问题答案:

我不是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"));

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