Spring JavaConfig properties in bean not set?

I am considering using Spring JavaConfig with some property files, but the properties in the bean are not set? The bean is not installed?

Here is my 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;
    }


}

Is the value in tr.url equal to "rt.setUrl" and not the value in application.properties?

+5
source share
3 answers

I'm not 100%, but I think yours is @PropertySourcenot quite right. Instead

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

It should be simple:

@PropertySource("classpath:application.properties")

based on this:

Spring Documentation PropertySource

Also, based on the link above and since you mentioned you are converting to java configuration approach instead of xml, I think there might be a solution to your problem below:

${...} @Value. ${...} @Value PropertySource, PropertySourcesPlaceholderConfigurer. XML, @ Bean @Configuration. . " " @Configuration Javadoc " BeanFactoryPostProcessor-return @Bean " @Bean Javadoc .

, :

 @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"));

.. . , . , .

+4

@ssn771, Environment , , , , @Value @Configuration POJO.

0

, PropertySourcesPlaceholderConfigurer Spring 3.1+ ( PropertyPlaceholderConfigurer Spring 3.0). static, , ( @Value).

@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

javadoc PropertySourcesPlaceholderConfigurer:

This class is designed as a general replacement for PropertyPlaceholderConfigurer in Spring 3.1 applications. It is used by default to support the property placeholder when working with XSD spring -context-3.1, whereas spring context versions <= 3.0 are the default for PropertyPlaceholderConfigurer for backward compatibility. See the spring-context XSD documentation for more details.

0
source

All Articles