Spring @Bean和PropertyPlaceHolderConfigurer
最近,我被我认为将是一个相当简单的实现所困扰-考虑以下基于Spring Java的bean定义文件(
@Configuration ):
package root;...@Configuration
@PropertySource("classpath:root/test.props")
public class SampleConfig {@Value("${test.prop}")private String attr;@Beanpublic SampleService sampleService() {return new SampleService(attr);}
}
此处定义了一个bean“ sampleService”,该bean初始化为一个属性,该属性使用@Value注释(使用属性占位符字符串$ {test.prop})填充。
对此的测试如下:
@ContextConfiguration(classes=SampleConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class ConfigTest {@Autowiredprivate SampleService sampleService;@Testpublic void testConfig() {assertThat(sampleService.aMethod(), is("testproperty"));}
}
由于占位符$ {test.prop}根本无法解析,因此使用SampleConfig的当前实现会失败。 为此的标准解决方法是注册一个PropertySourcesPlaceholderConfigurer ,它是一个BeanFactoryPostProcessor,负责扫描所有已注册的bean定义并注入已解析的占位符。 进行此更改后,@ Configuration文件如下所示:
@Configuration
@PropertySource("classpath:root/test.props")
public class SampleConfig { @Value("${test.prop}")private String attr;@Beanpublic SampleService sampleService() {return new SampleService(attr);}@Beanpublic PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {return new PropertySourcesPlaceholderConfigurer();}
}
但是,在添加了属性解析器后,测试仍然失败,这一次sampleService返回的值为null,甚至没有占位符值!
导致该问题的原因是,在@Configuration内部使用诸如@ Autowired,@ Value和@PostConstruct之类的批注的情况下,任何BeanFactoryPostProcessor Bean都必须使用static修饰符进行声明。 否则,包含的@Configuration类将在很早之前实例化,并且负责解析诸如@ Value,@ Autowired等注释的BeanPostProcessors无法对其执行操作。
此修复程序在@Bean的javadoc中有详细记录,还记录了一条消息,提供了解决方法:
WARN : org.springframework.context.annotation.ConfigurationClassEnhancer - @Bean method RootConfig.placeHolderConfigurer is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean Javadoc for complete details
因此,使用此修复程序,新的工作配置如下:
@Configuration
@PropertySource("classpath:root/test.props")
public class SampleConfig { @Value("${test.prop}")private String attr;@Beanpublic SampleService sampleService() {return new SampleService(attr);}@Beanpublic static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {return new PropertySourcesPlaceholderConfigurer();}
}
参考文献:
- 吉拉记录本期
- @Bean Javadoc
- Stackoverflow中的相关问题
翻译自: https://www.javacodegeeks.com/2013/07/spring-bean-and-propertyplaceholderconfigurer.html
Spring @Bean和PropertyPlaceHolderConfigurer相关推荐
- Spring8:一些常用的Spring Bean扩展接口
前言 Spring是一款非常强大的框架,可以说是几乎所有的企业级Java项目使用了Spring,而Bean又是Spring框架的核心. Spring框架运用了非常多的设计模式,从整体上看,它的设计严格 ...
- java 扩展接口_详解常用的Spring Bean扩展接口
前言 Spring是一款非常强大的框架,可以说是几乎所有的企业级Java项目使用了Spring,而Bean又是Spring框架的核心. Spring框架运用了非常多的设计模式,从整体上看,它的设计严格 ...
- spring Bean自动装配
spring Bean自动装配 自动装配是使用spring满足bean依赖的一种方式. spring会在应用上下文中为某个bean寻找其依赖的bean. spring自动装配需要从两个角度来实现,或者 ...
- Spring Bean 中的线程安全
在 使用Spring框架时,很多时候不知道或者忽视了多线程的问题.因为写程序时,或做单元测试时,很难有机会碰到多线程的问题,因为没有那么容易模拟多线 程测试的环境.但如果不去考虑潜在的漏洞,它就会变成 ...
- Spring Bean配置方式之一:Java配置
简介: Spring bean 是使用传统的 XML 方法配置的.在这篇文章中,您将学习使用基于纯 Java 的配置而非 XML 来编写 Spring bean 并配置它们.本文将介绍可用来配置 be ...
- Spring bean 实现生命周期的三种解决方案
Spring bean 实现生命周期的三种解决方案 参考文章: (1)Spring bean 实现生命周期的三种解决方案 (2)https://www.cnblogs.com/javawebsoa/a ...
- Spring ----Bean的生命周期
这Spring框架中,一旦把一个bean纳入到Spring IoC容器之中,这个bean的生命周期就会交由容器进行管理, 一般担当管理者角色的是BeanFactory或ApplicationConte ...
- 【Spring实战】注入非Spring Bean对象
2019独角兽企业重金招聘Python工程师标准>>> 大家经常用到Spring IOC去管理对象之间的依赖关系,但一般情况下都有一个前提:这些Bean对象必须是通过Spring容器 ...
- spring bean作用域_Spring面试知识点,这是我见过最全面的 - 知识铺
知识铺: 致力于打造轻知识点,持续更新每次的知识点较少,阅读不累.不占太多时间,不停地来唤醒记忆深处的知识点. Q1.什么是Spring框架? Spring是最流行的企业应用程序框架之一.Spring ...
最新文章
- 小型工作室创业项目_为什么新开发人员应该在小型创业公司工作
- 考研计算机385分什么水平,机械专硕初试385分 给学弟学妹分享一下准备的过程(数学二)...
- SAP 货物移动 BAPI 的简单使用 BAPI_GOODSMVT_CREATE
- C++11智能指针shared_ptr、weak_ptr、unique_ptr用法
- 3 linux禁用ssl_ESXI申请阿里云SSL证书并配置
- linux 误删除mysql表能恢复吗,Linux误删数据恢复
- java判断对象无数据_java 对象属性不能为空判断
- linux源码安装 rpm命令
- Kafka从上手到实践 - 初步认知:MQ系统 | 凌云时刻
- QT QDir(获取当前路径下的所有文件)
- 鼎捷易助8.0ERP系统制造业ERP系统介绍
- SAP中利用标准成本报表计算成品人工成本及组成实例
- 常见的NLP处理手段和相应的spaCy库使用
- 省对应的市区经纬度信息
- 计算机网络——路由器和交换机
- 编码 GBK 的不可映射字符——【java菜鸟成长记】
- vue视频,vue视频下载
- 物联网+阿里云+小程序开发的一些工具和方法总结
- python标准库math中用来计算平方根的函数_python考试复习题库
- 2020年11月26日JetbrainsAgent安装参数
热门文章
- php array分组,php数组分组简单例子
- 转-SpringBoot——使用外置的Tomcat服务器
- java转换文本文件到xlsx(自制缓冲区,无需先验文件行数)
- hadoop2.6.0+eclipse配置
- api 获取网络使用情况_您的API是什么情况?
- 将json绑定为对象_了解自定义对象创建:JSON绑定概述系列
- 古巴比伦乘法_古巴平台中的通用过滤器–类固醇上的excel过滤器
- java集成lucene_将Lucene搜索集成到应用程序中
- jcache_窥探JCache API(JSR 107)
- cassandra 入门_Apache Cassandra和Java入门(第二部分)