• 2019.6.28 Spring注解


    6.28

    • Spring提供了获取操作系统的信息:

      ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Myconfig.class);
              ConfigurableEnvironment contextEnvironment = (ConfigurableEnvironment) applicationContext.getEnvironment();
      /*对应jvm参数中的os.name*/
              String ospropertyname = contextEnvironment.getProperty("os.name");
              System.out.println(ospropertyname);
      
    • Conditional根据条件判断是否将bean注册到容器中

      1. 需要将注解bean的方法加上@Conditional

      2. @Conditiona接受一个Condition类型的CLass数组,

      3. 需要写一个条件类来实现Conditional接口里面的matches

      4. matches的方法形参是ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata

        public class Os implements Condition {
            @Override
            public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
                /*获取ioc使用的beanfactory*/
                ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
                /*获取bean定义的注册类*/
                BeanDefinitionRegistry registry = conditionContext.getRegistry();
                Environment environment = conditionContext.getEnvironment();
                String ospropertyname = environment.getProperty("os.name");
        
                /*判断容器中是否含有bean*/
                //boolean person = registry.containsBeanDefinition("person");
                if(ospropertyname.contains("window")){
                    return true;
                }
                return false;
            }
        }
        
  • 相关阅读:
    使用StreamHttpResponse和FileResponse下载文件的注意事项及文件私有化
    Django中@login_required用法简介
    Django之template
    单链表反转的原理和python代码实现
    两个队列实现栈,两个栈实现队列
    Linux--5 mariadb和redis的安装
    Linux--4
    Linux--3
    Linux--2 Linux之文档与目录结构、shell基本命令
    Linux--1 初识
  • 原文地址:https://www.cnblogs.com/mwss/p/11105110.html
Copyright © 2020-2023  润新知