008,Spring 环境区分

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

V哥学It

赏小的一个钱吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值