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接口,它 ...
随机推荐
- Spring bean作用域
全当知识要点记录了,大家随意踩踩. spring的作用域有以下几种singleton作用域prototype作用域request作用域session作用域global-session作用域 1. si ...
- java System.arrayCopy使用说明
java System.arrayCopy使用说明 java.lang.System.arraycopy() 方法复制指定的源数组的数组,在指定的位置开始,到目标数组的指定位置. 下面是 System ...
- UOJ 55 【WC2014】紫荆花之恋——点分治+平衡树
题目:http://uoj.ac/problem/55 点分治.在点分树上每个点上用 splay 维护管辖的点的情况.做几次就重构点分树.TLE.只能过 20 分. #include<cstdi ...
- 【linux】ls常用参数
1. 命令格式: ls [选项] [目录名] 2. 命令功能: 列出目标目录中所有的子目录和文件. 3. 常用参数: -a, –-all 列出目录下的所有文件,包括以 . 开头的隐含文件 -d, –- ...
- MySQL 高性能存储引擎:TokuDB初探
在安装MariaDB的时候了解到代替InnoDB的TokuDB,看简介非常的棒,这里对ToduDB做一个初步的整理,使用后再做更多的分享. 什么是TokuDB? 在MySQL最流行的支持全事务的引擎为 ...
- Bootstrap-Other:HTML编码规范
ylbtech-Bootstrap-Other:HTML编码规范 1.返回顶部 1. Bootstrap HTML编码规范 语法 用两个空格来代替制表符(tab) -- 这是唯一能保证在所有环境下获得 ...
- Ubuntu下VIM使用指南
基本命令: Esc:VIM中的万能功能键之一,基本上任何时候按这个键,都可以返回VIM的普通状态. i:在普通状态下按i可以进入“插入”编辑状态,这个时候按方向键移动光标,在想要输入的地方输入字符,用 ...
- c#,条码
static void Generate2(string text){ BarcodeWriter writer = new BarcodeWriter(); //使用ITF 格式,不能被现在常用的支 ...
- leetcode303
public class NumArray { List<int> list = new List<int>(); public NumArray(int[] nums) { ...
- 26.OGNL与ValueStack(VS)-N语法top语法
转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html N语法[0]:<s:property value="[0]& ...