gball个人知识库
首页
基础组件
基础知识
算法&设计模式
  • 操作手册
  • 数据库
  • 极客时间
  • 每日随笔
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
  • 画图工具 (opens new window)
关于
  • 网盘 (opens new window)
  • 分类
  • 标签
  • 归档
项目
GitHub (opens new window)

ggball

后端界的小学生
首页
基础组件
基础知识
算法&设计模式
  • 操作手册
  • 数据库
  • 极客时间
  • 每日随笔
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
  • 画图工具 (opens new window)
关于
  • 网盘 (opens new window)
  • 分类
  • 标签
  • 归档
项目
GitHub (opens new window)
  • 面试

  • 数据库

  • linux

  • node

  • tensorFlow

  • 基础组件

    • mybatis

    • spring

      • 源码分析

        • spring管理bean之间的协同方式
        • Bean的生命周期
      • Conditional 注解
      • IOC
      • spring bean的配置方式
      • springCache配合redis做缓存
      • springIOC
      • spring的refresh方法解析
      • spring模块介绍
      • spring声明式事务失效原因
      • spring事务实战
      • spring语法变化
      • spring的配置信息
      • springBean了解
      • spring的三级缓存如何解决循环依赖
    • 消息队列

    • springboot

    • tomcat如何工作

    • elasticsearch

  • 基础知识

  • 算法与设计模式

  • 分布式

  • 疑难杂症

  • go学习之旅

  • 极客时间

  • 知识库
  • 基础组件
  • spring
ggball
2021-01-30

Conditional 注解

Spring条件注解@Conditional @Conditional是Spring4新提供的注解,它的作用是根据某个条件创建特定的Bean,通过实现Condition接口,并重写matches接口来构造判断条件。总的来说,就是根据特定条件来控制Bean的创建行为,这样我们可以利用这个特性进行一些自动的配置。

本文将分为三大部分,@Conditional源码的介绍、Condition的使用示例和@Conditional的扩展注解的介绍。

一、@Conditional的源码 复制代码 @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Conditional {

/**
 * All {@link Condition Conditions} that must {@linkplain Condition#matches match}
 * in order for the component to be registered.
 */
Class<? extends Condition>[] value();

} 复制代码 从源码中可以看到,@Conditional注解可以用在类和方法上,需要传入一个实现了Condition接口class数组。 二、代码示例 下面将以不同的操作系统为条件,通过实现Condition接口,并重写其matches方法来构造判断条件。若在Windows系统下运行程序,则输出列表命令为dir;若在Linux系统下运行程序,则输出列表命令为ls。

1.判断条件类的定义 (1).判断Windows的条件 复制代码 package com.study.day01;

import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata;

/**

  • @Auther: lifq
  • @Description: */ public class WindowsCondition implements Condition { public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { return context.getEnvironment().getProperty("os.name").contains("Windows"); } } 复制代码 (2).判断Linux的条件

复制代码 package com.study.day01;

import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata;

/**

  • @Auther: lifq
  • @Description: */ public class LinuxCondition implements Condition { public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { return context.getEnvironment().getProperty("os.name").contains("Linux"); } } 复制代码

2.不同系统下的Bean的类 (1).接口代码 复制代码 package com.study.day01;

import org.springframework.stereotype.Service;

/**

  • @Auther: lifq

  • @Description: */ public interface ListService {

    public String showListCmd();

} 复制代码 (2).Windows实现类代码

复制代码 package com.study.day01;

/**

  • @Auther: lifq
  • @Description: */ public class WindowsService implements ListService { public String showListCmd() { return "dir"; } } 复制代码 (3).Linux实现类代码

复制代码 package com.study.day01;

/**

  • @Auther: lifq
  • @Description: */ public class LinuxService implements ListService { public String showListCmd() { return "ls"; } } 复制代码 3.配置类

复制代码 package com.study.day01;

import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration;

/**

  • @Auther: lifq

  • @Description: */ @Configuration public class Config {

    @Bean @Conditional(WindowsCondition.class) public ListService window() { return new WindowsService(); }

    @Bean @Conditional(LinuxCondition.class) public ListService linux() { return new LinuxService(); } } 复制代码 4.运行类

复制代码 package com.study.day01;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**

  • @Auther: lifq

  • @Description: */ public class Main01 {

    public static void main (String []args) {

     AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
    
     ListService listService = applicationContext.getBean(ListService.class);
    
     System.out.println(applicationContext.getEnvironment().getProperty("os.name") + "系统下的列表命令为:" + listService.showListCmd());
    

    } } 复制代码 我的是Windows操作系统,输出结果为dir,运行截图如下:

运行截图

#java
上次更新: 2025/06/04, 15:06:15
Bean的生命周期
IOC

← Bean的生命周期 IOC→

最近更新
01
AIIDE
03-07
02
githubActionCICD实战
03-07
03
windows安装Deep-Live-Cam教程
08-11
更多文章>
Theme by Vdoing
总访问量 次 | 总访客数 人
| Copyright © 2021-2025 ggball | 赣ICP备2021008769号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×

评论

  • 评论 ssss
  • 回复
  • 评论 ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
  • 回复
  • 评论 ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
  • 回复
×