• springBoot 条件注解


    前言

    @ConditionalOnXxx
    如果指定条件成立则指定条件触发

    常用的条件注解

    @ConditionalOnClass:
    如果类路径存在这个类,则触发了个行为
    @ConditionalMissingClass:
    如果类路径中不存这个类,则触发指定行为
    @ConditionalOnBean:
    如果容器中存在这个Bean(组件),则触发了个行为
    @ConditionalMissingBean:
    如果容器中不存在这个Bean(组件),则触发了个行为
    在这里插入图片描述

    用例场景

    如果存在FastsqlException 这个类,给容器放一个car组件,名为car01,没有就给容器放一个dog组件,名为dog01
    如果系统中有dog01这个组件,就给容器中放一个urser组件,名为张三,否则就放一个user组件,名为李四

    用例

    创建配置文件,并写入条件注解
    在这里插入图片描述

    public class AppConfig2 {
        @ConditionalOnClass(name="com.alibaba.druid.FastsqlException")
        @Bean
        public Cat cat01(){
            return new Cat();
        }
    
        @ConditionalOnMissingClass(value="com.alibaba.druid.FastsqlException")
        @Bean
        public Dog dog01(){
            return new Dog();
        }
    
        @ConditionalOnBean(value = Dog.class)
        @Bean
        public User zhangsn(){
            return new User();
        }
    
        @ConditionalOnMissingBean(value = Dog.class)
        @Bean
        public User lisi(){
            return new User();
        }
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    启动文件输入

    在这里插入图片描述

    package com.atguigu.boot;
    
    import com.alibaba.druid.FastsqlException;
    import com.atguigu.boot.bean.Cat;
    import com.atguigu.boot.bean.Dog;
    import com.atguigu.boot.bean.User;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Boot302DemoApplication {
    
    	public static void main(String[] args) {
    
    		var ioc = SpringApplication.run(Boot302DemoApplication.class, args);
    //		for(String name : ioc.getBeanDefinitionNames()){
    //			System.out.println("-------------------------");
    //			System.out.println(name);
    //		}
    		Object goWord1 = ioc.getBean("goWord");
    		Object goWord2 = ioc.getBean("goWord");
    		System.out.println(goWord1==goWord2);
    
    //		查看是否有这个组件
    		String[] forType = ioc.getBeanNamesForType(FastsqlException.class);
    		for (String s: forType){
    			System.out.println(s);
    		}
    
    //		查看是否存在cat组件
    		for (String s : ioc.getBeanNamesForType(Cat.class)) {
    			System.out.println(s);
    		}
    
    		for (String s : ioc.getBeanNamesForType(Dog.class)) {
    			System.out.println(s);
    		}
    
    		for (String s : ioc.getBeanNamesForType(User.class)) {
    			System.out.println(s);
    		}
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    打印结果

    在这里插入图片描述

  • 相关阅读:
    K8s日志类型综述
    洛谷刷题C语言:远古档案馆(Ancient Archive)、VOLIM、SAHOVNICA、Tuna、KRIŽALJKA
    LabVIEW学习记录 - 实时显示时间
    linux安装mysql 8.0.20
    R语言caTools包进行数据划分、scale函数进行数据缩放、e1071包的naiveBayes函数构建朴素贝叶斯模型
    两年Java的面试经验
    Socket 编程
    云原生安全——docker逃逸
    java学习路线小白——架构师
    JavaScript之BOM复习(54th)
  • 原文地址:https://blog.csdn.net/m0_50207524/article/details/133816404