Springboot Autowried及Resouce使用对比解析

所属分类: 软件编程 / java 阅读数: 45
收藏 0 赞 0 分享

在做项目时,发现项目中 加载类时,有的地方使用@Autowired,有的地方使用@Resource

在网上搜集了资料

共同点

@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注解一起来修饰。

写列子

新建 HumanService.java类

package com.komiles.study.service;

/**
 * @author komiles@163.com
 * @date 2020-03-23 11:46
 */
public interface HumanService {

  /**
   * 跑马拉松
   * @return
   */
  String runMarathon();
}

实现类 ManServiceImpl.java

package com.komiles.study.service.impl;

import com.komiles.study.service.HumanService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author komiles@163.com
 * @date 2020-03-23 11:48
 */
@Service
public class ManServiceImpl implements HumanService {
  /**
   * 跑马拉松
   */
  @Override
  public String runMarathon() {
    return " A man run marathon";
  }
}

新建HumanController.java

package com.komiles.study.controller;

import com.komiles.study.service.HumanService;
import com.komiles.study.service.impl.ManServiceImpl;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author komiles@163.com
 * @date 2020-03-23 11:49
 */
@RestController
@RequestMapping("/human")
public class HumanController {

  @Autowired
  private HumanService humanService;

  @GetMapping("/run")
  public String runMarathon()
  {
    return humanService.runMarathon();
  }
}

运行程序

输出内容为: man run marathon

把controller里的 @Autowired 改成@Resource 也能正常访问。

假如我写多个实现类会怎么样呢?

新建一个 WomanServiceImpl.java

package com.komiles.study.service.impl;

import com.komiles.study.service.HumanService;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author komiles@163.com
 * @date 2020-03-23 12:01
 */
@Service
public class WomanServiceImpl implements HumanService {

  /**
   * 跑马拉松
   */
  @Override
  public String runMarathon() {
    return "A Woman run marathon";
  }
}

运行程序,发现报错了,因为有两个实现类,程序不知道找那个了

怎么办呢?

有两种办法

第一种,在实现类中给类起名字,在引入的时候直接引入名字。

例如:在ManServiceImpl.java类,@Service上加值。@Service(value = "manService") 或者 @Component(value = "manService")

package com.komiles.study.service.impl;

import com.komiles.study.service.HumanService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author komiles@163.com
 * @date 2020-03-23 11:48
 */
@Service(value = "manService")
//@Component(value = "manService")
public class ManServiceImpl implements HumanService {

  /**
   * 跑马拉松
   */
  @Override
  public String runMarathon() {
    return " A man run marathon";
  }
}

在Controller类中使用时,也需要制定一下名字。

如果使用@Resource 需要加上 @Resource(name="manService")

如果使用@Autowired 需要使用@Qualifier(value="manService")

package com.komiles.study.controller;

import com.komiles.study.service.HumanService;
import com.komiles.study.service.impl.ManServiceImpl;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author komiles@163.com
 * @date 2020-03-23 11:49
 */
@RestController
@RequestMapping("/human")
public class HumanController {

  @Autowired
  @Qualifier(value = "manService")
//  @Resource(name="manService")

  private HumanService humanService;

  @GetMapping("/run")
  public String runMarathon()
  {
    return humanService.runMarathon();
  }
}

如果想优先引用某一个类,可以在实现类上使用 @Primary。

项目代码:

https://github.com/KoMiles/springboot/blob/master/src/main/java/com/komiles/study/controller/HumanController.java

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多