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中的相关问题

参考: all和其他博客中的Spring @Bean和PropertyPlaceHolderConfigurer(来自我们的JCG合作伙伴 Biju Kunjummen)。

翻译自: https://www.javacodegeeks.com/2013/07/spring-bean-and-propertyplaceholderconfigurer.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/367794.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

导出mysql excel数据字典_mysql导出 Excel数据字典(全)

解决问题(有mysql数据库数据表想要将表导入到PowerDesigner 或导出Excel数据字典)一、下载工具1、工具PowerDesigner 百度自行下载安装2、mysql-connector-odbc 下载链接: https://pan.baidu.com/s/1cjb73f3GvkkMFAzZKi85xA 提取码: u5ih二、mysql数据库数据表想要将表导入到Po…

斐波那契数列算法小结

关于求解斐波那契数列,这是一道比较经典的题目,本文主要是对斐波那契数列求解方法的小结。 首先,定义Fibonacci数列如下: 方法1: 利用递归求解,这是最容易写出的算法,代码如下: #inc…

模块(sys/os/序列化模块)

sys 模块: sys.path 返回模块的搜索路径,初始化时使用pythonpath环境变量的值 sys.modules 返回所有在当前这个python程序中导入的模块的 sys.exit 退出程序 sys.argv 返回一个列表 列表的第一个元素是执行这个文件的时候,写在python后面的第一个值, 之后的元素是在执行…

JPA 2 | 获取联接以及我们是否应该使用它们

介绍 最近,我一直在与JPA 2中的FETCH JOINS一起使用,以期从数据库中急切地获取数据,并且我学到了很多关于为什么在日常操作中应避免使用Fetch Joins的知识。 今天的博客文章谈论了我在Fetch上的经历和学习(主要基于当我在查询中有…

mysql yintint类型_MySQL服务器2 被嫌弃的胖子

1.sql的基本语法对数据库create database db1;  创建数据库对表:create database t1(id int,name char(10));  创建表show create table t1;  查看创建的t1表show tables;  查看所有的表desc t1;  查看表的详细结构对数据:insert into t1(id,n…

Html5表单元素-搜索框和上传文件框

1、search - 搜索框element/form/input/search.html<!doctype html><html><head> <title>search</title></head><body> <!-- search - 搜索框&#xff0c;文本框形式 --> <input type"search"…

Shell 简单的java微服务jar包 -- 部署脚本

部署描述&#xff1a; 1.jenkins 通过maven编译成jar 项目包 2.shell 脚本从jenkins机器发布到&#xff1a;目标主机 注释&#xff1a;次脚本没有写jar包的备份&#xff0c;有时间加上 脚本内容&#xff1a; #!/bin/bash#线上服务器列表 HOST_LIST${:2}#项目名 REMOTE_PROJECT$…

BZOJ 4552 [Tjoi2016Heoi2016]排序 | 二分答案 线段树

题目链接 题面 题目描述 在2016年&#xff0c;佳媛姐姐喜欢上了数字序列。因而他经常研究关于序列的一些奇奇怪怪的问题&#xff0c;现在他在研究一个难题&#xff0c;需要你来帮助他。这个难题是这样子的&#xff1a;给出一个1到n的全排列&#xff0c;现在对这个全排列序列进行…

python import 类 继承_python学习之类的继承

面向对象中一个重要的特性就是继承&#xff0c;继承的好处就是提高代码的重用率&#xff0c;减少不必要的代码。继承是父类与子类的关系&#xff0c;当子类继承了父类后&#xff0c;就具有了父类的所有变量和方法。在python中定义继承的语法是&#xff1a;class 派生类名(基类名…

Html5画布(canvas)实例之绘制矩形

路径方式绘制 - 矩形 | rect()canvas/shape/path/rect.html <!DOCTYPE HTML><html><head> <title>以路径的方式在 canvas 上绘制矩形的 demo</title></head><body> <canvas id"canvas" width"300" hei…

[patl1-046]整除光棍

解题关键&#xff1a;模拟除法 #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream> #include<cmath> using namespace std; typedef long long ll; char ans[10002]; int main(){int n;cin>…

使用AspectJ审计Spring MVC Webapp。 第2部分

现在&#xff0c;如果您有兴趣创建一个以Aspectj的Aspect和Before批注的形式使用面向方面编程&#xff08;AOP&#xff09;的Spring MVC Webapp来审核用户对屏幕的访问&#xff0c;那么这是您想要阅读的博客。 正如我在上一个博客中所说的那样&#xff0c;审核用户对屏幕的访问…

服装店管理系统打造门店拓客、促活、存留营销方案

打造门店拓客、促活和存留营销方案对于服装店的管理系统来说是非常重要的。以下是一些可行的方案&#xff1a; 1. 会员管理系统&#xff1a;引入会员管理功能&#xff0c;建立会员档案&#xff0c;跟踪会员消费记录和偏好。通过会员系统&#xff0c;可以实施积分制度、生日礼品…

mysql添加映射模块_iis7.5中让html与shtml一样支持include功能(添加模块映射)

刚开始弄得时候&#xff0c;发现了很多错误&#xff0c;其实很简单&#xff0c;参考shtm原来的设置就可以了前提条件&#xff1a;ServerSideIncludeModule的安装&#xff1a;在安装iis的时候选择上该服务(“在服务端包含文件”&#xff0c;选项)即可&#xff0c;如下&#xff1…

全局对象与临时转换

全局对象 ECMAScript 规定全局对象叫做 global&#xff0c;但是浏览器把 window 作为全局对象 这些全局变量分为两种&#xff1a; ECMAScript 规定的global.parseIntglobal.parseFloatglobal.Numberglobal.Stringglobal.Booleanglobal.Object 浏览器自己加的属性window.alertwi…

Html5开发-使用Canvas绘制图片

呈现图片 | drawImage()canvas/media/image.html <!DOCTYPE HTML><html><head> <title>在 canvas 上呈现图片的 demo</title></head><body> <canvas id"canvas" width"800" height"600" sty…

文件上传控件bootstrap-fileinput的使用

一、准备1、插件下载地址&#xff1a;https://github.com/kartik-v/bootstrap-fileinput/ 下载后的压缩包解压文件夹内容如下&#xff1a; js&#xff1a;插件核心js代码&#xff0c;引用fileinput.min.js/fileinput.js即可&#xff0c;默认插件语言为英文&#xff0c;如需要中…

在JPA 2.1中使用@Convert正确完成映射枚举

如果您曾经在JPA中使用过Java枚举&#xff0c;那么您肯定会意识到它们的局限性和陷阱。 使用enum作为Entity的属性通常是一个很好的选择&#xff0c;但是2.1之前的JPA不能很好地处理它们。 它给了您2 1个选择&#xff1a; 托肖夫达林 Enumerated(EnumType.ORDINAL) &#xf…

python里orient_OrientDB Python连接操作

OrientDB Python连接操作Python的OrientDB驱动程序使用二进制协议。 PyOrient是git hub项目名称&#xff0c;它用于将OrientDB与Python连接起来并操作数据。 它适用于OrientDB 1.7及更高版本。以下命令用于安装PyOrient。pip install pyorient可以使用名为demo.py的脚本文件执行…

HTML5-画布(canvas)效果之-渐变色

<!DOCTYPE HTML><html><head> <title>渐变色</title></head><body> <canvas id"canvas" width"200" height"100" style"background-color: rgb(222, 222, 222)"> 您的浏…