@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属性。。

 

转载于:https://www.cnblogs.com/SpringStudy/p/8577200.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值