bean中的Spring JavaConfig属性没有设置?

前端之家收集整理的这篇文章主要介绍了bean中的Spring JavaConfig属性没有设置?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在考虑使用Spring JavaConfig和一些属性文件,但bean中的属性没有设置?在bean中没有设置?

这是我的WebConfig:

  1. @Configuration
  2. @EnableWebMvc
  3. @PropertySource(value = "classpath:application.properties")
  4. @Import(DatabaseConfig.class)
  5. @ImportResource("/WEB-INF/applicationContext.xml")
  6. public class WebMVCConfig extends WebMvcConfigurerAdapter {
  7. private static final String MESSAGE_SOURCE = "/WEB-INF/classes/messages";
  8. private static final Logger logger = LoggerFactory.getLogger(WebMVCConfig.class);
  9. @Value("${rt.setPassword}")
  10. private String RTPassword;
  11. @Value("${rt.setUrl}")
  12. private String RTURL;
  13. @Value("${rt.setUser}")
  14. private String RTUser;
  15. @Bean
  16. public ViewResolver resolver() {
  17. UrlBasedViewResolver url = new UrlBasedViewResolver();
  18. url.setPrefix("/WEB-INF/view/");
  19. url.setViewClass(JstlView.class);
  20. url.setSuffix(".jsp");
  21. return url;
  22. }
  23. @Bean(name = "messageSource")
  24. public MessageSource configureMessageSource() {
  25. logger.debug("setting up message source");
  26. ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
  27. messageSource.setBasename(MESSAGE_SOURCE);
  28. messageSource.setCacheSeconds(5);
  29. messageSource.setDefaultEncoding("UTF-8");
  30. return messageSource;
  31. }
  32. @Bean
  33. public LocaleResolver localeResolver() {
  34. SessionLocaleResolver lr = new SessionLocaleResolver();
  35. lr.setDefaultLocale(Locale.ENGLISH);
  36. return lr;
  37. }
  38. @Override
  39. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  40. logger.debug("setting up resource handlers");
  41. registry.addResourceHandler("/resources/").addResourceLocations("/resources/**");
  42. }
  43. @Override
  44. public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
  45. logger.debug("configureDefaultServletHandling");
  46. configurer.enable();
  47. }
  48. @Override
  49. public void addInterceptors(final InterceptorRegistry registry) {
  50. registry.addInterceptor(new LocaleChangeInterceptor());
  51. }
  52. @Bean
  53. public SimpleMappingExceptionResolver simpleMappingExceptionResolver() {
  54. SimpleMappingExceptionResolver b = new SimpleMappingExceptionResolver();
  55. Properties mappings = new Properties();
  56. mappings.put("org.springframework.web.servlet.PageNotFound","p404");
  57. mappings.put("org.springframework.dao.DataAccessException","dataAccessFailure");
  58. mappings.put("org.springframework.transaction.TransactionException","dataAccessFailure");
  59. b.setExceptionMappings(mappings);
  60. return b;
  61. }
  62. @Bean
  63. public RequestTrackerConfig requestTrackerConfig()
  64. {
  65. RequestTrackerConfig tr = new RequestTrackerConfig();
  66. tr.setPassword(RTPassword);
  67. tr.setUrl(RTURL);
  68. tr.setUser(RTUser);
  69. return tr;
  70. }
  71. }

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

最佳答案
我不是100%,但我认为你的@PropertySource不太对劲.代替

@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.

上面链接中的示例是我通常如何做到的:

  1. @Configuration
  2. @PropertySource("classpath:/com/myco/app.properties")
  3. public class AppConfig {
  4. @Autowired
  5. Environment env;
  6. @Bean
  7. public TestBean testBean() {
  8. TestBean testBean = new TestBean();
  9. testBean.setName(env.getProperty("testbean.name"));
  10. return testBean;
  11. }
  12. }

所以在顶部添加

  1. @Autowired
  2. Environment env;

然后在你的方法中使用:

  1. tr.setPassword(env.getProperty("rt.setPassword"));

等等剩余的属性值.我对你的做法并不熟悉.我知道上面的方法会起作用.

猜你在找的Spring相关文章