spring中context:property-placeholder/元素
来源:互联网 发布:梵高知乎 编辑:程序博客网 时间:2024/05/21 09:31
1.有些参数在某些阶段中是常量
2.而这些参数在不同阶段之间又往往需要改变
期望:能不能有一种解决方案可以方便我们在一个阶段内不需要频繁书写一个参数的值,而在不同阶段间又可以方便的切换参数配置信息
解决:spring3中提供了一种简便的方式就是context:property-placeholder/元素
只需要在spring的配置文件里添加一句:<context:property-placeholder
#jdbc配置
test.jdbc.driverClassName=com.mysql.jdbc.Driver
test.jdbc.url=jdbc:mysql://localhost:3306/test
test.jdbc.username=root
test.jdbc.password=root
行内#号后面部分为注释
应用:
1.这样一来就可以为spring配置的bean的属性设置值了,比如spring有一个jdbc数据源的类DriverManagerDataSource
在配置文件里这么定义bean:
<bean id="testDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
</bean>
2.甚至可以将${ }这种形式的变量用在spring提供的注解当中,为注解的属性提供值
---------------------------------------------------------
外在化应用参数的配置
在开发企业应用期间,或者在将企业应用部署到生产环境时,应用依赖的很多参数信息往往需要调整,比如LDAP连接、RDBMS JDBC连接信息。对这类信息进行外在化管理显得格外重要。PropertyPlaceholderConfi
PropertyPlaceholderConfi
db.username=scott
db.password=tiger
如下内容摘自propertyplaceholderconfi
- <bean
id= "propertyPlaceholderConfigurer" -
- PropertyPlaceholderConfi
gurer"> -
<property name= -
<list> -
<value>userinfo.properties</value> -
</list> -
</property> - </bean>
-
- <bean
name= "userInfo"class="test.UserInfo"> -
<property name= value= "${db.username}"/> -
<property name= value= "${db.password}"/> - </bean>
通过运行并分析PropertyPlaceholderConfi
- <context:property-placeholder
location= "userinfo.properties"/>
PropertyPlaceholderConfi
Spring简化了加载资源文件的配置,可以通过<context:property-placeholder去加载,这个元素的写法如下:
<context:property-placeholder location="classpath:jdbc.properties"/>
如果想要配置多个properties文件
<context:property-placeholder location="classpath:jdbc.properties"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
这种方式是不被允许的,一定会出"Could not resolve placeholder"。
解决方案:
(1) 在Spring 3.0中,可以写:
<context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/>
<context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/>
(2) 但是在Spring 2.5中,<context:property-placeholder>没有ignore-unresolvable属性,所以就不能使用上面的那种方法去配置,
可以改如下的格式:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/jdbc.properties</value>
</list>
</property>
</bean>
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder元素
- spring中context:property-placeholder/元素 (
- spring中context:property-placeholder/元素
- spring中context:property-placeholder元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- 线程池和阻塞队列
- pl/sql块数据类型
- linux函数之msgrcv/msgsnd
- 计算结构体的大小
- 黑马程序员-5-入门注解的应用
- spring中context:property-placeholder/元素
- Git详解之四 服务器上的Git
- java类集学习概括
- 电子书:《加密与解密》第三版
- 子类继承和调用父类的构造函数方法 原则
- Spring 注解@Component,@Service,@Controller,@Repository
- oracle导出数据命令
- AspNetPager控件应用
- andriod 运行调试的时候报错INSTALL_FAILED_INSUFFICIENT_STORAGE