当前位置: 移动技术网 > IT编程>开发语言>Java > Springboot Autowried及Resouce使用对比解析

Springboot Autowried及Resouce使用对比解析

2020年06月14日  | 移动技术网IT编程  | 我要评论

在做项目时,发现项目中 加载类时,有的地方使用@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

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网