spring-@Profile源码跟踪
来源:互联网 发布:计算机恶意软件 编辑:程序博客网 时间:2024/06/08 14:43
@Profile标签可以让你在不同的环境切换bean。
@Profile也是一个条件化配置,因此,一个被@Profile注解的bean最后是能否注册到上下文由ProfileCondition.class决定。
package org.springframework.context.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({ElementType.TYPE, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documented@Conditional({ProfileCondition.class})public @interface Profile { String[] value();}
class ProfileCondition implements Condition { ProfileCondition() { } public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { if(context.getEnvironment() != null) {//环境 MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());//获取当前被@Profile注解的@Profile的value值。 if(attrs != null) { Iterator var4 = ((List)attrs.get("value")).iterator(); Object value; do { if(!var4.hasNext()) {//如果最后都没有找到对应的值,返回false,不需要注册当前被@Profile注解的bean return false; } value = var4.next();//下一个bean } while(!context.getEnvironment().acceptsProfiles((String[])((String[])value)));//@Profile的值是否符合当前环境,是的话跳出循环
//不是的话继续next。 return true;//注册当前bean } } return true; }}
public interface DatabaseConfig { DataSource createDataSource();}
开发环境配置
@Configuration@Profile(value = "dev")public class DevDatabaseConfig implements DatabaseConfig{ @Bean public DataSource createDataSource() { System.out.println("create dev database.."); DriverManagerDataSource dataSource = new DriverManagerDataSource(); return dataSource; }}
线上环境配置
@Configuration@Profile(value = "pro")public class ProductionDatabaseConfig implements DatabaseConfig { @Bean public DataSource createDataSource() { System.out.println("create pro database"); DriverManagerDataSource dataSource = new DriverManagerDataSource(); return dataSource; }}
测试代码
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.getEnvironment().setActiveProfiles("pro");//设置环境 context.scan("com.*"); context.refresh(); context.close();
DevDatabaseConfig
ProductionDatabaseConfig
输出
org.springframework.core.type.classreading.MethodMetadataReadingVisitor
create pro database
设置了@Profile之后需要激活,激活访问有好几种,比如jvm,spring设置等等。
参考
http://blog.csdn.net/hj7jay/article/details/53634159
https://www.cnblogs.com/chenpi/p/6213849.html
https://www.cnblogs.com/jeffen/p/6136371.html
阅读全文
0 0
- spring-@Profile源码跟踪
- 跟踪Spring源码(一)
- spring启动过程之源码跟踪(上)--spring Debug
- spring启动过程之源码跟踪(中)--spring Debug
- spring启动过程之源码跟踪(下)--spring Debug
- spring启动过程之源码跟踪(下)--spring Debug
- spring启动过程之源码跟踪(上)--spring Debug
- Spring profile
- spring profile
- spring profile
- 转载:Spring 中 PropertySource、Environment、Profile 源码解析
- Spring MVC 中重定向 及 重定向源码跟踪
- Maven profile整合Spring profile
- Maven profile整合Spring profile
- spring学习--spring profile
- spring源码附录(2)spring profile属性的简单使用
- spring启动过程之源码跟踪(续beanfactory)--spring Debug
- spring启动过程之源码跟踪(小结bean的生命周期)--spring Debug
- freopen()函数的使用
- 仿苹果音量调节柱
- vue组件与route通信
- C语言基础篇(一)
- 【Java】使用visualvm远程监控tomcat
- spring-@Profile源码跟踪
- java 后台模拟post表单请求发送key-value数据乱码
- idea打印gc信息及其简单概述
- Design 四: CoordinatorLayout + AppBarLayout+ Toolbar+NestedScrollView
- 常用工具记录
- # 程序员心理——大三写pj+lab有感
- SeamlessClone泊松克隆
- 【阿里聚安全·安全周刊】IPv6安全防护准备好了吗?| 苹果的 iOS安全性已经今非昔比
- Neo4j_高层架构和应用