spring3以后 <context:property-placeholder>配置资源文件
来源:互联网 发布:笔记本硬盘坏了数据 编辑:程序博客网 时间:2024/05/23 19:35
<context:property-placeholder>配置资源文件
直接在 spring 配置文件里面加上
(注意:多个*.properties文件,需要使用逗号“,”隔开。配置多个context:property-placeholder只会加载第一个,后面的就不加载,直接跳过了)
(file-encoding="UTF-8":若有中文,防止乱码,不需要可以不写。ignore-resource-not-found="true":没有找到文件就跳过,可以不写。)
<context:property-placeholder file-encoding="UTF-8" location="classpath*:application.properties,classpath*:resource.properties,classpath*:memcached.properties,classpath:sys.properties" ignore-resource-not-found="true" />
这样就能在配置数据源的时候这样子
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- Connection Info --> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- Connection Pooling Info --> <property name="initialSize" value="${dbcp.initSize}" /> <property name="maxIdle" value="${dbcp.maxIdle}"/> <property name="maxActive" value="${dbcp.maxActive}"/> <property name="defaultAutoCommit" value="false"/> <property name="timeBetweenEvictionRunsMillis" value="3600000"/> <property name="minEvictableIdleTimeMillis" value="3600000"/> </bean>
同时也可以在代码里面这样获取
@Value("${jdbc.driver}")
private String jdbcdriver;
当然如果用 @Value 注解获取属性的话,你的类也必须是spring注入管理的。
如果用了SpringMVC,而且你在controller里面用 @Value 获取不到属性值的话,应该在SpringMVC配置文件里面也加上上面的配置!
因为在使用spring mvc时,实际上是两个spring容器:
1,dispatcher-servlet.xml 是一个,我们的controller就在这里,所以这个里面也需要注入属性文件
org.springframework.web.servlet.DispatcherServlet
这里最终是使用
WebApplicationContext parent =WebApplicationContextUtils.getWebApplicationContext(getServletContext());
创建spring容器,代码在FrameworkServlet中
2,applicationContext.xml 是另外一个,也需要注入属性文件
org.springframework.web.context.ContextLoaderListener
在我们的service中可以拿到@Value注入的值,那是因为我们通常都会把获取属性文件定义在applicationContext.xml中,这样在 Controller中是取不到的,必须在dispatcher-servlet.xml 中把获取属性文件再定义一下
具体看这里吧 http://blog.csdn.net/sunhuwh/article/details/15813103
转载自:http://www.cnblogs.com/sky-sql/p/7145758.html
- spring3以后 <context:property-placeholder>配置资源文件
- <context:property-placeholder>配置资源文件
- spring3配置文件中的context:property-placeholder/元素
- spring3配置文件中的context:property-placeholder/元素
- spring3配置文件中的context:property-placeholder/元素
- spring3的context:property-placeholder详解
- spring3+mybatis 使用MapperScannerConfigurer时context:property-placeholder载不进属性
- spring <context:property-placeholder/> 配置详解
- spring <context:property-placeholder/> 配置详解
- context:property-placeholder junit配置无效解决方法
- spring <context:property-placeholder/> 配置详解
- spring <context:property-placeholder/> 配置详解
- Spring配置<context:property-placeholder>标签解析
- spring 配置<context:property-placeholder/> 作用
- context:property-placeholder
- context:property-placeholder
- context:property-placeholder
- context:property-placeholder
- angularJs 用户添加
- web前端分页
- UVALive 4387 Tower
- 02-Java 技术职称
- 自学web2个月总结
- spring3以后 <context:property-placeholder>配置资源文件
- JavaWeb实现文件上传实例解析
- vmware安装ubuntu16.04网址
- 2017 JUST Programming Contest 4.0
- Hikaricp源码解读(4)——Proxy*代理类介绍
- 前端_添加批量删除查找
- 回溯法
- 关于后台传过来的参数前端接受
- 转载C++如何进阶学习