spring里加入properties配置
直接例子了,在list里面可以加入多个properties配置:
Java代码
<bean id="jdbcConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${jdbc.maxActive}" />
<property name="maxIdle" value="${jdbc.maxIdle}" />
<property name="minIdle" value="${jdbc.minIdle}" />
<property name="initialSize" value="${jdbc.initialSize}" />
<property name="validationQuery" value="${jdbc.validationQuery}" />
<property name="testOnBorrow" value="${jdbc.testOnBorrow}" />
<property name="validationQueryTimeout" value="${jdbc.validationQueryTimeout}" />
</bean>
<bean id="jdbcConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${jdbc.maxActive}" />
<property name="maxIdle" value="${jdbc.maxIdle}" />
<property name="minIdle" value="${jdbc.minIdle}" />
<property name="initialSize" value="${jdbc.initialSize}" />
<property name="validationQuery" value="${jdbc.validationQuery}" />
<property name="testOnBorrow" value="${jdbc.testOnBorrow}" />
<property name="validationQueryTimeout" value="${jdbc.validationQueryTimeout}" />
</bean>
properties配置如下:
Java代码
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:1433/mmusic
jdbc.username=root
jdbc.password=
jdbc.maxActive=20
jdbc.maxIdle=15
jdbc.minIdle=10
jdbc.initialSize=15
jdbc.testOnBorrow=true
jdbc.validationQuery=select 1
jdbc.validationQueryTimeout=20
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:1433/mmusic
jdbc.username=root
jdbc.password=
jdbc.maxActive=20
jdbc.maxIdle=15
jdbc.minIdle=10
jdbc.initialSize=15
jdbc.testOnBorrow=true
jdbc.validationQuery=select 1
jdbc.validationQueryTimeout=20
还有一种配置:
Java代码
< beans>
< bean id="configproperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
< property name="location" value="file:config.properties"/>
< /bean>
< bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
< property name="properties" ref="configproperties"/>
< /bean>
3.Config.java
package com.starxing.test;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class Config {
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"com/starxing/test/conf.xml"));
// 如果要在BeanFactory中使用,bean factory post-processor必须手动运行:
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource(
"com/starxing/test/jdbc.properties"));
cfg.postProcessBeanFactory(factory);
DriverManagerDataSource dataSource = (DriverManagerDataSource) factory
.getBean("dataSource");
System.out.println(dataSource.getDriverClassName());
// 注意,ApplicationContext能够自动辨认和应用在其上部署的实现了BeanFactoryPostProcessor的bean。这就意味着,当使用ApplicationContext的时候应用PropertyPlaceholderConfigurer会非常的方便。由于这个原因,建议想要使用这个或者其他bean
// factory postprocessor的用户使用ApplicationContext代替BeanFactroy。
ApplicationContext context = new ClassPathXmlApplicationContext(
"com/starxing/test/conf.xml");
DriverManagerDataSource dataSource2 = (DriverManagerDataSource) context
.getBean("dataSource");
System.out.println(dataSource2.getDriverClassName());
}
}