注解autowired注解 原理 什么时候装配

Spring注解@Resource和@Autowired_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
Spring注解@Resource和@Autowired
&&Spring注解@Resource和@Autowired
阅读已结束,下载文档到电脑
想免费下载更多文档?
定制HR最喜欢的简历
你可能喜欢& & & &今天在预发布环境配置一个manager的时候,应用启动失败,报错:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.taobao.deptadmin.biz.core.dynup.DynupSearchManager com.taobao.deptadmin.biz.core.dynup.impl.DefaultDynupRuleBuildManager.dynupSearchM nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dynupSearchManager': Autowir nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private mon.searchengine.SearchManager com.taobao.deptadmin.biz.core.dynup.impl.DefaultDynupSearchManager.searchM nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [mon.searchengine.SearchManager] is defined: expected single matching bean but found 2: [dynupSpuSearchManager, searchEngineManager]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
... 152 more
& & & &主要是:
& & & &No unique bean of type [mon.searchengine.SearchManager] is defined: expected single matching bean but found 2
& & & DynupSearchManager依赖了SearchManager,类类型为mon.searchengine.DefaultSearchManager,DynupSearchManager使用@autowire 关键字来注入SearchManager属性。由于业务需要,我在另外一个地方也需要配置一个类型为DefaultSearchManager的manager。之前的代码使用@autowire
关键字来自动注入,为类型匹配。如果出现了两个相同的类型,那么会报错,就是日志中的错误。所以这里不呢个使用@autowire 来注入。可以去掉@autowire,用set方法,在xml文件中,用by-name的方式来注入。或者使用@resource来注入,或者使用@Qualifier来做。
本文已收录于以下专栏:
相关文章推荐
1   配置文件的方法
我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量。并且要配套写上 get 和 set方法。 
...
Spring@Autowired注解与自动装配
1   配置文件的方法
我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量。并且要配套写上
get 和 se...
1   配置文件的方法
我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量。并且要配套写上 get 和 set方...
1   配置文件的方法
我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量。并且要配套写上 get
本文转自:http://blog.csdn.net/oathevil/article/details/6661497
遇到的问题@Autowired
@Qualifier(&cipShopOwnerServiceImpl&)
ShopOwnerService cipShopOwnerServi...
配置文件的方法      我们编写spring框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean都建议定义成私有的域变量。并且要配套写上get和set方法。     ...
1   配置文件的方法
我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量。并且要配套写上 get 和 set方...
1   配置文件的方法
我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量。并且要配套写上 get 和 set方法。
他的最新文章
讲师:王哲涵
讲师:韦玮
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)Spring@Autowired注解与自动装配 - / - 博客园
1 & 配置文件的方法
我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量。并且要配套写上 get 和 set方法。
Boss 拥有 Office 和 Car 类型的两个属性:&&&&&&&清单 3. Boss.java
package&com.&&&&&
public&class&Boss&{&&&&&
&&&&private&Car&&&&&&
&&&&private&Office&&&&&&
&&&&@Override&&&&
&&&&public&String&toString()&{&&&&&
&&&&&&&&return&"car:"&+&car&+&"/n"&+&"office:"&+&&&&&&
&&&&}&&&&&
&&&&&&&System.out.println必须实现toString方法&&我们在 Spring 容器中将 Office 和 Car 声明为 Bean,并注入到 Boss Bean 中:下面是使用传统 XML 完成这个工作的配置文件 beans.xml:&&&&&&&清单 4. beans.xml 将以上三个类配置成 Bean&&&&&&&&&&&&&&&&&&&&&&
&version="1.0"&encoding="UTF-8"&&&&&&
&xmlns="http://www.springframework.org/schema/beans"&&&&
&&&&xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&&&&
&&&&xsi:schemaLocation="http://www.springframework.org/schema/beans&&&&&&
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&&&&&
&&&&&id="boss"&class="com.baobaotao.Boss"&&&&&
&&&&&&&&&name="car"&ref="car"&&&&&
&&&&&&&&&name="office"&ref="office"&&&&&&
&&&&&id="office"&class="com.baobaotao.Office"&&&&&
&&&&&&&&&name="officeNo"&value="002"&&&&&
&&&&&id="car"&class="com.baobaotao.Car"&scope="singleton"&&&&&
&&&&&&&&&name="brand"&value="&红旗&CA72"&&&&&
&&&&&&&&&name="price"&value="2000"&&&&&
&当我们运行以下代码时,控制台将正确打出 boss 的信息:&&&&&&&清单 5. 测试类:AnnoIoCTest.java&&&&&&&&&&&&&&&&&&&&&&
import&org.springframework.context.ApplicationC&&&&&
import&org.springframework.context.support.ClassPathXmlApplicationC&&&&&
public&class&AnnoIoCTest&{&&&&&
&&&&public&static&void&main(String[]&args)&{&&&&&
&&&&&&&&String[]&locations&=&{"beans.xml"};&&&&&
&&&&&&&&ApplicationContext&ctx&=&&&&&&
&&&&&&&&&&&&new&ClassPathXmlApplicationContext(locations);&&&&&
&&&&&&&&Boss&boss&=&(Boss)&ctx.getBean("boss");&&&&&
&&&&&&&&System.out.println(boss);&&&&&
&&&&}&&&&&
&&&&&&&&&&这说明 Spring 容器已经正确完成了 Bean 创建和装配的工作。&&&
&2 & @Autowired&
Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。&通过 @Autowired的使用来消除 set ,get方法。
要实现我们要精简程序的目的。需要这样来处理:&* 在applicationContext.xml中加入:&&&
&!--&该&BeanPostProcessor&将自动对标注&@Autowired&的&Bean&进行注入&--&&&&&&
&&&bean&class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/&&&
Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。&&&
* 修改在原来注入spirng容器中的bean的方法。&&&&& 在域变量上加上标签@Autowired,并且去掉 相应的get 和set方法
清单 6. 使用 @Autowired 注释的 Boss.java&&&&&&&&&&&&&&&&&&&&&&
package&com.&&&&&
import&org.springframework.beans.factory.annotation.A&&&&&
public&class&Boss&{&&&&&
&&&&@Autowired&&&&
&&&&private&Car&&&&&&
&&&&@Autowired&&&&
&&&&private&Office&&&&&&
&&&&&&&&&&
* 在applicatonContext.xml中 把原来 引用的&porpery &标签也去掉。
&&&&&&&&&&&&
&version="1.0"&encoding="UTF-8"&&&&&&
&xmlns="http://www.springframework.org/schema/beans"&&&&
&&&&xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&&&&
&&&&xsi:schemaLocation="http://www.springframework.org/schema/beans&&&&&&
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&&&&&
&&&&&class="org.springframework.beans.factory.annotation.&&&&&
&&&&&&&&AutowiredAnnotationBeanPostProcessor"&&&&&
&&&&&id="boss"&class="com.baobaotao.Boss"&&&&&
&&&&&id="office"&class="com.baobaotao.Office"&&&&&
&&&&&&&&&name="officeNo"&value="001"&&&&&
&&&&&id="car"&class="com.baobaotao.Car"&scope="singleton"&&&&&
&&&&&&&&&name="brand"&value="&红旗&CA72"&&&&&
&&&&&&&&&name="price"&value="2000"&&&&&
&这样,当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。&&&&&按照上面的配置,Spring 将直接采用 Java 反射机制对 Boss 中的 car 和 office 这两个私有成员变量进行自动注入。所以对成员变量使用 @Autowired 后,您大可将它们的 setter 方法(setCar() 和 setOffice())从 Boss 中删除。&&&&&当然,您也可以通过 @Autowired 对方法或构造函数进行标注,如果构造函数有两个入参,分别是 bean1 和 bean2,@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 CountryService (Bean1 bean1 ,Bean2 bean2) 的入参来创建 CountryService Bean。来看下面的代码:&&对方法
package&com.&&&&&
public&class&Boss&{&&&&&
&&&&private&Car&&&&&&
&&&&private&Office&&&&&&
&&&&&@Autowired&&&&
&&&&public&void&setCar(Car&car)&{&&&&&
&&&&&&&&this.car&=&&&&&&
&&&&}&&&&&
&&&&@Autowired&&&&
&&&&public&void&setOffice(Office&office)&{&&&&&
&&&&&&&&this.office&=&&&&&&
&&&&}&&&&&
&&&&&&&&&&
这时,@Autowired 将查找被标注的方法的入参类型的 Bean,并调用方法自动注入这些 Bean。而下面的使用方法则对构造函数进行标注:&&&
package&com.&&&&&
public&class&Boss&{&&&&&
&&&&private&Car&&&&&&
&&&&private&Office&&&&&&
&&&&@Autowired&&&&
&&&&public&Boss(Car&car&,Office&office){&&&&&
&&&&&&&&this.car&=&&&&&&
&&&&&&&&this.office&=&office&;&&&&&
&&&&}&&&&&
&&&&&&&&&&
由于 Boss() 构造函数有两个入参,分别是 car 和 office,@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 Boss(Car car ,Office office) 的入参来创建 Boss Bean。&&2050人阅读
使用到注解需导入jar包:common-annotations.jar
手工装配依赖对象有两种编程方式:
一、在xml配置文件中通过bean节点进行配置,如:
&?xml version="1.0" encoding="UTF-8"?&
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&
id="personDao" class="test.spring.dao.impl.PersonDaoBean" /&
id="personService" class="test.spring.service.impl.PersonServiceBean3"&
index="0" type="test.spring.dao.PersonDao" ref="personDao"/&
index="1" value="LinDL"/&
二、在Java代码中使用@Autowired或@Resource注解方式进行装配。但需在xml配置文件中配置以下信息:
&?xml version="1.0" encoding="UTF-8"?&
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"&
id="..." class="..." /&
这个配置隐式注册了多个对注释进行解析处理的处理器:AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor、RequireAnnotationBeanPostProcessor
在代码中使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是:
@Autowired默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。
用于字段上
private PersonDao personD
用于属性的setter方法上
public void setPersonDao(PersonDao personDao) {
this.personDao = personD
一个应用@Resource的例子:
package test.spring.
public interface PersonDao {
public abstract void add();
package test.spring.dao.
import test.spring.dao.PersonD
public class PersonDaoBean implements PersonDao {
public void add(){
System.out.println("执行PersonDaoBean里的test1()方法");
package test.spring.
public interface PersonService2 {
public abstract void save();
package test.spring.service.
import javax.annotation.R
import test.spring.dao.PersonD
import test.spring.service.PersonService2;
public class PersonServiceBean4 implements PersonService2 {
private PersonDao personD
public PersonServiceBean4() {
public PersonDao getPersonDao() {
return personD
public void setPersonDao(PersonDao personDao) {
this.personDao = personD
public void save() {
personDao.add();
beans2.xml
&?xml version="1.0" encoding="UTF-8"?&
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"&
id="personDaoxx" class="test.spring.dao.impl.PersonDaoBean" /&
id="personService" class="test.spring.service.impl.PersonServiceBean4"&&
package test.spring.jnit
import org.junit.Test
import org.springframework.context.support.AbstractApplicationContext
import org.springframework.context.support.ClassPathXmlApplicationContext
import test.spring.service.PersonService2
public class SpringTest3 {
public void testInject() {
AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"beans2.xml")
PersonService2 personService = (PersonService2) applicationContext
.getBean("personService")
personService.save()
applicationContext.close()
@Autowired的应用
package test.spring.service.
import org.springframework.beans.factory.annotation.A
import org.springframework.beans.factory.annotation.Q
import test.spring.dao.PersonD
import test.spring.service.PersonService2;
public class PersonServiceBean5 implements PersonService2 {
@Autowired(required = true)
@Qualifier("personDaoxx")
private PersonDao personD
public PersonServiceBean5() {
public PersonDao getPersonDao() {
return personD
public void setPersonDao(PersonDao personDao) {
this.personDao = personD
public void save() {
personDao.add();
自动装配,使用自动装配方式难以调试,程序中的bug通常在运行过程中出现问题才发现,而且容易出现难以预料的问题,故不建议使用。
beans3.xml
&?xml version="1.0" encoding="UTF-8"?&
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"&
id="personDaoxx" class="test.spring.dao.impl.PersonDaoBean" /&
id="personService" class="test.spring.service.impl.PersonServiceBean6" autowire="byType"&&
package test.spring.service.
import test.spring.dao.PersonD
import test.spring.service.PersonService2;
public class PersonServiceBean6 implements PersonService2 {
private PersonDao personD
public PersonServiceBean6() {
public void setPersonDao(PersonDao personDao) {
this.personDao = personD
public void save() {
personDao.add();
package test.spring.jnit
import org.junit.Test
import org.springframework.context.ApplicationContext
import org.springframework.context.support.ClassPathXmlApplicationContext
import test.spring.service.PersonService2
public class SpringTest5 {
public void testAutowired() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"beans3.xml")
PersonService2 personService = (PersonService2) applicationContext
.getBean("personService")
personService.save()
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:410115次
积分:8275
积分:8275
排名:第2556名
原创:422篇
转载:53篇
评论:55条
(1)(4)(1)(4)(2)(2)(1)(6)(1)(3)(1)(4)(18)(2)(3)(5)(8)(12)(5)(24)(5)(29)(30)(10)(27)(13)(24)(26)(3)(9)(7)(1)(7)(4)(4)(169)
(window.slotbydup = window.slotbydup || []).push({
id: '4740887',
container: s,
size: '250,250',
display: 'inlay-fix'

我要回帖

更多关于 autowired注解 的文章

 

随机推荐