我正在考虑使用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中的值?
@PropertySource(value =“classpath:application.properties”)
它应该只是:
@PropertySource( “类路径:application.properties”)
基于此:
Spring PropertySource Documentation
另外,根据上面的链接,因为你提到你转换为java配置方法而不是xml,我认为以下可能是你的问题的解决方案:
Resolving ${…} placeholders in and @Value annotations In
order to resolve ${…} placeholders in definitions or @Value
annotations using properties from a PropertySource,one must register
a PropertySourcesPlaceholderConfigurer. This happens automatically
when using in XML,but must be
explicitly registered using a static @Bean method when using
@Configuration classes. See the “Working with externalized values”
section of @Configuration Javadoc and “a note on
beanfactoryPostProcessor-returning @Bean methods” of @Bean Javadoc for
details and examples.
上面链接中的示例是我通常如何做到的:
@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"));