spring注解 @PropertySource 配置数据源

    一般在配置数据源是都会使用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类配置,换成类的方式类配置强迫感就消失了!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值