一般在配置數據源是都會使用xml的方式注入,key-value在properties中管理;spring4.X已有着比較完善的註解來替換xml的配置方式。
使用xml配置數據源
通常我們使用xml配置數據源,使用SpEL獲取properties中的配置。
applicationContext.xml 中配置 dataSource 及 PreferencesPlaceholderConfigurer,使用 PropertyPlaceholderConfigurer進行Bean屬性替換
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:/jdbc.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8"/>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties" />
</bean>
<!-- 使用proxool連接池的數據源, -->
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<!-- 數據源的別名 -->
<property name="alias" value="${proxool.alias}" />
<!-- 驅動 -->
<property name="driver" value="${proxool.driver}" />
<!-- 鏈接URL -->
<property name="driverUrl" value="${proxool.driverUrl}" />
<!-- 用戶名-->
<property name="user" value="${proxool.user}" />
<!-- 密碼 -->
<property name="password" value="${proxool.password}" />
<!-- 最大鏈接數-->
<property name="maximumConnectionCount" value="${proxool.maximumConnectionCount}" />
<!-- 最小鏈接數 -->
<property name="minimumConnectionCount" value="${proxool.minimumConnectionCount}" />
<!-- ...(略) -->
</bean>
jdbc.properties
proxool.alias=mySql
proxool.driver=com.mysql.jdbc.Driver
proxool.driverUrl=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
proxool.user=root
proxool.password=root
proxool.maximumActiveTime=1200
proxool.maximumConnectionCount=50
#...
使用javaBean配置數據源
DataSourceConfiguration類是數據源的javaBean配置方式,@Configuratio註解當前類,
spring啓動時會掃描被@Configuratio註解的類,注入當前類中配置的方法bean;
當然別忘了啓用註解掃描:
<context:annotation-config/>
<context:component-scan base-package="com.XXX.test.dateSource"></context:component-scan>
@value註解讀取配置
@value中可以直接使用SpEL,獲取properties配置,成員變量也不需要getter、setter,不過還是有一個前提,需要配置xml:
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:/jdbc.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8"/>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties" />
</bean>
@Bean註解:spring掃面當前類時,注入每個有@Bean註解的方法的返回值Bean, name屬性默認爲返回值類類名首字母小寫,這裏自己設置name。
package com.XXX.test.dateSource;
import org.logicalcobwebs.proxool.ProxoolDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuratio
public class DataSourceConfiguration{
@Value("${proxool.alias}")
private String alias;
@Value("${proxool.driver}")
private String driver;
@Value("${proxool.driverUrl}")
private String driverUrl;
@Value("${proxool.user}")
private String user;
@Value("${proxool.password}")
private String password;
//...
@Bean(name="dataSource")
public ProxoolDataSource dataSource(){
ProxoolDataSource proxoolDataSource = new ProxoolDataSource();
proxoolDataSource.setDriver(driver);
proxoolDataSource.setDriverUrl(driverUrl);
proxoolDataSource.setUser(user);
proxoolDataSource.setPassword(password);
//...
return proxoolDataSource;
}
}
這時dataSource已被注入,使用時可註解注入,如下:
@Autowired
private ProxoolDataSource dataSource;
@PropertySource註解讀取配置
@PropertySource註解當前類,參數爲對應的配置文件路徑,這種方式加載配置文件,可不用在xml中配置PropertiesFactoryBean引入jdbc.properties,使用時方便得多,DataSourceConfiguration不再需要成員變量,取而代之的是需要注入一個Environment環境配置,使用env.getProperty(key)獲取數據:
package com.XXX.test.dateSource;
import org.logicalcobwebs.proxool.ProxoolDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@Configuratio
@PropertySource("classpath:/jdbc.properties")
public class DataSourceConfiguration{
@Autowired
private Environment env;
@Bean(name="dataSource")
public ProxoolDataSource dataSource(){
ProxoolDataSource proxoolDataSource = new ProxoolDataSource();
proxoolDataSource.setDriver(env.getProperty("proxool.alias"));
proxoolDataSource.setDriverUrl(env.getProperty("proxool.driver"));
proxoolDataSource.setUser(env.getProperty("proxool.user"));
proxoolDataSource.setPassword(env.getProperty("proxool.password"));
//...
return proxoolDataSource;
}
}
這裏主要是說明註解的用法,所以沒有具體體現數據源全部參數的配置。對於有強迫症的來說若項目中所有bean都使用註解,幾乎不太希望僅dataSource用xml類配置,換成類的方式類配置強迫感就消失了!