• @Autowired 和 @Qualifier


    一 无冲突

    bean工厂

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
        
        <!-- here1 -->
        <context:annotation-config />
        
        <beans>
            <bean id="person" class="cn.zno.hello.Person" >
                <property name="name" value="XiaoMing"></property>
                <property name="age" value="22"></property>
                <property name="savings" value="10000"></property>
            </bean>
            
            <bean id="car" class="cn.zno.hello.Car">
                <property name="brand" value="BYD"></property>
                <property name="price" value="54000"></property>
            </bean>
        </beans>
    
    </beans>

    测试主函数

        public static void main(String[] args) {
    
            ApplicationContext ctx = new ClassPathXmlApplicationContext("Beans.xml");
    
            Person person = (Person) ctx.getBean("person");//here2
            // Person person = new Person(); //here3
            System.out.println(person);
        }

    javebean 片段

    public class Person {
        
        private String name;
        
        private int age;
        
        private double savings;
        
        @Autowired
        private Car car;
        
        
        public void say(){
            System.out.println("Hello World!");
        }
        
        ...

    说明:

    here1 开启自动注入

    here3 通过 new 的方式无法自动注入

    here2 从bean工厂取的bean可以自动注入

    二有冲突

    package cn.zno.testmybatis;
    
    public class QualifierBean {
    
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
        
    }
        <bean id="a" class="cn.zno.testmybatis.QualifierBean">
            <property name="name" value="a1"></property>
        </bean>
        <bean id="b" class="cn.zno.testmybatis.QualifierBean">
            <property name="name" value="b1"></property>
        </bean>

    单元测试:

        @Autowired @Qualifier("a")
        private QualifierBean qualifierBean1;
    
        @Autowired @Qualifier("b")
        private QualifierBean qualifierBean2;

    注意:

    @Qualifier 必须配合 @Autowired ,否则无法注入

    @Qualifier 指定的值必须在<bean 中通过 id 或 name 指定,不存在时会抛异常(id 和 name 可同时存在且值不同,且都可以通过@Qualifier 指定)

    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.zno.testmybatis.QualifierBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=a)}
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    	... 28 more

    id 和 name 不能与其他bean 的 id 和 name 相同

    Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Bean name 'b' is already used in this <beans> element

    三静态注入

    @Component
    public class Foo {
    
        public void say(String s) {
            System.out.println(s);
        }
    }
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component
    public class SomeUtils {
    
        private static Foo foo;
    
    
        @Autowired
        public void setFoo(Foo foo) {
            SomeUtils.foo = foo;
        }
        
        
        public static void doSomeThing() {
            foo.say("1111");
        }
        
    }
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class ZTestsetApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ZTestsetApplication.class, args);
            
            SomeUtils.doSomeThing();
        }
    
    }
  • 相关阅读:
    TP-LINK WR941N路由器研究
    thinkjs初试
    记浏览器帐号登录插件开发遇到的问题
    你被adblock坑过吗?
    web应用,我们需要了解什么?
    算法之合并排序
    算法之插入排序
    算法之初体验
    nodejs学习笔记之网络编程
    炫酷吊炸天的nodeppt
  • 原文地址:https://www.cnblogs.com/zno2/p/4767557.html
Copyright © 2020-2023  润新知