zoukankan      html  css  js  c++  java
  • @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

  • 相关阅读:
    生成错误:对路径".dll"的访问被拒绝
    实现一个类似于收趣APP的功能
    使用Jquery.form.js ajax表单提交插件弹出下载提示框
    关于Mysql错误:./bin/mysqld_safe --user=mysql& [1] 32710 121003 16:40:22 mysqld_safe Logging to '/var/log/mysqld.log'. 121003 16:40:22 mysqld_s
    Linux下更新时间
    关于轻松安装LNMP和LAMP的编译环境
    LAMP环境CentOS6.4 PHP5.4随笔未整理
    论Linux运维的一些基础安全知识和简单办法
    shell脚本笔记(原创不断记录)
    MySQL创建一个用户,指定一个数据库 授权
  • 原文地址:https://www.cnblogs.com/mayang2465/p/12019396.html
Copyright © 2011-2022 走看看