17、spring注解学习(自动装配)——@Profile根据当前环境,动态的激活和切换一系列组件的功能
@Profile:
Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;
开发环境develop、测试环境test、生产环境master
数据源:(/dev) (/test) (/master)
@Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件
1) 加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境
2) 写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效
package com.spring.config; import java.beans.PropertyVetoException; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.EmbeddedValueResolverAware; 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 org.springframework.util.StringValueResolver; import com.mchange.v2.c3p0.ComboPooledDataSource; /** * Profile: * Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能; * * 开发环境develop、测试环境test、生产环境master * 数据源:(/dev) (/test) (/master) * * @Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件 * * 1) 加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境 * 2) 写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效 * */ @PropertySource("classpath:/dbconfig.properties") @Configuration public class MainConfigOfProfile implements EmbeddedValueResolverAware{ @Value("${db.user}") private String user; private String driverClass; @Profile("default") @Bean("test") public DataSource testDataSource(@Value("${db.password}")String password) throws PropertyVetoException { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setUser(user); dataSource.setPassword(password); dataSource.setDriverClass(driverClass); return dataSource; } @Profile("dev") @Bean("dev") public DataSource devDataSource(@Value("${db.password}")String password) throws PropertyVetoException { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setUser(user); dataSource.setPassword(password); dataSource.setDriverClass(driverClass); return dataSource; } @Profile("master") @Bean("master") public DataSource masterDataSource(@Value("${db.password}")String password) throws PropertyVetoException { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setUser(user); dataSource.setPassword(password); dataSource.setDriverClass(driverClass); return dataSource; } public void setEmbeddedValueResolver(StringValueResolver resolver) { String driverClass = resolver.resolveStringValue("${db.driverClass}"); this.driverClass = driverClass; } }
package com.spring.test; import java.util.Arrays; import javax.sql.DataSource; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.spring.config.MainConfigOfProfile; public class IOCTestProfile { //1. 使用命令行动态参数:在虚拟机参数位置加载 -Dspring.profiles.active=test //2. 使用代码的方式激活某种环境; @Test public void test01() { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(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 public void test02() { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfProfile.class); String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class); System.out.println(Arrays.toString(beanNamesForType)); applicationContext.close(); } }
分类:
Spring
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)