@Qualifier使用

我的applicationContext.xml的配置如下

<?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">

    <context:component-scan base-package="liusheng.springboot.Spring"/>
    <bean name="student" class="ls.entity.Student">
        <property name="student_ID" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="19"></property>
    </bean>
    <bean name="user2" class="ls.entity.User">
        <property name="age" value="10"></property>
        <property name="name" value="李四"></property>
    </bean>
    <bean name="user1" class="ls.entity.User">
        <property name="age" value="101"></property>
        <property name="name" value="王五"></property>
    </bean>
</beans>

 

1.问题:当我们的容器中有多类型一直或者存在关系的类型且方法的参数名字和字段的名字没有与容器中的bean的名字相同,那么使用@AutoWired就会报如下异常,

我测试类是:

package liusheng.springboot.Spring;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import ls.entity.User;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class MutilClassTypeTest {
        @Autowired
        User user1;
        User user4;
        @Autowired
        public void setUser(User user3){
            this.user4=user3;
        }
        @Test
        public void test() throws Exception {
            System.out.println(user1);
        }
}

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liusheng.springboot.Spring.MutilClassTypeTest':

Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void liusheng.springboot.Spring.MutilClassTypeTest.setUser(ls.entity.User); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [ls.entity.User] is defined: expected single matching bean but found 2: user2,user1

这是我们需要按照名称注入使用 @Qualifer注解,只有一个属性是value  ,这个值会从容器中找相同的名字的bean

这个注解可以使用在字段和参数上,默认使用为空(这个注解要和@AutoWired一起使用,否则无法注入)

package liusheng.springboot.Spring;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import ls.entity.User;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class QualifierTest {
        @Autowired
        @Qualifier(value="user")
        User user;
        @Test
        public void test() throws Exception {
            System.out.println(user);
        }
}

结果:

在方法中的参数上使用:

package liusheng.springboot.Spring;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import ls.entity.User;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class QualifierTest {
        User user1;
        
        @Autowired
        public void setUser1(@Qualifier("user2")  User user1) {
            this.user1 = user1;
        }

        @Test
        public void test() throws Exception {
            System.out.println(user1);
        }
}

结果:

结论:这个注解是只按照名称注入的,故所有想使用的bean必须要有name或者id属性。。

 

posted on 2018-03-15 23:37  学习spring是我必须的  阅读(9124)  评论(0编辑  收藏  举报