Spring注解驱动开发——自动装配 @Pofile根据环境注册bean
Profile:
Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;
开发环境、测试环境、生产环境;
数据源:(/A)(/B)(/C);
@Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件。
1)丶加了环境标识的bean ,只有这个环境被激活的时候才能注册到容器中。默认是default环境
2)丶写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能生效
3)丶没有标注环境标识的bean在,任何环境下都是加载的;前提是整个配置类生效的情况
案例:根据环境动态切换数据源。
pom.xml (mysql 驱动和C3p0数据源 )
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.25</version> </dependency> <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <!-- https://mvnrepository.com/artifact/c3p0/c3p0 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency>
配置类----在方法上标注
package com.mongoubiubiu.conf; import java.beans.PropertyVetoException; import javax.sql.DataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import com.mchange.v2.c3p0.ComboPooledDataSource; /** * Profile: * Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能; * 开发环境、测试环境、生产环境; * 数据源:(/A)(/B)(/C); * * @Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件。 * * 1)丶加了环境标识的bean ,只有这个环境被激活的时候才能注册到容器中。默认是default环境 * @author 86138 * 2)丶写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能生效 * 3)丶没有标注环境标识的bean在,任何环境下都是加载的;前提是整个配置类生效的情况 */ @Configuration public class MainConfigOfProfile { @Profile("sit") @Bean("sitDataSource") public DataSource datasource01() throws PropertyVetoException{ ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource(); comboPooledDataSource.setUser("root"); comboPooledDataSource.setPassword("root"); comboPooledDataSource.setJdbcUrl("jdbc:mysql://kocalhost:3306/test"); comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver"); return comboPooledDataSource; } @Profile("uat") @Bean("uatdDataSource") public DataSource datasource02() throws PropertyVetoException{ ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource(); comboPooledDataSource.setUser("root"); comboPooledDataSource.setPassword("root"); comboPooledDataSource.setJdbcUrl("jdbc:mysql://kocalhost:3306/guli"); comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver"); return comboPooledDataSource; } @Profile("prod") @Bean("prodDataSource") public DataSource datasource03() throws PropertyVetoException{ ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource(); comboPooledDataSource.setUser("root"); comboPooledDataSource.setPassword("root"); comboPooledDataSource.setJdbcUrl("jdbc:mysql://kocalhost:3306/niuke"); comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver"); return comboPooledDataSource; } }
测试类
使用命令行动态参数
package com.mongougbiubiu.test; import javax.sql.DataSource; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.mongoubiubiu.conf.MainConfigOfProfile; public class IOC_Test_dataSource { /** * 1丶使用命令行动态参数:在虚拟机参数位置加载-Dspring.profiles.active=sit */ @Test public void Test01(){ //创建ioc 容器 AnnotationConfigApplicationContext applica= new AnnotationConfigApplicationContext(MainConfigOfProfile.class); //UserService userService=applica.getBean(UserServiceImpl.class); String []str= applica.getBeanNamesForType(DataSource.class); for(String s: str){ System.out.println(s); } System.out.println(applica); } }

测试

确实只注入了 sitDataSource
使用代码
package com.mongougbiubiu.test;
import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.mongoubiubiu.conf.MainConfigOfProfile;
public class IOC_Test_dataSource {
/**
* 1丶使用命令行动态参数:在虚拟机参数位置加载-Dspring.profiles.active=sit
*/
@Test
public void Test01(){
//创建ioc 容器
AnnotationConfigApplicationContext applica= new AnnotationConfigApplicationContext();
//UserService userService=applica.getBean(UserServiceImpl.class);
//设置需要激活的环境
applica.getEnvironment().setActiveProfiles("prod");
//注册主配置类
applica.register(MainConfigOfProfile.class);
//刷新容器
applica.refresh();
String []str= applica.getBeanNamesForType(DataSource.class);
for(String s: str){
System.out.println(s);
}
System.out.println(applica);
}
}
测试发现

prod 环境的数据源被注入进来了
配置类----在类上标注
package com.mongoubiubiu.conf; import java.beans.PropertyVetoException; import javax.sql.DataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import com.mchange.v2.c3p0.ComboPooledDataSource; import com.mongoubiubiu.bean.Car; /** * Profile: * Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能; * 开发环境、测试环境、生产环境; * 数据源:(/A)(/B)(/C); * * @Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件。 * * 1)丶加了环境标识的bean ,只有这个环境被激活的时候才能注册到容器中。默认是default环境 * @author 86138 * 2)丶写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能生效 * 3)丶没有标注环境标识的bean在,任何环境下都是加载的;前提是整个配置类生效的情况 */ @Profile("sit") @Configuration public class MainConfigOfProfile { @Bean public Car car(){ return new Car(); } @Profile("sit") @Bean("sitDataSource") public DataSource datasource01() throws PropertyVetoException{ ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource(); comboPooledDataSource.setUser("root"); comboPooledDataSource.setPassword("root"); comboPooledDataSource.setJdbcUrl("jdbc:mysql://kocalhost:3306/test"); comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver"); return comboPooledDataSource; } @Profile("uat") @Bean("uatdDataSource") public DataSource datasource02() throws PropertyVetoException{ ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource(); comboPooledDataSource.setUser("root"); comboPooledDataSource.setPassword("root"); comboPooledDataSource.setJdbcUrl("jdbc:mysql://kocalhost:3306/guli"); comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver"); return comboPooledDataSource; } @Profile("prod") @Bean("prodDataSource") public DataSource datasource03() throws PropertyVetoException{ ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource(); comboPooledDataSource.setUser("root"); comboPooledDataSource.setPassword("root"); comboPooledDataSource.setJdbcUrl("jdbc:mysql://kocalhost:3306/niuke"); comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver"); return comboPooledDataSource; } }
测试类
package com.mongougbiubiu.test; import javax.sql.DataSource; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.mongoubiubiu.conf.MainConfigOfProfile; public class IOC_Test_dataSource { /** * 1丶使用命令行动态参数:在虚拟机参数位置加载-Dspring.profiles.active=sit */ @Test public void Test01(){ //创建ioc 容器 AnnotationConfigApplicationContext applica= new AnnotationConfigApplicationContext(); //UserService userService=applica.getBean(UserServiceImpl.class); //设置需要激活的环境 applica.getEnvironment().setActiveProfiles("prod"); //注册主配置类 applica.register(MainConfigOfProfile.class); //刷新容器 applica.refresh(); String []str= applica.getBeanNamesForType(DataSource.class); for(String s: str){ System.out.println(s); } System.out.println(applica); } }

由于类上标注的 只在sit 环境下才注入 所以 上面代码 用prod不行
改成 sit 后测试


发现只有没加 @ Profile 的car 和 加了sit 环境的bean 被注入进来了 说明 先按照类 的规则 再按照方法级别的规则 注入bean

浙公网安备 33010602011771号