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. CentOS 6.5系统安装编译安装MySQL 5.6详细过程

    点评:CentOS 6.5下通过yum安装的MySQL是5.1版的,比较老,那我们就通过源代码安装高版本的MySQL5.6.14.一:卸载旧版本使用下面的命令检查是否安装有MySQL Server r ...

  2. cas sso ajax的jsonp实现方案总结(新浪微薄、淘宝案例分析)

    Cas自定义登录页面Ajax实现:http://cydiay.iteye.com/blog/1738713 CAS 之 跨域 Ajax 登录实践:http://denger.iteye.com/blo ...

  3. 如何ping别人的计算机名来获取IP?

    来源:http://blog.csdn.net/qq_27109081/article/details/47128175 如何ping别人的计算机名来获取IP?            获取别人的IP, ...

  4. lucene相关度算法

    一.这篇博客已经讲的很清楚 http://blog.csdn.net/starzhou/article/details/51543209 补充:q:就是一个查询比如是hello world 则  t: ...

  5. QT 控制LED实验

    1.实验准备 在PC 机D:盘下创建文件夹qt-led,将光盘qt_led_exp 文件夹下的images 文件夹拷贝到E:盘下qt-led 文件夹qt-led 内 2.新建工程 新建一个Empty ...

  6. 学习笔记之C++入门到精通(名师教学·手把手教会)【职坐标】_腾讯课堂

    C++入门到精通(名师教学·手把手教会)[职坐标]_腾讯课堂 https://ke.qq.com/course/101465#term_id=100105503 https://github.com/ ...

  7. Mongodb第一步资料

    学习的时候收集了部分好文章,这个文章主要收集的是下载,安装的基本资料 官方http://docs.mongodb.org/manual/tutorial/install-mongodb-on-wind ...

  8. java程序调优系列(一)intern()代替equal()

    1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...

  9. 清除MAC 可清除空间

    一.首先:查到了官方解释 https://support.apple.com/zh-cn/HT202867官方说 在 macOS Sierra 中,当您打开优化 Mac 储存空间时,会显示“可清除”内 ...

  10. Django---Xss过滤以及单例模式

    Xss过滤 在表单填写的过程中我们就用到textarea,富文本编辑框,里面要用户输入相关的内容.如果有的人想要搞怪,在里面写一些js代码或者修改编辑的时候修改源代码,那提交上去之后就会使得页面显示不 ...