Spring处理自动装配的歧义性

歧义性的简单场景如下:

package noUniqueBeanDefinition;

public interface Dessert {
	public void getDessert();
}
package noUniqueBeanDefinition;

import org.springframework.stereotype.Component;

@Component
public class Cake implements Dessert {
	public void getDessert() {
		System.out.println("=====Cake=====");
	}
}
package noUniqueBeanDefinition;

import org.springframework.stereotype.Component;

@Component
public class Cookies implements Dessert {
	public void getDessert() {
		System.out.println("=====Cookies=====");
	}
}
package noUniqueBeanDefinition;

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Component("iceCream")
public class IceCream implements Dessert {
	public void getDessert() {
		System.out.println("=====IceCream=====");
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    	http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/aop/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        ">
	 <context:component-scan base-package="noUniqueBeanDefinition"/> 
</beans>

package noUniqueBeanDefinition;

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;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("../ApplicationContextXml.xml")
public class DessertTest {
	@Autowired
	Dessert dessert;
	@Test
	public void test() {
		dessert.getDessert();
	}
}

当调用DessertTest类中的test()方法的时候,编译器不知道调用哪个bean,会报

 org.springframework.beans.factory.NoUniqueBeanDefinitionException:
 No qualifying bean of type 'noUniqueBeanDefinition.Dessert' available: 
 expected single matching bean but found 3: cake,cookies,iceCream

避免装配的歧义性的2中方案:

  1. 首选:使用@Primary
  2. 限定符:使用@Qualifier(“xxxx”)

首选

修改IceCream类如下

package noUniqueBeanDefinition;

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Component("iceCream")
@Primary
public class IceCream implements Dessert {
	public void getDessert() {
		System.out.println("=====IceCream=====");
	}
}

在类名上加上@Primary,表示IceCream为首选。
再次调用DessertTest类中的test()方法的时候,返回

=====IceCream=====

如果这时候再在Cake类上加上@Primary,会报和上面一样的错。两个首选就等于没有首选。
所以首选的局限性就是只能设置一个,设置多个相当与没设置。

限定符—@Qualifier注解是使用限定符的主要方式。

修改DessertTest类如下:

package noUniqueBeanDefinition;

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;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("../ApplicationContextXml.xml")
public class DessertTest {
	@Autowired
	@Qualifier("iceCream")
	Dessert dessert;
	@Test
	public void test() {
		dessert.getDessert();
	}
}

为@Qualifier注解所设置的参数就是想要注入的bean的ID。
缺点:@Qualifier注解所设置的参数与要注入的bean的名称是紧耦合。对类名称的任意改动都会导致限定符的失败。避免出现紧耦合的方式就是在类上加上@Qualifier(“xxxx”),这样在装配的时候就是装配xxxx,修改类名不会影响装配
例如:

package noUniqueBeanDefinition;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component("iceCream")
@Qualifier("cream")
public class IceCream implements Dessert {
	public void getDessert() {
		System.out.println("=====IceCream=====");
	}
}
package noUniqueBeanDefinition;

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;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("../ApplicationContextXml.xml")
public class DessertTest {
	@Autowired
	@Qualifier("cream")
	Dessert dessert;
	@Test
	public void test() {
		dessert.getDessert();
	}
}

调用test()方法,返回=====IceCream=====

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值