1. 程式人生 > ><context:property-placeholder>配置資源文件

<context:property-placeholder>配置資源文件

sys 管理 ssp resource spring 配置 frame emca 屬性 encoding

直接在 spring 配置文件裏面加上

<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}") 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

[email protected],[email protected]cationContext.xml中,這樣在 Controller中是取不到的,必須在dispatcher-servlet.xml 中把獲取屬性文件再定義一下

具體看這裏吧 http://blog.csdn.net/sunhuwh/article/details/15813103

<context:property-placeholder>配置資源文件