一、用@Conditional根据条件决定是否要注入bean

1.

package com.habuma.restfun;public class MagicBean {}

2.

package com.habuma.restfun;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;@Configuration
public class MagicConfig {@Bean@Conditional(MagicExistsCondition.class)public MagicBean magicBean() {return new MagicBean();}}

3.

package com.habuma.restfun;import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;public class MagicExistsCondition implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {Environment env = context.getEnvironment();return env.containsProperty("magic");}}

4.

 1 package com.habuma.restfun;
 2
 3 import static org.junit.Assert.*;
 4
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.context.ApplicationContext;
 9 import org.springframework.test.context.ContextConfiguration;
10 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11
12 @RunWith(SpringJUnit4ClassRunner.class)
13 @ContextConfiguration(classes=MagicConfig.class)
14 public class MagicExistsTest {
15
16   @Autowired
17   private ApplicationContext context;
18
19   /*
20    * This test will fail until you set a "magic" property.
21    * You can set this property as an environment variable, a JVM system property, by adding a @BeforeClass
22    * method and calling System.setProperty() or one of several other options.
23    */
24   @Test
25   public void shouldNotBeNull() {
26     assertTrue(context.containsBean("magicBean"));
27   }
28
29 }

二、用@Conditional处理profile

1.

1 @Retention(RetentionPolicy.RUNTIME)
2 @Target({ElementType.TYPE, ElementType.METHOD})
3 @Documented
4 @Conditional(ProfileCondition.class)
5 public @interface Profile {
6     String[] value();
7 }

2.

 1 class ProfileCondition implements Condition {
 2     public boolean matches(
 3         ConditionContext context, AnnotatedTypeMetadata metadata) {
 4         if (context.getEnvironment() != null) {
 5             MultiValueMap < String, Object > attrs =
 6                 metadata.getAllAnnotationAttributes(Profile.class.getName());
 7             if (attrs != null) {
 8                 for (Object value: attrs.get("value")) {
 9                     if (context.getEnvironment()
10                         .acceptsProfiles(((String[]) value))) {
11                         return true;
12                     }
13                 }
14                 return false;
15             }
16         }
17         return true;
18     }
19 }

As you can see, ProfileCondition fetches all the annotation attributes for the
@Profile annotation from AnnotatedTypeMetadata . With that, it checks explicitly for
the value attribute, which contains the name of the bean’s profile. It then consults
with the Environment retrieved from the ConditionContext to see whether the pro-
file is active (by calling the acceptsProfiles() method).

转载于:https://www.cnblogs.com/shamgod/p/5235454.html

SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-003-@Conditional根据条件生成bean及处理profile...相关推荐

  1. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-008-SpEL介绍

    一. 1.SpEL expressions are framed with  #{ ... } 2.SpEl的作用 Sp EL has a lot of tricks up its sleeves, ...

  2. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-008-带参数的ADVICE

    一. 假设有情形如:cd里有很多轨,当播放音乐时,要统计每个音轨的播放次数,这些统计操作不应放在播放方法里,因为统计不是播放音乐的主要职责,这种情况适合应用AOP. 二. 1. package sou ...

  3. SPRING IN ACTION 第4版笔记-第四章Aspect-oriented Spring-001-什么是AOP

    一. Aspect就是把会在应用中的不同地方重复出现的非业务功能的模块化,比如日志.事务.安全.缓存 In software development, functions that span mult ...

  4. 计算机网络第七版笔记--第三章

    计算机网络第七版学习笔记 第三章数据链路层 3.1使用点对点信道的数据链路层 3.1.1数据链路和帧 1.链路(link)就是从一个结点到相邻结点的一段物理线路(有线或无线),而中间没有任何其他的交换 ...

  5. SPRING IN ACTION 第4版笔记-第二章-004-Bean是否单例

    spring的bean默认是单例,加载容器是会被化,spring会拦截其他再次请求bean的操作,返回spring已经创建好的bean. It appears that the CompactDisc ...

  6. SPRING IN ACTION 第4版笔记-第二章-002-@ComponentScan、@Autowired的用法

    一.@ComponentScan 1. @Configuration //说明此类是配置文件 @ComponentScan //开启扫描,会扫描当前类的包及其子包 public class CDPla ...

  7. SPRING IN ACTION 第4版笔记-第二章-001-用@Autowired\@ComponentScan、@Configuration、@Component实现自动装载bean...

    1. 1 package soundsystem; 2 import org.springframework.context.annotation.ComponentScan; 3 import or ...

  8. (第二版)零基础入门Python小甲鱼-笔记-第三章-p5

    (第二版)零基础入门Python小甲鱼-笔记-第三章-p5 变量和字符串(下) 上节课讲了有些字符比如换行符.TAB制表符还有单引号.双引号等等...可以通过转义字符来实现,今天来谈谈原始字符串 1. ...

  9. 机器人导论(第四版)学习笔记——第三章

    机器人导论(第四版)学习笔记--第三章 3 操作臂运动学 3.1 引言 3.2 连杆的描述 3.3 连杆连接的描述 3.4 连杆坐标系的定义 3.5 操作臂运动学 3.6 驱动空间.关节空间和笛卡尔空 ...

最新文章

  1. 理解 : UDID、UUID、IDFA、IDFV
  2. JUC AQS ReentrantLock源码分析
  3. Python安装第三方库太慢?配置好这个速度飞起
  4. 乐山电子计算机职业学院,学校介绍
  5. Angular InjectionToken的一个具体使用例子
  6. python继承语法_python中继承父类的例子(python3的语法)
  7. [转载] 机器学习模型的保存和调用
  8. curl post json_Go Web编程--解析JSON请求和生成JSON响应
  9. NCBI获取指定区域基因序列及其引物设计
  10. MATLAB矩阵及其运算
  11. 开源OA的公文编辑器详解:公文格式和基本使用
  12. 面试字节跳动后台开发(实习)
  13. ccpc网络预选赛总结
  14. 使用windows自带虚拟机---Hyper-V 管理器
  15. excel表格合并程序
  16. NXP i.MX6Q 双屏同显hdmi显示闪烁解决方案
  17. 写简洁java代码的小技巧
  18. Win11找不到gpedit.msc怎么办?Win11无法打开gpedit.msc解决教程
  19. 729. 我的日程安排表 I
  20. 【求助】ipad远程桌面下vmware键盘布局错乱

热门文章

  1. 故障解决:error while loading shared libraries: libncurses.so.5
  2. Spring IOC 组件概述
  3. STM32开发 -- 地球坐标系(WGS84),火星坐标系(GCJ02), 百度坐标系(BD09)坐标转换
  4. UNIX再学习 -- 函数abort
  5. cast函数 oracle 日期_从Oracle到PG 该做的改造工作一个都不能少!
  6. 如何通过Bit-Z的场外交易购买BZ?(新手图文攻略)
  7. Android 代码优化工具FindBugs
  8. Android APP终极瘦身指南
  9. 带通 带阻滤波器 幅频响应_二阶有源带通滤波器设计
  10. I2C和SPI异同及使用注意