Spring学习笔记(四) DataSource四种配置方法


第一种:beans.xml

 

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
    destroy-method="close">  
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />  
    <property name="url"  
        value="jdbc:sqlserver://localhost:1433;DatabaseName=spring" />  
    <property name="username" value="sa" />  
    <property name="password" value="********" />  
</bean>  

 

第二种:beans.xml
     

   <bean id="mappings" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   
          <property name="locations" value="classpath:jdbc.properties"></property>   
     </bean>   
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"   destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />  
        <property name="url" value="${jdbc.url}" />  
        <property name="username" value="${jdbc.username}" />  
        <property name="password" value="${jdbc.password}" />  
    </bean>  

 

在src文件夹里新建一个jdbc.properties文件,里面的内容为如下:

 

jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver  
jdbcjdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=spring  
jdbc.username=sa  
jdbc.password=********  

 

 

 

第三种: beans.xml

 

 <bean id="mappings"   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   
           <property name="locations" value="classpath:jdbc.properties"></property>   
 </bean>   
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
      <property name="driverClassName" value="${jdbc.driverClassName}" />  
      <property name="url" value="${jdbc.url}" />  
      <property name="username" value="${jdbc.username}" />  
      <property name="password" value="${jdbc.password}" />  
 </bean>  
  
 <context:property-placeholder location="classpath:jdbc.properties" />

 

在src文件夹里新建一个jdbc.properties文件,里面的内容为如下:

 

jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver  
jdbcjdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=spring  
jdbc.username=sa  
jdbc.password=********  

 

第四种: beans.xml

 

<bean id="mappings" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
 <!-- typed as a java.util.Properties -->  
 <property name="properties">  
    <value>  
    	jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver  
    	jdbcjdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=spring  
    	jdbc.username=sa  
    	jdbc.password=********  
    </value>  
 </property>  
</bean>  
  
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
     <property name="driverClassName" value="${jdbc.driverClassName}" />  
     <property name="url" value="${jdbc.url}" />  
     <property name="username" value="${jdbc.username}" />  
     <property name="password" value="${jdbc.password}" />  
</bean>  

 

你可能感兴趣的:(spring,dataSource)