• @Qualifier的作用和应用


    @Qualifier的作用

    这是官方的介绍

    This annotation may be used on a field or parameter as a qualifier for

    candidate beans when autowiring. It may also be used to annotate other

    custom annotations that can then in turn be used as qualifiers.

    简单的理解就是:
    (1)在使用@Autowire自动注入的时候,加上@Qualifier(“test”)可以指定注入哪个对象;
    (2)可以作为筛选的限定符,我们在做自定义注解时可以在其定义上增加@Qualifier,用来筛选需要的对象。这个理解看下面的代码吧,不好解释。

    功能介绍

    • 首先是对(1)的理解。

    //我们定义了两个TestClass对象,分别是testClass1和testClass2

    //我们如果在另外一个对象中直接使用@Autowire去注入的话,spring肯定不知道使用哪个对象

    //会排除异常 required a single bean, but 2 were found

    @Configuration

    public class TestConfiguration {

       @Bean("testClass1")

       TestClass testClass1(){

           return new TestClass("TestClass1");

       }

       @Bean("testClass2")

       TestClass testClass2(){

           return new TestClass("TestClass2");

       }

    }

    下面是正常的引用

    @RestController

    public class TestController {

     

        //此时这两个注解的连用就类似 @Resource(name="testClass1")

        @Autowired

        @Qualifier("testClass1")

        private TestClass testClass;

     

        @GetMapping("/test")

        public Object test(){

            return testClassList;

        }

     

    }

    @Autowired和@Qualifier这两个注解的连用在这个位置就类似 @Resource(name=“testClass1”)

    • 对(2)的理解

     

    @Configuration

    public class TestConfiguration {

        //我们调整下在testClass1上增加@Qualifier注解

        @Qualifier

        @Bean("testClass1")

        TestClass testClass1(){

            return new TestClass("TestClass1");

        }

     

        @Bean("testClass2")

        TestClass testClass2(){

            return new TestClass("TestClass2");

        }

    }

    @RestController

    public class TestController {

        //我们这里使用一个list去接收testClass的对象

        @Autowired

        List<TestClass> testClassList= Collections.emptyList();

       

        @GetMapping("/test")

        public Object test(){

            return testClassList;

        }

    }

    我们调用得到的结果是

    [

         {

            "name": "TestClass1"

         },

        {

           "name": "TestClass2"

        }

    ]

    我们可以看到所有的testclass都获取到了。接下来我们修改下代码

    @RestController

    public class TestController {

     

        @Qualifier //我们在这增加注解

        @Autowired

        List<TestClass> testClassList= Collections.emptyList();

     

        @GetMapping("/test")

        public Object test(){

            return testClassList;

        }

    和上面代码对比就是在接收参数上增加了@Qualifier注解,这样看是有什么区别,我们调用下,结果如下:

    [

         {

            "name": "TestClass1"

         }

    ]

    返回结果只剩下增加了@Qualifier注解的TestClass对象,这样我们就可以理解官方说的标记筛选是什么意思了。
    另外,@Qualifier注解是可以指定value的,这样我们可以通过values来分类筛选想要的对象了,这里不列举代码了,感兴趣的同学自己试试。

  • 相关阅读:
    jQuery validator plugin之Selector
    jQuery validator plugin之Methods
    jQuery validator plugin之Plugin Method
    jQuery validator plugin之概要
    jQuery validator plugin 之 custom methods 案例1:multi email
    实体关系图应用——google ads
    Google advertiser api开发概述——入门指南
    Google advertiser api开发概述——部分失败
    Google advertiser api开发概述——批量处理
    oogle advertiser api开发概述——速率限制
  • 原文地址:https://www.cnblogs.com/ylsx/p/13933013.html
Copyright © 2020-2023  润新知