一灰灰blog 一灰灰blog
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)

一灰灰blog

资深搬运工
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)
  • 基础系列

    • 配置

    • AOP

    • Bean

      • 【基础系列】Bean之基本定义与使用
      • 【基础系列】Bean之自动加载
      • 【基础系列】Bean之动态注册
      • 【基础系列】Bean之注销与动态注册实现服务mock(应用篇)
      • 【基础系列】Bean之条件注入@Condition使用姿势
      • 【基础系列】Bean之@ConditionalOnBean与@ConditionalOnClass
      • 【基础系列】Bean之条件注入@ConditionalOnExpression
        • IV. 表达式条件注入
          • 1. @ConditionalOnExpression
          • 2. 实例测试
        • II. 其他
          • 0. 相关
          • 1. 一灰灰Blog
          • 2. 声明
          • 3. 扫描关注
      • 【基础系列】Bean之条件注入@ConditionalOnProperty
      • 【基础系列】Bean之多实例选择
      • 【基础系列】FactoryBean及代理实现SPI机制的实例(应用篇)
      • 【配置系列】Bean加载顺序之错误使用姿势辟谣
      • 【基础系列】指定Bean初始化顺序的若干姿势
      • 【基础系列】从0到1实现一个自定义Bean注册器(应用篇)
      • 【基础系列】自动配置选择生效
      • 【基础系列-实战】如何指定bean最先加载(应用篇)
      • 【基础系列】实现一个自定义的@Autowired(应用篇)
      • 【基础系列】SpringContext.getBean()方法调用导致NPE?
    • SpEL

    • 事件

    • 国际化

    • 定时器

    • 日志

  • DB系列

  • 搜索系列

  • MQ系列

  • WEB系列

  • 中间件

  • 运维

  • SpringSecurity

  • SpringCloud

  • Spring系列
  • 基础系列
  • Bean
一灰灰
2018-10-19

【基础系列】Bean之条件注入@ConditionalOnExpression

bean的条件注入,除了前面几篇博文分别介绍的通过@Conditional注解配合Condition接口的基本实现,以及如何使用条件注解@ConditionalOnBean和@ConditionalOnClass和基于配置的@ConditionalOnProperty

本文介绍的注解将更加的灵活,基于SPEL表达式的条件注解ConditionalOnExpression

# IV. 表达式条件注入

相比较前面的Bean,Class是否存在,配置参数是否存在或者有某个值而言,这个依赖SPEL表达式的,就显得更加的高级了;其主要就是执行Spel表达式,根据返回的true/false来判断是否满足条件

至于SPEL是什么东西,后面会有专文进行解释,此处不加以展开。下面以一个简单的demo进行演示它的使用姿势

# 1. @ConditionalOnExpression

接口定义

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnExpressionCondition.class)
public @interface ConditionalOnExpression {

	/**
	 * The SpEL expression to evaluate. Expression should return {@code true} if the
	 * condition passes or {@code false} if it fails.
	 * @return the SpEL expression
	 */
	String value() default "true";
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 2. 实例测试

用一个简单的例子,当配置参数中,根据是否满足某个条件来决定是否需要加载bean

# a. 测试用例

定义一个满足条件和一个不满足的bean

public class ExpressFalseBean {
    private String name;

    public ExpressFalseBean(String name) {
        this.name = name;
    }

    public String getName() {
        return "express bean :" + name;
    }
}

public class ExpressTrueBean {
    private String name;

    public ExpressTrueBean(String name) {
        this.name = name;
    }

    public String getName() {
        return "express bean :" + name;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

重点关注下bean的配置

@Configuration
public class ExpressAutoConfig {
    /**
     * 当存在配置,且配置为true时才创建这个bean
     * @return
     */
    @Bean
    @ConditionalOnExpression("#{'true'.equals(environment['conditional.express'])}")
    public ExpressTrueBean expressTrueBean() {
        return new ExpressTrueBean("express true");
    }

    /**
     * 配置不存在,或配置的值不是true时,才创建bean
     * @return
     */
    @Bean
    @ConditionalOnExpression("#{!'true'.equals(environment.getProperty('conditional.express'))}")
    public ExpressFalseBean expressFalseBean() {
        return new ExpressFalseBean("express != true");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

对应的配置如下

conditional.express=true
1

# b. 实例演示

@RestController
@RequestMapping(path = "express")
public class ExpressRest {
    @Autowired(required = false)
    private ExpressTrueBean expressTrueBean;
    @Autowired(required = false)
    private ExpressFalseBean expressFalseBean;

    @GetMapping(path = "show")
    public String show() {
        Map<String, String> result = new HashMap<>(4);
        result.put("expressTrueBean", expressTrueBean == null ? "null ==> false" : expressTrueBean.getName());
        result.put("expressFalseBean", expressFalseBean == null ? "null ==> true": expressFalseBean.getName());
        return JSONObject.toJSONString(result);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

上面的执行,expressTrueBean应该存在,另外一个为null,运行结果如下

gif.gif

# II. 其他

# 0. 相关

# a. 更多博文

基础篇

  • 181009-SpringBoot基础篇Bean之基本定义与使用 (opens new window)
  • 181012-SpringBoot基础篇Bean之自动加载 (opens new window)
  • 181013-SpringBoot基础篇Bean之动态注册 (opens new window)
  • 181018-SpringBoot基础篇Bean之条件注入@Condition使用姿势 (opens new window)
  • 181019-SpringBoot基础篇Bean之@ConditionalOnBean与@ConditionalOnClass (opens new window)
  • 181019-SpringBoot基础篇Bean之条件注入@ConditionalOnProperty (opens new window)
  • 181019-SpringBoot基础篇Bean之条件注入@ConditionalOnExpression (opens new window)

应用篇

  • 181017-SpringBoot应用篇Bean之注销与动态注册实现服务mock (opens new window)

# b. 项目源码

  • 工程:spring-boot-demo (opens new window)
  • module: 007-conditionbean (opens new window)

# 1. 一灰灰Blog

  • 一灰灰Blog个人博客 https://blog.hhui.top (opens new window)
  • 一灰灰Blog-Spring专题博客 http://spring.hhui.top (opens new window)

一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

# 2. 声明

尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激

  • 微博地址: 小灰灰Blog (opens new window)
  • QQ: 一灰灰/3302797840

# 3. 扫描关注

一灰灰blog

QrCode

知识星球

goals

编辑 (opens new window)
#Bean#Condition
上次更新: 2021/10/15, 19:56:22
【基础系列】Bean之@ConditionalOnBean与@ConditionalOnClass
【基础系列】Bean之条件注入@ConditionalOnProperty

← 【基础系列】Bean之@ConditionalOnBean与@ConditionalOnClass 【基础系列】Bean之条件注入@ConditionalOnProperty→

最近更新
01
【基础系列】基于maven多环境配置
04-25
02
【WEB系列】内嵌Tomcat配置Accesslog日志文件生成位置源码探索
04-24
03
【搜索系列】ES查询常用实例演示
04-18
更多文章>
Theme by Vdoing | Copyright © 2017-2022 一灰灰Blog
MIT License | 鄂ICP备18017282号 |
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×