Spring注解开发04--------环境切换

概述

我们在开发过程中,使用的往往不是一个环境,一般分为开发环境(dev),测试环境(test)以及线上环境(prd),我们如何在不修改代码的情况下实现这些环境之间的自由切换呢?

答:Spring为我们提供了可以根据当前环境,动态激活与切换一些列组件的功能。

@Profile注解的使用

在Spring中,我们可以通过@Profile注解来实现环境的切换,在这里我们以mysql数据源连接为例。

测试

  1. maven相关依赖

    <!-- Spring核心依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.5</version>
    </dependency>
    
    <!-- junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>
    
    <!-- 支持JSR330规范 -->
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    
    <!-- c3p0数据库连接池 -->
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>
    </dependency>
    <!-- 数据库连接驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.16</version>
    </dependency>
    
  2. 编写配置类

    @Configuration
    public class MainConfigOfProfile {
    
    
        /**
         * 测试环境数据源
         * @return
         * @throws Exception
         */
        @Profile("test")
        @Bean
        public ComboPooledDataSource testDataSource() throws Exception {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
            dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
            dataSource.setUser("root");
            dataSource.setPassword("111111");
            return dataSource;
        }
    
        /**
         * 开发环境数据源
         * @return
         * @throws Exception
         */
        @Profile("dev")
        @Bean
        public ComboPooledDataSource devDataSource() throws Exception {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
            dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
            dataSource.setUser("root");
            dataSource.setPassword("1111111");
            return dataSource;
        }
    
        /**
         * 生产环境数据源
         * @return
         * @throws Exception
         */
        @Profile("prd")
        @Bean
        public ComboPooledDataSource prdDataSource() throws Exception {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
            dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
            dataSource.setUser("root");
            dataSource.setPassword("111111");
            return dataSource;
        }
    }
    

    这里,我们为每个bean都加上了@Profile注解,注解的value值为对应的环境名称

  3. 编写测试方法

    @Test
    public void testProfile() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfProfile.class);
        String[] names = applicationContext.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }
    }
    
  4. 在idea中将测试方法对应的环境变量改为dev

    修改环境变量方法
    在Vm options中输入:-Dspring.profiles.active=dev,dev表示开发环境,你也可以修改为test或者prd,但 是修改的名字要和配置类中设置的@Profile的value值一致!

  5. 运行测试,结果如下:
    测试结果

我们的dev成功加载进了ioc容器中。

总结

Profile注解说明:

  • @Profile标注的bean,在指定环境下才会被注入到容器中,不标注该注解,则在任何环境中都能注入到容器中。
  • 这个注解也能放在配置类上,放在配置类上,这又这个环境被激活时,整个配置类中的配置才会生效

环境激活方法:

  1. 使用命令行参数: 在虚拟机参数位置加上-Dspring.profiles.active=test(即我们测试使用的方法)

  2. 也就可以通过代码实现:
    a.创建一个applicationContext
    b.设置需要激活的环境
    c.注册主配置类
    d.启动刷新容器。具体代码如下:

    @Test
    public void testProfile02() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    
        StandardEnvironment environment = new StandardEnvironment();
        environment.setActiveProfiles("test");
        applicationContext.setEnvironment(environment);
    
        applicationContext.register(MainConfigOfProfile.class);
        applicationContext.refresh();
    
        String[] names = applicationContext.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }
    
    }
    
posted @   卧龙戏公瑾  阅读(54)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
点击右上角即可分享
微信分享提示