多语言展示
当前在线:1628今日阅读:147今日分享:48

使用@Profile注解根据不同环境注册bean

使用@Profile注解根据不同环境注册bean
工具/原料
1

Spring

2

Eclipse

方法/步骤
1

@Profile可以根据不同的环境下注册到不同的容器中。现在配置三个不同环境下不同的profile配置:package com.gwolf.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;@Configuration@PropertySource('classpath:dbconfig.properties')public class MainConfigOfProfile {                @Value('${jdbc.user}')        private String user;                @Profile('test')        @Bean('dataSourceTest')        public DataSource dataSourceTest(@Value('${jdbc.password}') String password)  throws Exception {                ComboPooledDataSource dataSource = new ComboPooledDataSource();                dataSource.setUser(user);                dataSource.setPassword(password);                dataSource.setJdbcUrl('jdbc:mysql://localhost:3306/ssm_crud');                dataSource.setDriverClass('com.mysql.jdbc.Driver');                return dataSource;        }                @Profile('dev')        @Bean('dataSourceDev')        public DataSource dataSourceDev() throws Exception {                ComboPooledDataSource dataSource = new ComboPooledDataSource();                dataSource.setUser('root');                dataSource.setPassword('root');                dataSource.setJdbcUrl('jdbc:mysql://localhost:3307/ssm_crud');                dataSource.setDriverClass('com.mysql.jdbc.Driver');                return dataSource;        }                @Profile('product')        @Bean('dataSourceProduct')        public DataSource dataSourceProduct() throws Exception {                ComboPooledDataSource dataSource = new ComboPooledDataSource();                dataSource.setUser('root');                dataSource.setPassword('root');                dataSource.setJdbcUrl('jdbc:mysql://localhost:3308/ssm_crud');                dataSource.setDriverClass('com.mysql.jdbc.Driver');                return dataSource;        }}

2

加了环境的标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境。现在运行junit测试类,打印出所有已经注册到容器中的组件。

3

从打印中可以看出没有注册任何的数据源。根据其中一个数据源为default。这样就打印出了一个数据源。

4

切换到不同的环境可以使用运行参数:-Dspring.profiles.active=test。

5

也可以使用代码的方式来切换不同的环境。package com.gwolf.test;import org.junit.Test;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import com.gwolf.config.MainConfigOfProfile;public class ComponentTest {                @Test        public void testImport() {                AnnotationConfigApplicationContext applicationContext =                                 new AnnotationConfigApplicationContext();                                applicationContext.getEnvironment().setActiveProfiles('test');                                applicationContext.register(MainConfigOfProfile.class);                                applicationContext.refresh();                                String[] beanNames = applicationContext.getBeanDefinitionNames();                                for(String beanName:beanNames) {                        System.out.println(beanName);                }                        }}

6

@Profile注解也可以写在配置类上,只有是指定的环境的时候,整个配置类里面所有的配置才能开始生效。

推荐信息