@Profile


Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能。
指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件

案例:根据不同的环境注册不同的数据源信息

源码实现

配置类

//@Profile("test")
@PropertySource("classpath:/dbconfig.properties")
@Configuration
public class MainConfigOfProfile implements EmbeddedValueResolverAware {

    @Value("${db.user}")
    private String user;

    private StringValueResolver valueResolver;

    private String driverClass;

    /**
     * 没有标注环境标识的bean在,任何环境下都是加载的
     */
    @Bean
    public Yellow yellow() {
        return new Yellow();
    }


    //    @Profile("default")
    @Profile("test")
    @Bean("testDataSource")
    public DataSource dataSourceTest(@Value("${db.password}") String pwd) throws Exception {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(pwd);
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/crm_00");
        dataSource.setDriverClass(driverClass);
        return dataSource;
    }

    @Profile("pre")
    @Bean("preDataSource")
    public DataSource dataSourcePre(@Value("${db.password}") String pwd) throws Exception {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(pwd);
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/care");
        dataSource.setDriverClass(driverClass);
        return dataSource;
    }

    @Profile("prd")
    @Bean("prdDataSource")
    public DataSource dataSourcePrd(@Value("${db.password}") String pwd) throws Exception {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(pwd);
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mysql");
        dataSource.setDriverClass(driverClass);
        return dataSource;
    }

    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        //值解析器
        this.valueResolver = resolver;
    }

    private String getDriverClassValue() {
        return valueResolver.resolveStringValue("${db.driverClass}");
    }
}

@Data
public class Yellow {
}

配置 dbconfig.properties

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

测试

public class IOCTest_Profile {

    /**
     * @param
     * @return void
     * @Description: 切环境
     * 1:使用命令行动态参数: 在虚拟机参数位置加载 -Dspring.profiles.active=test
     * 2:代码的方式激活某种环境
     * @author luoyong
     * @create 22:18 2019/12/30
     * @last modify by [LuoYong 22:18 2019/12/30 ]
     */
    @Test
    public void test() {
        //1:创建一个ApplicationContext
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //2:设置需要激活的环境
//        applicationContext.getEnvironment().setActiveProfiles("test", "pre", "prd");
        applicationContext.getEnvironment().setActiveProfiles("test");
        //3:注册主配置类
        applicationContext.register(MainConfigOfProfile.class);
        //4:启动刷新容器
        applicationContext.refresh();
//        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfProfile.class);
        String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
        Arrays.stream(beanNamesForType).forEach(System.out::println);

        Yellow yellow = applicationContext.getBean(Yellow.class);
        System.out.println(yellow);

        applicationContext.close();
    }
}

测试结果:
在这里插入图片描述

小结

1. 加 了 环 境 标 识 的 b e a n , 只 有 这 个 环 境 被 激 活 的 时 候 才 能 注 册 到 容 器 中 。 默 认 是 d e f a u l t 环 境 \color{blue}{1. 加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境} 1.beandefault
2. 写 在 配 置 类 上 , 只 有 是 指 定 的 环 境 的 时 候 , 整 个 配 置 类 里 面 的 所 有 配 置 才 能 开 始 生 效 \color{blue}{ 2.写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效} 2.
3. 没 有 标 注 环 境 标 识 的 b e a n 在 , 任 何 环 境 下 都 是 加 载 的 \color{blue}{ 3.没有标注环境标识的bean在,任何环境下都是加载的} 3.bean

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值