Spring中PropertyPlaceholderConfigurer类载入外部配置
来源:互联网 发布:修真的程序员txt下载 编辑:程序博客网 时间:2024/06/05 19:57
在Spring项目中,你可能需要从properties文件中读入配置注入到bean中,例如数据库连接信息,memcached server的地址端口信息等,这些配置信息最好独立于jar包或者war包,这样便于修改配置。Spring提供了PropertyPlaceholderConfigurer类来处理这件事情。
例如,要载入配置文件中的mysql配置信息:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mypage
jdbc.username=root
jdbc.password=123456
我们需要在spring配置文件中定义PropertyPlaceholderConfigurer的location属性,这个location指向配置文件的位置:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="location" value="file:${APP_HOME}/conf/mypage.properties"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
在上面的例子中PropertyPlaceholderConfigurer类的systemPropertiesModeName的属性是SYSTEM_PROPERTIES_MODE_OVERRIDE这意味着可以从jvm虚拟机的参数中获得配置信息。
配置location的值中的${APP_HOME}可以根据环境不同设置不同的值,例如在开发环境,设置开发环境的目录,而在线上环境就要配置线上配置文件的路径了。
对于jar包文件,需要在启动jar包时指定APP_HOME的值,例如:
java -classpath test.jar -DAPP_HOME=/data0/conf cn.outofmemory.App
而对于运行在tomcat容器中的war包,需要修改下tomcat目录下的bin/catalina.sh文件,在此文件中添加或者修改如下变量的值:
JAVA_OPTS="-Xms2560m -Xmx5120m -DAPP_HOME=/application/outofmemory.cn/conf/"
注意-Xms2560m -Xmx5120m是为了修改虚拟机的内存值,-DAPP_HOME才影响配置文件位置。
给PropertyPlaceholderConfigurer指定必要的属性之后就可以通过${variable}的形式将配置文件中的值植入到bean中了。
如果你希望将配置文件放在jar包或者war包内,那么需要将location中的file:去掉,例如在war包中通常会放在/WEB-INF/conf路径下,那么可以这么写:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="location" value="/WEB-INF/conf/mypage.properties"/>
</bean>
转载自原链接:http://outofmemory.cn/code-snippet/3708/spring-properties-file-location
- Spring中PropertyPlaceholderConfigurer类载入外部配置
- 使用PropertyPlaceholderConfigurer类载入外部配置
- 使用PropertyPlaceholderConfigurer类载入外部配置
- spring中使用外部属性文件(关于PropertyPlaceholderConfigurer)
- spring中PropertyPlaceholderConfigurer的使用,引入外部资源文件
- 使用Spring的PropertyPlaceholderConfigurer类配置datasource
- spring的 PropertyPlaceholderConfigurer配置
- Spring配置之PropertyPlaceholderConfigurer
- Spring 配置之 PropertyPlaceholderConfigurer
- Spring中PropertyPlaceholderConfigurer
- Spring中配置和读取多个Properties文件-- PropertyPlaceholderConfigurer
- 系统启动PropertyPlaceholderConfigurer加载配置到spring容器中
- Spring配置多个PropertyPlaceholderConfigurer
- [Spring]PropertyPlaceholderConfigurer类相关
- spring中的PropertyPlaceholderConfigurer类
- spring之PropertyPlaceholderConfigurer类
- Spring 中 PropertyPlaceholderConfigurer 的使用
- Spring中PropertyPlaceholderConfigurer的使用
- Gstreamer源码安装(更新安装,基于ubuntu)
- [poj2406]Power Strings KMP
- Web移动端Fixed布局的解决方案
- RxSwift
- bzoj4926 皮皮妖的递推
- Spring中PropertyPlaceholderConfigurer类载入外部配置
- Redis和Memcache对比及选择
- shell 学习
- MYSQL:ERROR: Field '***' doesn't have a default value
- Spring任务调度<task:scheduled-tasks>【含cron参数详解】
- 通过旋转候选框实现任意方向的场景文本检测
- Deep learning系列(七)激活函数
- 【PHP】富文本HTML过滤器:HTMLPurifier使用教程(防止XSS)
- 纯css设置选项卡