我的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屬性。。