利用Spring的Profile加載指定數據源


開發、測試、生產都有各自的數據庫,這樣需要配置的數據源不一樣。

每次部署時修改配置過於繁瑣,此時,可以使用Spring的Profile來配置多個數據源,運行前指定需要加載的數據源即可。

采用spring結合junit做演示,使用oracle和mysql做數據源區別演示,Profile名指定為oracle和mysql,junit測試時,使用@ActiveProfiles("mysql"),@ActiveProfiles("oracle")來指定需要加載的數據源

實際工作開發、測試、生產用的同一種數據庫,指定Profile名稱不同,運行是指定需要加載的Profile即可。

在spring配置文件applicationContext.xml中配置oracle和mysql數據源

applicationContext-profile.xml,在其中配置Spring JdbcTemplate,junit測試查詢數據庫用。

    <beans profile="oracle">
        <!-- properties file -->
        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:oracle.properties</value>
                </list>
            </property>
        </bean>
        <!-- 配置連接池 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${oracle.driver}"/>
            <property name="jdbcUrl" value="${oracle.url}"/>
            <property name="user" value="${oracle.username}"/>
            <property name="password" value="${oracle.password}"/>
        </bean>
    </beans>

    <beans profile="mysql">
        <!-- properties file -->
        <!--<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:mysql.properties</value>
                </list>
            </property>
        </bean>-->
        <context:property-placeholder location="classpath:mysql.properties"/>
        <!-- 配置連接池 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${mysql.driver}"/>
            <property name="jdbcUrl" value="${mysql.url}"/>
            <property name="user" value="${mysql.username}"/>
            <property name="password" value="${mysql.password}"/>
        </bean>
    </beans>

    <beans>
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    </beans>

oracle.properties

oracle.driver=oracle.jdbc.driver.OracleDriver
oracle.url=jdbc:oracle:thin:@localhost:1521:orcl
oracle.username=scott
oracle.password=tiger

mysql.properties

mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/test
mysql.username=root
mysql.password=root

OracleProfileTest.java

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.rowset.JdbcRowSet;
import java.util.Map;

/**
 * @ClassName ProfileTest
 * @Description TODO
 * @Author AlphaJunS
 * @Date 2020/4/2 20:06
 * @Version 1.0
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:applicationContext-profile.xml"})
@ActiveProfiles("oracle")
public class OracleProfileTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    public void oracleTest() {
        String sql = "select * from emp where empno = 7900";
        Map<String, Object> map = jdbcTemplate.queryForMap(sql);
        System.out.println(map);
    }

}

 

 

 

MysqlProfileTest.java

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Map;

/**
 * @ClassName MysqlProfileTest
 * @Description TODO
 * @Author AlphaJunS
 * @Date 2020/4/2 20:14
 * @Version 1.0
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:applicationContext-profile.xml"})
@ActiveProfiles("mysql")
public class MysqlProfileTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    public void mysqlTest() {
        String sql = "select * from test where id = 1";
        Map<String, Object> map = jdbcTemplate.queryForMap(sql);
        System.out.println(map);
    }

}

 

 

 

其中配置profile時,可以引入數據庫配置文件的方式有多種。

配置方法1
<
bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:value.properties</value> </list> </property> </bean>
配置方法2
<util:properties id="configProperties" location="classpath:value.properties"></util:properties>  
配置方式3
<context:property-placeholder location="classpath:mysql.properties"/>

激活指定Profile

junit使用@ActiveProfiles("oracle")

web.xml中配置初始化參數使用spring.profiles.default、spring.profiles.active

 

 參考帖子:

spring Profile

使用PropertyPlaceholderConfigurer指定配置文件和@Value 注解獲取properties值


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM