bean中的Spring JavaConfig属性没有设置? [英] Spring JavaConfig properties in bean is not getting set?
问题描述
我正在考虑使用Spring JavaConfig和一些属性文件,但bean中的属性没有设置?bean中的属性没有设置?
I am looking at using Spring JavaConfig with some property files but properties in bean is not getting set?in bean is not getting set?
这是我的WebConfig:
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;
}
}
tr.url中的值是rt.setUrl而不是application.properties中的值?
推荐答案
我不是100%,但我认为你的 @PropertySource
是不对的。而不是
I'm not 100%, but I think your @PropertySource
isn't quite right. Instead of
@PropertySource(value =classpath:application.properties)
它应该是:
@PropertySource(classpath:application.properties)
基于此:
此外,根据上面的链接,因为你提到你转换为java配置方法而不是xml,我认为以下可能是你的问题的解决方案:
Also, based on the link above and since you have mentioned you were converting to a java config approach instead of xml, I think the below might be the solution to your issue:
解决$ {...}占位符和@Value注释
命令解析定义中的$ {...}占位符或使用@Value
注释PropertySource中的属性,必须注册
a PropertySourcesPlaceholderConfigurer。当在XML中使用时,这会自动发生
,但在使用
@Configuration类时,必须使用静态@Bean方法显式注册
。有关
的详细信息和示例,请参阅@Bean Javadoc的使用外部化值
部分的@Configuration Javadoc和关于
BeanFactoryPostProcessor返回@Bean方法的说明。
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.
以上链接中的示例是我通常的做法:
The example from the link above is how I normally do it:
@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;
然后在你的方法中使用:
Then in your method use:
tr.setPassword(env.getProperty("rt.setPassword"));
等等剩余属性值。我对你的做法并不熟悉。我知道上述方法可行。
and so on for the remaining property values. I am just not as familiar with the way you are doing it. I know the above approach will work though.
这篇关于bean中的Spring JavaConfig属性没有设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!