@Resource与@Autowired用法区别

    spring中,@Resource和@Autowired都是做bean的注入时使用。使用过程中,有时候@Resource 和 @Autowired可以替换使用;有时,则不可以。

    下面,根据自己的学习,整理下这两个注解使用中的共同点和不同点,及用法上的不同。

     共同点

     @Resource和@Autowired都可以作为注入属性的修饰,在接口仅有单一实现类时,两个注解的修饰效果相同,可以互相替换,不影响使用。

    不同点

  • @Resource是Java自己的注解,@Resource有两个属性是比较重要的,分是name和type;Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。
  • @Autowired是spring的注解,是spring2.5版本引入的,Autowired只根据type进行注入,不会去匹配name。如果涉及到type无法辨别注入对象时,那需要依赖@Qualifier或@Primary注解一起来修饰。

     

     我们创建一个简单的springboot项目demo,

         定义一个接口Human.java,里面一个方法 runMarathon,

         一个实现类Man.java

         一个Controller类HumanController.java,里面注入Human接口的实现

           

           附各Java类源码

package com.example.annotation.service;

/**
 * service接口定义
 * @author Administrator
 */
public interface Human {
	
	/**
	 * 跑马拉松
	 * @return
	 */
	String runMarathon();
}

package com.example.annotation.service.impl;

import com.example.annotation.service.Human;
import org.springframework.stereotype.Service;

/**
 * service接口第一实现类
 * @author Administrator
 */
@Service
public class Man implements Human {

	public String runMarathon() {
		return "A man run marathon";
	}
}

package com.example.annotation.controller;

import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.annotation.service.Human;

/**
 * controller层实现类
 * @author Administrator
 */
@RestController
@RequestMapping("/an")
public class HumanController {

	@Resource
	private Human human;
	
	@RequestMapping("/run")
	public String runMarathon() {
		return human.runMarathon();
	}
}

           至此,代码整理完成,启动springboot,浏览器地址栏输入http://localhost:8080/an/run

改动一:

将HumanController.java 类中的注解替换为@Autowired,再次启动,可以正常访问,与上图相同,这里不再贴访问结果图。

改动二:

再增加一个实现类Woman.java

package com.example.annotation.service.impl;

import com.example.annotation.service.Human;
import org.springframework.stereotype.Service;

/**
 * service接口第二实现类
 * @author Administrator
 */
@Service
public class Woman implements Human {

	public String runMarathon() {
		return "An woman run marathon";
	}
}

HumanController.java 注解使用@Resource

        @Resource
	private Human human;

启动springboot,控制台会报错,报错信息太多,截取关键信息

2018-09-10 16:07:10.362  WARN 5592 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'humanController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.example.annotation.service.Human' available: expected single matching bean but found 2: man,woman

找关键信息 expected single matching bean but found 2: man,woman,被期望的单一结果被匹配到两个结果man和woman。

这里,我们需要借助@Resource注解的name属性或@Qualifier来确定一个合格的实现类

代码修改为 

        @Resource(name="woman")
	private Human human;

或 

        @Resource
	@Qualifier("woman")
	private Human human;

上面,我们指定了Human接口的实现类是Woman.java,启动springboot,访问 http://localhost:8080/an/run

改动三:

在改动二的基础上,将注解替换为@Autowired,启动报错

Description:

Field human in com.example.annotation.controller.HumanController required a single bean, but 2 were found:
	- man: defined in file [D:\DEV_ENV\springbootws\annotation\target\classes\com\example\annotation\service\impl\Man.class]
	- woman: defined in file [D:\DEV_ENV\springbootws\annotation\target\classes\com\example\annotation\service\impl\Woman.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

报错信息很明显,HumanController需要一个bean实现,但是找到了两个 man 和woman

解决方案:使用@Primary注解,在有多个实现bean时告诉spring首先@Primary修饰的那个;或者使用@Qualifier来标注需要注入的类。

@Qualifier修改方式与改动二的相同,依然是修改HumanController.java 中间注入的Human上面,这里不再复述

@Primary是修饰实现类的,告诉spring,如果有多个实现类时,优先注入被@Primary注解修饰的那个。这里,我们希望注入Man.java ,那么修改Man.java为

package com.example.annotation.service.impl;

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import com.example.annotation.service.Human;

/**
 * service接口第一实现类
 * @author Administrator
 */
@Service
@Primary
public class Man implements Human {

	public String runMarathon() {
		return "A man run marathon";
	}
}

启动springboot后,可以看到注入的已经是Man.java 了。

以上,个人学习总结,如有不当之处,欢迎留言批评斧正。谢谢

  • 128
    点赞
  • 472
    收藏
    觉得还不错? 一键收藏
  • 31
    评论
### 回答1: @Resource注解是Java中的一种注解,它可以用来标记一个属性、方法或构造函数,告诉容器在进行自动装配时,使用名称或类型来查找需要注入的bean。 @Autowired注解是Spring框架中的一种注解,它可以用来标记一个属性、方法或构造函数,告诉容器在进行自动装配时,使用类型来查找需要注入的bean。 ### 回答2: @resource注解和@Autowired注解都是用于在Spring框架中进行依赖注入的方式。 @resource注解是JSR 250规范中的一部分,它可以用来标记一个类的属性或方法来指示Spring容器注入一个特定的资源。可以应用在字段上,在setter方法上,以及在构造函数中,用来注入具体类型的依赖。它可以使用一个name属性来指定要注入的资源的名称,如果没有指定name属性,它将根据变量的名称来查找匹配的资源。@resource注解可以用于注入任何类型的资源,包括其他对象、数据源、事务管理器等。 @Autowired注解是Spring框架提供的一种依赖注入方式。它可以通过类型来自动注入一个合适的bean或者其他的依赖项。可以应用在字段上,setter方法上,以及构造函数中。与@resource注解不同的是,@Autowired注解不需要明确指定需要注入的bean的名称,它会根据类型来自动寻找匹配的bean,并将其注入到对应的属性中。如果存在多个相同类型的bean,可以使用@Qualifier注解来指定要注入的bean的名称。在进行注入时,Spring会根据属性的类型,从容器中查找匹配的bean,并将其自动装配到指定的属性中。 总结来说,@resource注解主要用于注入各种类型的资源,可以指定资源的名称,而@Autowired注解主要用于自动注入其他对象或者依赖项,根据类型自动寻找匹配的bean进行注入。 ### 回答3: @Resource注解和@Autowired注解都是Spring框架中用于进行依赖注入的注解。 @Resource注解是JavaEE的注解,可以用于引用其他组件或者资源。在Spring中,它可以用于注入依赖的组件。@Resource注解有两个常用的属性:name和type。name属性可以指定要注入的组件的名称,type属性可以指定要注入的组件的类型。如果两个属性都没有指定,则根据属性的类型进行自动装配。 @Autowired注解是Spring框架提供的注解,用于进行自动装配。它可以根据属性的类型进行自动装配,也可以根据属性的名称进行自动装配。当需要装配的组件只有一个时,会根据类型进行匹配。当需要装配的组件有多个时,会根据属性名称进行匹配。如果找不到匹配的组件,会抛出异常。 在使用@Resource注解时,可以使用@Resource和@Autowired注解的效果是一样的,但是@Resource注解的功能更加强大,可以引用其他JavaEE容器的资源,而@Autowired注解只能引用Spring容器中的组件。 总结起来,@Resource注解是JavaEE的注解,用于引用其他组件或资源,而@Autowired是Spring框架的注解,用于进行自动装配。在Spring中,可以使用@Resource和@Autowired注解来实现依赖注入的功能。
评论 31
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值