Spring @Profile注解详解
@Profile注解详解
@Profile:Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;
开发环境develop、测试环境test、生产环境master
数据源:(/dev)(/test)(/master)
@Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件
1)加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境
2)写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效
packagecom.spring.config; importjava.beans.PropertyVetoException; importjavax.sql.DataSource; importorg.springframework.beans.factory.annotation.Value; importorg.springframework.context.EmbeddedValueResolverAware; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; importorg.springframework.context.annotation.Profile; importorg.springframework.context.annotation.PropertySource; importorg.springframework.util.StringValueResolver; importcom.mchange.v2.c3p0.ComboPooledDataSource; /** *Profile: *Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能; * *开发环境develop、测试环境test、生产环境master *数据源:(/dev)(/test)(/master) * *@Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件 * *1)加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境 *2)写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效 * */ @PropertySource("classpath:/dbconfig.properties") @Configuration publicclassMainConfigOfProfileimplementsEmbeddedValueResolverAware{ @Value("${db.user}") privateStringuser; privateStringdriverClass; @Profile("default") @Bean("test") publicDataSourcetestDataSource(@Value("${db.password}")Stringpassword)throwsPropertyVetoException{ ComboPooledDataSourcedataSource=newComboPooledDataSource(); dataSource.setUser(user); dataSource.setPassword(password); dataSource.setDriverClass(driverClass); returndataSource; } @Profile("dev") @Bean("dev") publicDataSourcedevDataSource(@Value("${db.password}")Stringpassword)throwsPropertyVetoException{ ComboPooledDataSourcedataSource=newComboPooledDataSource(); dataSource.setUser(user); dataSource.setPassword(password); dataSource.setDriverClass(driverClass); returndataSource; } @Profile("master") @Bean("master") publicDataSourcemasterDataSource(@Value("${db.password}")Stringpassword)throwsPropertyVetoException{ ComboPooledDataSourcedataSource=newComboPooledDataSource(); dataSource.setUser(user); dataSource.setPassword(password); dataSource.setDriverClass(driverClass); returndataSource; } publicvoidsetEmbeddedValueResolver(StringValueResolverresolver){ StringdriverClass=resolver.resolveStringValue("${db.driverClass}"); this.driverClass=driverClass; } }
packagecom.spring.test; importjava.util.Arrays; importjavax.sql.DataSource; importorg.junit.Test; importorg.springframework.context.annotation.AnnotationConfigApplicationContext; importcom.spring.config.MainConfigOfProfile; publicclassIOCTestProfile{ //1.使用命令行动态参数:在虚拟机参数位置加载-Dspring.profiles.active=test //2.使用代码的方式激活某种环境; @Test publicvoidtest01(){ AnnotationConfigApplicationContextapplicationContext=newAnnotationConfigApplicationContext(MainConfigOfProfile.class); //1.创建一个applicationContext //2.设置需要激活的环境 applicationContext.getEnvironment().setActiveProfiles("dev","master"); //3.注册主配置类 applicationContext.register(MainConfigOfProfile.class); //4.启动刷新容器 applicationContext.refresh(); String[]beanNamesForType=applicationContext.getBeanNamesForType(DataSource.class); System.out.println(Arrays.toString(beanNamesForType)); applicationContext.close(); } @Test publicvoidtest02(){ AnnotationConfigApplicationContextapplicationContext=newAnnotationConfigApplicationContext(MainConfigOfProfile.class); String[]beanNamesForType=applicationContext.getBeanNamesForType(DataSource.class); System.out.println(Arrays.toString(beanNamesForType)); applicationContext.close(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。