• @Condition 条件化注册Bean


    看《Spring源码深度解析》笔记

    1、@Condition:

       按照一定的条件进行判断,满足条件给容器中注册bean;
    实例:
    根据系统给容器中注册Bean,如果是windows注册(“bill”);
    如果是linus,注册(“linus”)
        @Bean("bill")
        public Person person01(){
            return new Person("Bill Gates",62);
        }
     
        @Bean("linus")
        public Person person02(){
            return new Person("linus", 48);
        }

    2、根据注解@Condition,我们需要添加两个条件类,这两个条件类均要实现Condition接口:

    //判断是否windows系统
    public class WindowsCondition implements Condition {
        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
            Environment environment = context.getEnvironment();
            String property = environment.getProperty("os.name");
            if(property.contains("Windows")){
                return true;
            }
            return false;
        }
    }
    //判断是否linux系统
    public class LinuxCondition implements Condition {
        /**
         * ConditionContext:判断条件能使用的上下文(环境)
         * AnnotatedTypeMetadata:注解信息
         */
        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
            // TODO是否linux系统
            //1、能获取到ioc使用的beanfactory
            ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
            //2、获取类加载器
            ClassLoader classLoader = context.getClassLoader();
            //3、获取当前环境信息
            Environment environment = context.getEnvironment();
            //4、获取到bean定义的注册类
            BeanDefinitionRegistry registry = context.getRegistry();
            String property = environment.getProperty("os.name");
            //可以判断容器中的bean注册情况,也可以给容器中注册bean
            boolean definition = registry.containsBeanDefinition("person");
            if(property.contains("linux")){
                return true;
            }
            return false;
        }
    }

    3、使用,在进行实例化的前加含有对应条件类的接口:

       @Conditional(WindowsCondition.class)
        @Bean("bill")
        public Person person01(){
            return new Person("Bill Gates",62);
        }
        
        @Conditional(LinuxCondition.class)
        @Bean("linus")
        public Person person02(){
            return new Person("linus", 48);
        }

    4、测试:

    @Test
        public void test03(){
            AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
            String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
            ConfigurableEnvironment environment = applicationContext.getEnvironment();
            //动态获取环境变量的值;Windows 10
            String property = environment.getProperty("os.name");
            System.out.println(property);
            for (String name : namesForType) {
                System.out.println(name);
            }
            Map<String, Person> persons = applicationContext.getBeansOfType(Person.class);
            System.out.println(persons);
            
        }

    如果希望获取Linux的模拟环境,只需要更改运行参数:-Dos.name= linux

  • 相关阅读:
    CSS团队协作规范
    百度前端学院-基础学院-第四课
    百度前端学院-基础学院-第三课
    百度前端学院-基础学院-第二课
    vue(三)-父子组件通信
    setTimeout()与clearTimeout()
    vue(二)-父子组件语法
    vue(一)使用vue-cli搭建项目
    CSS table-layout 属性
    git学习
  • 原文地址:https://www.cnblogs.com/mayang2465/p/12019396.html
Copyright © 2020-2023  润新知