PropertyPlaceholderConfigurer
用于Spring 从外部属性文件中载入属性,并使用这些属性值替换Spring 配置文件中的占位符变量(${varible})。
Spring 的ApplicationContext 容器可以非常方便的使用PropertyPlaceholderConfigurer,只需通过简单的配置即可使用,如下prop.xml。
如果需要使用多个配置文件可以使用PropertyPlaceholderConfigurer 的locations属性。
properties文件的中的格式为:
注意别漏了下面这个
Spring 的ApplicationContext 容器可以非常方便的使用PropertyPlaceholderConfigurer,只需通过简单的配置即可使用,如下prop.xml。
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location" value="jdbc.properties" />
- </bean>
如果需要使用多个配置文件可以使用PropertyPlaceholderConfigurer 的locations属性。
- <bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>hello.properties</value>
- <value>welcome.properties</value>
- <value>other.properties</value>
- </list>
- </property>
- </bean>
properties文件的中的格式为:
- datasource.type=oracle
- datasource.driverClassName=oracle.jdbc.driver.OracleDriver
- datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
- #datasource.url=jdbc:oracle:thin:@192.168.1.2:1521:orcl
- datasource.username=cpcim2
- datasource.password=cpcim2
然后在web.xml中配上prop.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:prop.xml
</param-value>
</context-param>
注意别漏了下面这个
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>