.catalogue-div { position: relative; background-color: rgba(255, 255, 255, 1); right: 0 }
.catalogue { background-color: rgba(255, 255, 255, 1); padding-right: 50px; padding-left: 20px; min-width: 300px; max-width: 500px; position: absolute; top: -4em; right: inherit; width: inherit; height: auto; border-radius: 5px; box-shadow: 5px 5px 10px 2px rgba(128, 128, 128, 1) }
.catalogue ul { }
.catalogue ul li { overflow: hidden; text-overflow: ellipsis; white-space: nowrap }
.catalogue ul li:before { position: relative; content: "☞"; font-size: 25px; margin-right: 5px; margin-left: 20px; line-height: 25px; bottom: -2px }
.catalogue a { text-decoration: none }
.catalogue a:hover { color: rgba(173, 255, 47, 1); cursor: pointer }

1.说明:基于条件,判断是否实例化对象,注入容器中,组合@bean注解使用和扫描。

2.源代码

        @Target({ElementType.TYPE, ElementType.METHOD}) // 注解在类、方法使用
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional { Class<? extends Condition>[] value(); // 1.注解值必须继承Condition,2.值为单个值或数组 }
    @FunctionalInterface
public interface Condition {
boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);//返回false,条件不成立。目标动作不执行,返回true,条件成立执行目标动作。
}

3.使用方式:创建 Condition 的实现类 TsetCondition,使用@Condition(value= TsetCondition.class)

1.使用在类:条件成立实例化对象,注入容器

基于条件创建被标注的类,可以结合 @Service ,@Component ,@Controller 三个注解使用,条件成立,创建实例。

          @Component
@Conditional(value = TestCondition.class)
public class Tst {
@Scheduled(fixedRate = 5000)
public void getStr() {
System.err.println("我的scheduled 执行了");
}
}

也可以结合 @Configuration ,@Bean 基于配置文件,条件不成立@Configuration标注的类不创建,类下被标注的@Bean方法不执行

                @Configuration
@Conditional(value = TestCondition.class)
public class TestConfiguration { @Bean
public Tst createTst() {
return new Tst();
}
}

2.在方法上使用

结合@Bean注解使用 ,条件成立方法执行,创建对象实例。

            @Configuration
public class TestConfiguration { @Bean
@Conditional(value = TestCondition.class)
public Tst createTst() { // 基于条件实例化Bean
return new Tst();
} @Bean
public Tst2 createTst2() { //没有条件、只要系统启动就会创建Tst2实例
return new Tst2();
}
}

3.多条件件使用(方法和类):会执行每个 Condition的 matches ,如果有一个返回false ,条件不成立目标不执行

            @Configuration
public class TestConfiguration { @Bean
@Conditional(value = {TestCondition.class,TestCondition2.class})
public Tst createTst() { // 基于条件实例化Bean,只有 TestCondition 、TestCondition2 都返回true才实例化对象,就是说,value集合中只要有一个Condition不成立,就不会实例对象
return new Tst();
}
}

@Conditional 注解,基于条件实例对象的更多相关文章

  1. springboot根据不同的条件创建bean,动态创建bean,@Conditional注解使用

    这个需求应该也比较常见,在不同的条件下创建不同的bean,具体场景很多,能看到这篇的肯定懂我的意思. 倘若不了解spring4.X新加入的@Conditional注解的话,要实现不同条件创建不同的be ...

  2. 一文了解@Conditional注解说明和使用

    ​ @Conditional:Spring4.0 介绍了一个新的注解@Conditional,它的逻辑语义可以作为"If-then-else-"来对bean的注册起作用. @Con ...

  3. Spring中的@conditional注解

    今天主要从以下几方面来介绍一下@Conditional注解 @Conditional注解是什么 @Conditional注解怎么使用 1,@Conditional注解是什么 @Conditional注 ...

  4. SpringBoot(15)—@Conditional注解

    SpringBoot(15)-@Conditional注解 作用 @Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件的才给容器注册Bean. 一.概述 1 ...

  5. Spring @Conditional注解 详细讲解及示例

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/xcy1193068639/article/details/81491071 前言: @Conditi ...

  6. spring 3.x变通实现@Conditional注解的功能

    在某些情况下,我们要根据当前的系统配置决定是否初始化一个bean,也就是条件性加载,比如本地缓存或者分布式缓存,亦或是采用http实现或者socket实现.在spring 4.x中,可以使用新增的@C ...

  7. mybatis从dao传入多个参数到sqlmap时dao中要使用map或实例对象(如:user)作为参数传入, 否则报错找不到属性getter方法

    23:37 2015-07-02 注意1. 使用mybaits的resultMap查询时, 如果想传入多个参数(比如where 1=1动态多条件查询时)sqlmap文件中对应的方法中, selectL ...

  8. Vue实例对象的数据选项

    前面的话 一般地,当模板内容较简单时,使用data选项配合表达式即可.涉及到复杂逻辑时,则需要用到methods.computed.watch等方法.本文将详细介绍Vue实例对象的数据选项 data ...

  9. JS基础:基于原型的对象系统

    简介: 仅从设计模式的角度讲,如果我们想要创建一个对象,一种方法是先指定它的类型,然后通过这个类来创建对象,例如传统的面向对象编程语言 "C++"."Java" ...

随机推荐

  1. C#中指针的使用(转)

    在C#中,有时候希望通过指针来操作内存,这样可以提高效率.我们可以用unsafe关键字修饰含有指针操作的程序段,如下所示: class Program {   static int Main(stri ...

  2. 论文翻译:2020_Densely connected neural network with dilated convolutions for real-time speech enhancement in the time domain

    提出了模型和损失函数 论文名称:扩展卷积密集连接神经网络用于时域实时语音增强 论文代码:https://github.com/ashutosh620/DDAEC 引用:Pandey A, Wang D ...

  3. docker 配置redis并远程访问

    我安装的是这个镜像 docker.io/redis docker pull docker mkdir docker cd docker mkdir redis cd redis mkdir data ...

  4. shiro 学习笔记

    1. 权限管理 1.1 什么是权限管理? 权限管理实现对用户访问系统的控制,按照安全规则或者安全策略,可以控制用户只能访问自己被授权的资源 权限管理包括用户身份认证和授权两部分,简称认证授权 1.2 ...

  5. SpringCloud升级之路2020.0.x版-44.避免链路信息丢失做的设计(1)

    本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 我们在这一节首先分析下 Spring Cloud Gateway 一些其他可能丢失链路信息 ...

  6. 7.5 实现基于探针对pod中的访问的健康状态检查

    1.pod的声明周期 取值 描述 Pending Pod 已被 Kubernetes 系统接受,但有一个或者多个容器尚未创建亦未运行.此阶段包括等待 Pod 被调度的时间和通过网络下载镜像的时间, R ...

  7. vcstool是什么?

    为什么会去了解vcstool,在想要手动编译并且获取ROS源码的时候,有一个Get ROS 2 code的章节中使用到了这个工具. mkdir -p ~/ros2_foxy/src cd ~/ros2 ...

  8. 详解Threejs中的光源对象

    光源的分类 AmbientLight(环境光),PointLight(点光源),SpotLight(聚光源) 和 DirectionalLight(平行光)是基础光源 HemisphereLight( ...

  9. UOJ #36 -【清华集训2014】玛里苟斯(线性基+暴搜)

    UOJ 题面传送门 看到 \(k\) 次方的期望可以很自然地想到利用低次方和维护高次方和的套路进行处理,不过.由于这里的 \(k\) 达到 \(5\),直接这么处理一来繁琐,二来会爆 long lon ...

  10. Codeforces 1406E - Deleting Numbers(根分+数论)

    Codeforces 题面传送门 & 洛谷题面传送门 一道个人感觉挺有意思的交互题,本人一开始想了个奇奇怪怪的做法,还以为卡不进去,结果发现竟然过了,而且还是正解( 首先看到这类题目可以考虑每 ...