Spring注解驱动开发 第十六节@Profile的使用

Spring注解驱动开发 第十六节@Profile的使用

要使用@Profile注解,首先需要搭建一下适应的环境。

@SuppressWarnings("all")
@Configuration
@PropertySource("classpath:/DBSource.properties")
//@ComponentScan({"com.meng.beanfactory"})
public class MainConfig6 implements EmbeddedValueResolverAware {
//    @Primary
//    @Bean("bookDao2")
//    public BookDao bookDao(){
//        BookDao bookDao = new BookDao();
//        bookDao.setLabel("2");
//        return bookDao;
//    }
    private StringValueResolver resolver;
    @Bean
    public DataSource test(){
        ComboPooledDataSource pool = null;
        try {
            pool = new ComboPooledDataSource();
            pool.setUser("${username}");
            pool.setPassword("");
            pool.setJdbcUrl("jdbc:mysql://localhost:3306/ibatis");
            pool.setDriverClass(driverClass);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        return  pool;
    }

    @Bean
    public DataSource dev(){
        ComboPooledDataSource pool = null;
        try {
            pool = new ComboPooledDataSource();
            pool.setUser("${username}");
            pool.setPassword("");
            pool.setJdbcUrl("jdbc:mysql://localhost:3306/test");
            pool.setDriverClass(driverClass);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        return  pool;
    }

    @Bean
    public DataSource prod(){
        ComboPooledDataSource pool = null;
        try {
            pool = new ComboPooledDataSource();
            pool.setUser("${username}");
            pool.setPassword("");
            pool.setJdbcUrl("jdbc:mysql://localhost:3306/mysql");
            pool.setDriverClass(driverClass);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        return  pool;
    }
    private String driverClass;
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        this.resolver = resolver;
        driverClass = resolver.resolveStringValue("${driverClass}");
    }
}

配置文件DBSource.properties

driverClass=com.mysql.jdbc.Driver
username=root
url=jdbc:mysql://localhost:3306/test

这里我采用了多种方式获取值,顺便复习一下,我在配置文件里建立了三个bean,都是连接数据库的,但是引入值的方式我用了两种,setUser我是用的是配置文件的方式引入,driverClass我使用spring的核心组件获取字符串占位符组件进行操作,其实第二种方式也是从配置文件中取值。
接下来我们使用@Profile注解

@SuppressWarnings("all")
@Configuration
@PropertySource("classpath:/DBSource.properties")
//@ComponentScan({"com.meng.beanfactory"})
public class MainConfig6 implements EmbeddedValueResolverAware {
//    @Primary
//    @Bean("bookDao2")
//    public BookDao bookDao(){
//        BookDao bookDao = new BookDao();
//        bookDao.setLabel("2");
//        return bookDao;
//    }
    private StringValueResolver resolver;
    //加了环境标识的bean,只有这个环境被激活的时候才能注入到容器中,默认是default
    @Profile("test")
    @Bean
    public DataSource test(){
        ComboPooledDataSource pool = null;
        try {
            pool = new ComboPooledDataSource();
            pool.setUser("${username}");
            pool.setPassword("");
            pool.setJdbcUrl("jdbc:mysql://localhost:3306/ibatis");
            pool.setDriverClass(driverClass);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        return  pool;
    }
    @Profile("dev")
    @Bean
    public DataSource dev(){
        ComboPooledDataSource pool = null;
        try {
            pool = new ComboPooledDataSource();
            pool.setUser("${username}");
            pool.setPassword("");
            pool.setJdbcUrl("jdbc:mysql://localhost:3306/test");
            pool.setDriverClass(driverClass);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        return  pool;
    }
    @Profile("prod")
    @Bean
    public DataSource prod(){
        ComboPooledDataSource pool = null;
        try {
            pool = new ComboPooledDataSource();
            pool.setUser("${username}");
            pool.setPassword("");
            pool.setJdbcUrl("jdbc:mysql://localhost:3306/mysql");
            pool.setDriverClass(driverClass);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        return  pool;
    }
    private String driverClass;
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        this.resolver = resolver;
        driverClass = resolver.resolveStringValue("${driverClass}");
    }
}

我们在三个组件上面加了@Profile注解,并标注了他们的名称,表示他们在允许的情况下被加载到spring容器中。
在这里插入图片描述
给jvm设置参数,指定当前环境是test,查看打印结果。

org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig6
test

Process finished with exit code 0

test被加载进来了,下面我们指定dev

org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig6
dev

Process finished with exit code 0

dev又被加载进来了。
第一种是修改jvm虚拟机参数修改环境,现在我们要在代码中修改环境。

	@Test
    public void test03(){
        //启动spring
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        //设定当前环境
        context.getEnvironment().setActiveProfiles("test","dev");
        //加载配置文件
        context.register(MainConfig6.class);
        //刷新spring容器
        context.refresh();
        String[] names = context.getBeanDefinitionNames();
        for(String name:names){
            System.out.println(name);
        }
    }

上面代码已经详细说明了他们的功能,我们查看打印结果:

org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig6
test
dev

Process finished with exit code 0

可以看到test和dev注入进来了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值