context:propertyPlaceholder
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.
方式一:
<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的更多相关文章
- spring源码分析之<context:property-placeholder/>和<property-override/>
在一个spring xml配置文件中,NamespaceHandler是DefaultBeanDefinitionDocumentReader用来处理自定义命名空间的基础接口.其层次结构如下: < ...
- spring整合mybatis使用<context:property-placeholder>时的坑
背景 最近项目要上线,需要开发一个数据迁移程序.程序的主要功能就是将一个数据库里的数据,查询出来经过一系列处理后导入另一个数据库.考虑到开发的方便快捷.自然想到用spring和mybatis整合一下. ...
- 003医疗项目-关于<context:property-placeholder location="classpath:db.properties"/>的问题
项目结构如下:
- spring中context:property-placeholder/元素
1.有些参数在某些阶段中是常量 比如 :a.在开发阶段我们连接数据库时的连接url,username,password,driverClass等 b.分布式应用中client端访问server端所用的 ...
- spring错误<context:property-placeholder>:Could not resolve placeholder XXX in string value XXX
spring同时集成redis和mongodb时遇到多个资源文件加载的问题 这两天平台中集成redis和mongodb遇到一个问题 单独集成redis和单独集成mongodb时都可以正常启动程序,但是 ...
- spring错误:<context:property-placeholder>:Could not resolve placeholder XXX in string value XXX
spring同时集成redis和mongodb时遇到多个资源文件加载的问题 这两天平台中集成redis和mongodb遇到一个问题 单独集成redis和单独集成mongodb时都可以正常启动程序,但是 ...
- context:property-placeholder
这个在spring中配置文件中是非常常用的. context:property-placeholder大大的方便了我们数据库的配置. 只需要在spring的配置文件里添加一句:<context: ...
- spring中context:property-placeholder/元素 转载
spring中context:property-placeholder/元素 转载 1.有些参数在某些阶段中是常量 比如 :a.在开发阶段我们连接数据库时的连接url,username,passwo ...
- util:properties与context:property-placeholder
spring 使用注解装配的Bean如何使用property-placeholder属性配置中的值 这个问题不大不小,以前偷懒凡是碰到需要引用属性文件中的类时就改用xml来配置. 今天看了下sprin ...
- <context:property-placeholder/>元素
<context:property-placeholder/>元素 PropertyPlaceholderConfigurer实现了BeanFactoryPostProcessor接口,它 ...
随机推荐
- Go语言面组合式向对象编程基础总结
转自:http://blog.csdn.net/yue7603835/article/details/44282823 Go语言的面向对象编程简单而干净,通过非侵入式接口模型,否定了C/C++ Jav ...
- 接口自动化(六)--使用QQ邮箱发送邮件
接口测试执行完发送一个邮件,这里使用QQ邮箱发送,先要拿到QQ邮箱授权码,方法自行百度 # coding=utf-8 import smtplib from email.mime.text impor ...
- python打造文件包含漏洞检测工具
0x00前言: 做Hack the box的题.感觉那个平台得开个VIp 不然得凉.一天只能重置一次...mmp 做的那题毒药是文件包含漏洞的题,涉及到了某个工具 看的不错就开发了一个. 0x01代码 ...
- 整体读入cmd结果,而不是分行读入,效率极高
public static long GetDirectorySize(string path) { long res = 0; System.Diagnostics.Process p = new ...
- Selenium2+python自动化62-jenkins持续集成环境搭建
前言 selenium脚本写完之后,一般是集成到jenkins环境了,方便一键执行. 一.环境准备 小编环境: 1.win10 64位 2.JDK 1.8.0_66 3.tomcat 9.0.0.M4 ...
- Group By 和Having总结
1.Group By 概述 “Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组 所谓的分组就是将一个“数据集”划分成若干个“小区域”,然后针对若干个“小区域”进行数据处理. ...
- 彻底禁止win10更新
关闭win10自动更新: 可以用下面方法关闭: 1.首先在服务界面关闭Windows Update服务并设置为禁用并在恢复界面全部如下图设置为无操作. 2.只关闭了Windows Update服务发现 ...
- Spring 3.1 entityManagerFactory java.lang.NoSuchFieldError: NULL Error
This means there is a version mismatch--most likely with spring classes. So make sure all your sprin ...
- Halcon中二维码解析函数解码率和时长的优化方法
Halcon中条码解析函数包容多种条码类型且简单强大.现有的‘Data Matrix ECC 200’.‘QR Code’和‘PDF417’等广泛使用的条码均能解析.简单是通过默认参数即可对多种条码进 ...
- Spring中的注解配置-注入bean
在使用Spring框架中@Autowired标签时默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个.当找不到一个匹配的 Bean ...