2013-03-18 60 views
5

我期待在使用Spring JavaConfig與豆某些屬性的文件,但性質是沒有得到設置?在豆是沒有得到設置?bean中的Spring JavaConfig屬性沒有設置?

這裏是我的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價值?

+0

'@Override public void addResourceHandlers(ResourceHandlerRegistry registry)'方法有錯誤的實現。正確的實現是'registry.addResourceHandler(「/ resources/**」)。addResourceLocations(「/ resources /」);' – 2014-06-12 11:23:41

回答

4

我不是100%,但我認爲你的@PropertySource是不完全正確。取而代之的

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

應該僅僅是:

@PropertySource("classpath:application.properties")

在此基礎上:

Spring PropertySource Documentation

此外,基於上述既然你提到的鏈接你轉換爲java配置方法而不是xml,我認爲下面的m飛行能夠解決您的問題:

解決$ {...}在佔位符和@Value註釋在 爲了解決使用中的從屬性定義或@Value 註釋$ {...}佔位符PropertySource,必須註冊 a PropertySourcesPlaceholderConfigurer。這在XML使用時自動發生 ,但必須使用 @Configuration類時使用靜態@Bean方法明確地註冊。請參閱「關於 的BeanFactoryPostProcessor返流@Bean方法記」爲 細節和例子@Bean的Javadoc @Configuration的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")); 

等等的剩餘屬性值。我只是不熟悉你這樣做的方式。我知道上面的方法會起作用。

+0

我改變了它並沒有幫助:( – SJS 2013-03-18 14:21:03

+1

我認爲我剛剛發佈的最後一件事可能是你的因爲你只是轉換到java配置,而不是xml配置。你有沒有添加'PropertySourcesPlaceholderConfigurer'到你的配置? – ssn771 2013-03-18 14:24:15

+0

我不知道你的意思,但是.. – SJS 2013-03-18 14:32:11

0

在許多建議的事情中,最重要的是您需要在Spring 3.1+(或Spring 3.0中的PropertyPlaceholderConfigurer)中配置PropertySourcesPlaceholderConfigurer。如果您希望將其應用於配置類(使用@Value註釋),則它必須是static

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

從Javadoc文檔PropertySourcesPlaceholderConfigurer:

此類設計在春季3.1應用程序PropertyPlaceholderConfigurer一般更換。默認情況下,它使用property-placeholder元素來支持spring-context-3.1 XSD,而spring-context版本< = 3.0默認爲PropertyPlaceholderConfigurer,以確保向後兼容。有關完整的詳細信息,請參閱spring-context XSD文檔。

相關問題