zoukankan      html  css  js  c++  java
  • @Profile 动态切换环境

    一、dbconfig.properties

    db.user=root
    db.password=123456
    db.driverClass=com.mysql.jdbc.Driver

    二、配置类

    package com.config;
    
    import javax.sql.DataSource;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Profile;
    import org.springframework.context.annotation.PropertySource;
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    
    /**
     * Profile:
     *         Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;
     * 
     * 开发环境、测试环境、生产环境的数据源分别为 dev、test、prod
     * 
     * 
     * @Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件
     * 
     * 1)、加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境
     * 2)、写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效
     * 3)、没有标注环境标识的bean在,任何环境下都是加载的;
     */
    
    @PropertySource("classpath:/dbconfig.properties")
    @Configuration
    public class MainConfigOfProfile{
        
        @Value("${db.user}")
        private String user;
        
        @Value("${db.pwd}")
        private String pwd;
        
        @Value("${db.driverClass}")
        private String driverClass;
        
        
        //测试环境的数据源
        @Profile("test") //标注环境表标识 test ( 测试环境 )
        @Bean("testDataSource")
        public DataSource dataSourceTest() throws Exception{
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setUser(user);
            dataSource.setPassword(pwd);
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
            dataSource.setDriverClass(driverClass);
            return dataSource;
        }
        
        //开发环境的数据源
        @Profile("dev") //标注环境标识为 dev  ( 开发环境 )
        @Bean("devDataSource")
        public DataSource dataSourceDev() throws Exception{
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setUser(user);
            dataSource.setPassword(pwd);
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/dev");
            dataSource.setDriverClass(driverClass);
            return dataSource;
        }
        
        //生产环境的数据源
        @Profile("prod") //标注环境标识为 prod ( 生产环境 )
        @Bean("prodDataSource")
        public DataSource dataSourceProd() throws Exception{
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setUser(user);
            dataSource.setPassword(pwd);
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/prod");
            dataSource.setDriverClass(driverClass);
            return dataSource;
        }
    
    }

    三、测试代码

    public class IOCTest_Profile {
        //使指定环境生效的两种方法如下
        //1、使用命令行动态参数: 在虚拟机参数位置加载 -Dspring.profiles.active=test
        //2、代码的方式激活某种环境;
        @Test
        public void test01(){
            //1、创建一个applicationContext
            AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();       
            //2、设置需要激活的环境
            applicationContext.getEnvironment().setActiveProfiles("dev");
            //3、注册主配置类
            applicationContext.register(MainConfigOfProfile.class);
            //4、启动刷新容器
            applicationContext.refresh();
            
            String[] namesForType = applicationContext.getBeanNamesForType(DataSource.class);
            for (String string : namesForType) {
                System.out.println(string);
            }
                    
            applicationContext.close();
        }
    
    }
  • 相关阅读:
    解压tar.gz文件报错gzip: stdin: not in gzip format解决方法
    通过 HTTP 头进行 SQL 注入(转)
    Android涉及到的设计模式(转)
    初探Java8中的HashMap(转)
    substance的使用示例(转)
    大数据量下高并发同步的讲解(不看,保证你后悔)(转)
    HDU 4812 D Tree 树分区+逆+hash新位置
    在weblogic11g发布该项目时遇到错误(不支持web-app_3_0)
    highchart几个图表马金摘要
    android 使用 service 实现音乐
  • 原文地址:https://www.cnblogs.com/fangwu/p/8687872.html
Copyright © 2011-2022 走看看