Activates replacement of ${...} placeholders by registering a PropertySourcesPlaceholderConfigurer within the application context. Properties will be resolved against the specified properties file or Properties object -- so called "local properties", if any, and against the Spring Environment's current set of PropertySources. Note that as of Spring 3.1 the system-properties-mode attribute has been removed in favor of the more flexible PropertySources mechanism. However, Spring 3.1-based applications may continue to use the 3.0 (and older) versions of the spring-context schema in order to preserve system-properties-mode behavior. In this case, the traditional PropertyPlaceholderConfigurer component will be registered instead of the new PropertySourcesPlaceholderConfigurer. See ConfigurableEnvironment javadoc for more information on using.

https://docs.spring.io/spring/docs/4.3.11.RELEASE/spring-framework-reference/htmlsingle/#xsd-config-body-schemas-context-pphc

方式一:

<context:property-placeholder location="classpath:foo.properties"/>
@Component
public class FooProperties { public static String fooName; @Autowired
public void setFooName( @Value("${foo.name}") String fooName) {
FooProperties.fooName = fooName;
} }

方式二:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:foo.properties</value>
</property>
</bean>
@Component
public class FooProperties { public static String fooName; @Autowired
public void setFooName( @Value("${foo.name}") String fooName) {
FooProperties.fooName = fooName;
} }

方式三:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties">
<value>
foo.name=aaa
foo.age=12
</value>
</property>
</bean>
@Component
public class FooProperties { public static String fooName; @Autowired
public void setFooName( @Value("${foo.name}") String fooName) {
FooProperties.fooName = fooName;
} }

方式四:

        new PropertyPlaceholderConfigurer() {

            @Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
throws BeansException {
// TODO Auto-generated method stub
super.processProperties(beanFactoryToProcess, props);
}
}

重写默认placeholder 配置器

注: @Value 通过实例方法给静态变量注入值的时候需要配合 @Autowired 使用

@Value 可以直接为实例变量注入值 ${...} or "#{...['XXX']}

@Value("${...}")    与   @Value("#{...['XXX']} 各有优势,需视情况使用

context:propertyPlaceholder的更多相关文章

  1. spring源码分析之<context:property-placeholder/>和<property-override/>

    在一个spring xml配置文件中,NamespaceHandler是DefaultBeanDefinitionDocumentReader用来处理自定义命名空间的基础接口.其层次结构如下: < ...

  2. spring整合mybatis使用<context:property-placeholder>时的坑

    背景 最近项目要上线,需要开发一个数据迁移程序.程序的主要功能就是将一个数据库里的数据,查询出来经过一系列处理后导入另一个数据库.考虑到开发的方便快捷.自然想到用spring和mybatis整合一下. ...

  3. 003医疗项目-关于<context:property-placeholder location="classpath:db.properties"/>的问题

    项目结构如下:

  4. spring中context:property-placeholder/元素

    1.有些参数在某些阶段中是常量 比如 :a.在开发阶段我们连接数据库时的连接url,username,password,driverClass等 b.分布式应用中client端访问server端所用的 ...

  5. spring错误<context:property-placeholder>:Could not resolve placeholder XXX in string value XXX

    spring同时集成redis和mongodb时遇到多个资源文件加载的问题 这两天平台中集成redis和mongodb遇到一个问题 单独集成redis和单独集成mongodb时都可以正常启动程序,但是 ...

  6. spring错误:<context:property-placeholder>:Could not resolve placeholder XXX in string value XXX

    spring同时集成redis和mongodb时遇到多个资源文件加载的问题 这两天平台中集成redis和mongodb遇到一个问题 单独集成redis和单独集成mongodb时都可以正常启动程序,但是 ...

  7. context:property-placeholder

    这个在spring中配置文件中是非常常用的. context:property-placeholder大大的方便了我们数据库的配置. 只需要在spring的配置文件里添加一句:<context: ...

  8. spring中context:property-placeholder/元素 转载

    spring中context:property-placeholder/元素  转载 1.有些参数在某些阶段中是常量 比如 :a.在开发阶段我们连接数据库时的连接url,username,passwo ...

  9. util:properties与context:property-placeholder

    spring 使用注解装配的Bean如何使用property-placeholder属性配置中的值 这个问题不大不小,以前偷懒凡是碰到需要引用属性文件中的类时就改用xml来配置. 今天看了下sprin ...

  10. <context:property-placeholder/>元素

    <context:property-placeholder/>元素 PropertyPlaceholderConfigurer实现了BeanFactoryPostProcessor接口,它 ...

随机推荐

  1. Java9的新特性

    2017.9.21延期了好几次的Java9正式发布,在人工智能的时代,java还能不能持续辉煌是个问题.看看java9的新特性没什么让自己想升级的意愿,因为要么时一些特性用不到,要么时已经有其它方案代 ...

  2. VMware ESXi NAT实现

    VMware ESXi默认不支持NAT,但是我们如果只有一个外网端口映射,然后希望通过这个映射,从外网访问两台机器的话,那最好做NAT.这里我们通过一个开源的网络防火墙pfSense来实现NAT[1] ...

  3. js 获取下一秒 时间

    function getNextTime(start){ //var start = '09:30:00'; var _s = new Date(); var startDate = _s.getFu ...

  4. Linux(Centos)安装配置SVN服务器

    1. 安装SVN服务器: 检查是否已安装 # rpm -qa subversion 安装SVN服务器 # yum install httpd httpd-devel subversion mod_da ...

  5. 【失败】CentOS 6.5安装VNCserver 并开启远程桌面

    如果你的centos系统压根就没装CDE.Gnome等桌面,那你先执行这条命令,把GUI环境装上,no GUI no VNC yum -y groupinstall 'GNOME Desktop En ...

  6. canvas基础动画示例

    canvas基础动画示例 本文主要用最简单的例子,展示canvas动画效果是如何实现的 动画效果,是一个球绕着一点旋转 const canvas = document.getElementById(' ...

  7. VB.NET条码机打印设置纸张大小的方法

    Imports System.Drawing.PrintingImports System.Runtime.InteropServices Public Class Page    <Runti ...

  8. sass的类型判定

    由于sass的作者是rubyer,因此它的类型与JS有点不一样,但一样可以类推. @charset "utf-8";//必须设置了这个才能编译有中文的注释 $gray: #333; ...

  9. S 导入值列表浏览器、值列表

    先导入值列表浏览器,再导入值列表 一.导出模板 上面为导出模板 二.导入值列表浏览器 下面开始导入EXCEL数据 List Of Values Parent(1).xls List Of Values ...

  10. 解题报告-683. K Empty Slots

    There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one ...