bean中的Spring JavaConfig属性没有设置?
我正在考虑使用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中的值?
我不是100%,但我认为你的@PropertySource
不太@PropertySource
。 代替
@PropertySource(value = "classpath:application.properties")
它应该只是:
@PropertySource("classpath:application.properties")
基于此:
Spring PropertySource文档
此外,根据上面的链接,因为你提到你转换为java配置方法而不是xml,我认为以下可能是你的问题的解决方案:
解析$ {…}占位符和@Value注释为了使用PropertySource中的属性解析定义或@Value注释中的$ {…}占位符,必须注册一个PropertySourcesPlaceholderConfigurer。 在XML中使用时会自动发生这种情况,但在使用@Configuration类时必须使用静态@Bean方法显式注册。 有关详细信息和示例,请参阅@Configuration Javadoc的“使用外部化值”部分和@Bean Javadoc的“关于BeanFactoryPostProcessor返回@Bean方法的说明”部分。
上面链接中的示例是我通常如何做到的:
@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"));
等等剩余的属性值。 我对你的做法并不熟悉。 我知道上面的方法会起作用。
除了@ ssn771回答涉及注入Environment
并通过它检索属性这确实是建议的方式, 这就是我作为一种解决方法所做的,而不必改变@Value
在@Configuration
中使用的方式@Configuration
POJO。
在许多建议的事情中,最重要的是你需要在Spring 3.1+(或Spring 3.0中的PropertyPlaceholderConfigurer
中配置PropertySourcesPlaceholderConfigurer
。 如果要将其应用于配置类(使用@Value
注释),它必须是static
。
@Bean public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); }
来自PropertySourcesPlaceholderConfigurer的javadoc:
此类被设计为Spring 3.1应用程序中PropertyPlaceholderConfigurer的一般替代品。 默认情况下,它用于支持property-placeholder元素处理spring-context-3.1 XSD,而spring-context version <= 3.0默认为PropertyPlaceholderConfigurer以确保向后兼容性。 有关完整的详细信息,请参阅spring-context XSD文档。