V哥官网:http://www.vgxit.com
本博客对应视频教程:http://www.vgxit.com/course/23
1,概述
在软件开发的过程中,敏捷开发模式很常见,也就是每次都提交一个小阶段的测试,那么可能是开发人员使用一套环境(开发环境),而测试人员使用另一套环境(测试环境),而这两套系统的数据库是不一样的,毕竟测试人员也需要花费很多的时间去构建测试数据,可不想老是被开发人员修改那些测试数据,这样就有了在不同的环境中进行切换的需求了。
2,注解方式区分环境
比如,我们在开发的时候,开发人员连接的数据库和测试人员连接的数据库是分开的。这个时候,我们可以用如下的方式来做
1,首先我们定义两个方法,来获取两个环境不同的数据源:
package com.vgxit.learn.profile.config;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.io.InputStream;
import java.util.Properties;
@ComponentScan(basePackages = "com.vgxit.learn.profile.config")
@Configuration
public class ProfileConfig {
/**
* 获取开发环境的数据源
* @return
*/
@Bean(name = "devDataSource")
public DataSource devDataSource() throws Exception {
Properties properties = new Properties();
InputStream druidInputStram = ProfileConfig.class.getClassLoader().getResourceAsStream("dev.druid.properties");
properties.load(druidInputStram);
DataSource ds = DruidDataSourceFactory.createDataSource(properties);
return ds;
}
/**
* 获取测试环境的数据源
* @return
*/
@Bean(name = "qaDataSource")
public DataSource qaDataSource() throws Exception {
Properties properties = new Properties();
InputStream druidInputStram = ProfileConfig.class.getClassLoader().getResourceAsStream("qa.druid.properties");
properties.load(druidInputStram);
DataSource ds = DruidDataSourceFactory.createDataSource(properties);
return ds;
}
}
2,我们直接测试运行:
package com.vgxit.learn.profile.test;
import com.vgxit.learn.profile.config.ProfileConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import javax.sql.DataSource;
public class ProfileTest {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(ProfileConfig.class);
DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource);
}
}
直接运行,立马报错,这个是因为我们在Spring里面注入了两个DataSource类型的Bean,我们在获取的时候,就不知道应该用哪一个了。这个时候,如果我们是测试环境,我们用devDataSource,如果是qa环境,俺么我们使用qaDataSource。
如果要达到这个效果,首先我们应该给不同的数据源指定不同的环境。
package com.vgxit.learn.profile.config;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import javax.sql.DataSource;
import java.io.InputStream;
import java.util.Properties;
@ComponentScan(basePackages = "com.vgxit.learn.profile.config")
@Configuration
public class ProfileConfig {
/**
* 获取开发环境的数据源
* @return
*/
@Bean(name = "devDataSource")
@Profile("dev")
public DataSource devDataSource() throws Exception {
Properties properties = new Properties();
InputStream druidInputStram = ProfileConfig.class.getClassLoader().getResourceAsStream("dev.druid.properties");
properties.load(druidInputStram);
DataSource ds = DruidDataSourceFactory.createDataSource(properties);
return ds;
}
/**
* 获取测试环境的数据源
* @return
*/
@Bean(name = "qaDataSource")
@Profile("qa")
public DataSource qaDataSource() throws Exception {
Properties properties = new Properties();
InputStream druidInputStram = ProfileConfig.class.getClassLoader().getResourceAsStream("qa.druid.properties");
properties.load(druidInputStram);
DataSource ds = DruidDataSourceFactory.createDataSource(properties);
return ds;
}
}
但是我们继续测试,还是发现报错。这个是因为我们还没有指定我们当前是什么环境。那么我们接下来就要指定环境。
接下来再运行,不报错了。
3,xml的方式来区分环境
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<beans profile="dev">
<bean id="devDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis.ktdm?serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="Abc@123456"/>
<property name="initialSize" value="1"/>
<property name="minIdle" value="1"/>
<property name="maxActive" value="20"/>
</bean>
</beans>
<beans profile="qa">
<bean id="qaDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="Abc@123456"/>
<property name="initialSize" value="1"/>
<property name="minIdle" value="1"/>
<property name="maxActive" value="20"/>
</bean>
</beans>
</beans>