spring里加入properties配置

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());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值