凯发娱发k8

@resource与@autowired注解的区别 -凯发娱发k8

2023-08-17,,

一、写本博文的原因

年初刚加入到现在的项目时,在使用时我用的@。后来,同事:你怎么使用@resource注解?我:使用它有错吗?同事:没错,但是现在都使用@autowired。我:我研究一下。

在大学,学习j2ee实训时一直使用的是@resource注解,后来我就养成习惯了。现在对这两个注解做一下解释:

@resource默认按照名称方式进行bean匹配,@autowired默认按照类型方式进行bean匹配
@resource(import javax.annotation.resource;)是j2ee的注解,@autowired( import org.springframework.beans.factory.annotation.autowired;)是spring的注解

spring属于第三方的,j2ee是java自己的东西。使用@resource可以减少代码和spring之间的耦合。

二、@resource注入

现在有一个接口human和两个实现类manimpl、womanimpl,在service层的一个bean中要引用了接口human,这种情况处理如下:

接口human

    package testwebapp.com.wangzuojia.service;
    public interface human {
    public void speak();
    public void walk();
    }

实现类manimpl

    package testwebapp.com.wangzuojia.service.impl;
    import org.springframework.stereotype.service;
    import testwebapp.com.wangzuojia.service.human;
    @service
    public class manimpl implements human {
    public void speak() {
    system.out.println(" man speaking!");
    }
    public void walk() {
    system.out.println(" man walking!");
    }
    }

实现类womanimpl

    package testwebapp.com.wangzuojia.service.impl;
    import org.springframework.stereotype.service;
    import testwebapp.com.wangzuojia.service.human;
    @service
    public class womanimpl implements human {
    public void speak() {
    system.out.println(" woman speaking!");
    }
    public void walk() {
    system.out.println(" woman walking!");
    }
    }

主调类sequenceserviceimpl

    package testwebapp.com.wangzuojia.service.impl;
    import java.util.map;
    import javax.annotation.resource;
    import org.springframework.stereotype.service;
    import testwebapp.com.wangzuojia.dao.sequencemapper;
    import testwebapp.com.wangzuojia.service.human;
    import testwebapp.com.wangzuojia.service.sequenceservice;
    @service
    public class sequenceserviceimpl implements sequenceservice {
    @resource
    private sequencemapper sequencemapper;
    public void generateid(map map) {
    sequencemapper.generateid(map);
    }
    //起服务此处会报错
    @resource
    private human human;
    }

这时候启动tomcat会包如下错误:

严重: exception sendingcontext initialized event to listener instance of classorg.springframework.web.context.contextloaderlistenerorg.springframework.beans.factory.beancreationexception: error creating beanwith name 'sequenceserviceimpl': injection of resource dependencies failed;nested exception isorg.springframework.beans.factory.nouniquebeandefinitionexception: noqualifying bean of type [testwebapp.com.wangzuojia.service.human] is defined:expected single matching beanbut found 2: manimpl,womanimpl atorg.springframework.context.annotation.commonannotationbeanpostprocessor.postprocesspropertyvalues(commonannotationbeanpostprocessor.java:311) atorg.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1214) atorg.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:543) atorg.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:482) atorg.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306) atorg.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) atorg.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302) atorg.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:197) atorg.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:772) atorg.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:838) atorg.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:537) atorg.springframework.web.context.contextloader.configureandrefreshwebapplicationcontext(contextloader.java:446) atorg.springframework.web.context.contextloader.initwebapplicationcontext(contextloader.java:328) atorg.springframework.web.context.contextloaderlistener.contextinitialized(contextloaderlistener.java:107) atorg.apache.catalina.core.standardcontext.listenerstart(standardcontext.java:4717) atorg.apache.catalina.core.standardcontext.startinternal(standardcontext.java:5179)atorg.apache.catalina.util.lifecyclebase.start(lifecyclebase.java:183) atorg.apache.catalina.core.containerbase$startchild.call(containerbase.java:1404) atorg.apache.catalina.core.containerbase$startchild.call(containerbase.java:1394) atjava.util.concurrent.futuretask.run(futuretask.java:266) atjava.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) atjava.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617)atjava.lang.thread.run(thread.java:745) caused by:org.springframework.beans.factory.nouniquebeandefinitionexception: noqualifying bean of type [testwebapp.com.wangzuojia.service.human] is defined: expectedsingle matching bean but found 2: manimpl,womanimpl atorg.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:1126) atorg.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:1014) atorg.springframework.context.annotation.commonannotationbeanpostprocessor.autowireresource(commonannotationbeanpostprocessor.java:508) atorg.springframework.context.annotation.commonannotationbeanpostprocessor.getresource(commonannotationbeanpostprocessor.java:486) atorg.springframework.context.annotation.commonannotationbeanpostprocessor$resourceelement.getresourcetoinject(commonannotationbeanpostprocessor.java:615) atorg.springframework.beans.factory.annotation.injectionmetadata$injectedelement.inject(injectionmetadata.java:169) atorg.springframework.beans.factory.annotation.injectionmetadata.inject(injectionmetadata.java:88) atorg.springframework.context.annotation.commonannotationbeanpostprocessor.postprocesspropertyvalues(commonannotationbeanpostprocessor.java:308) ...22 more

报错的地方给我们提示了:but found 2: manimpl,womanimpl      意思是human有两个实现类。凯发娱发k8的解决方案如下:

    package testwebapp.com.wangzuojia.service.impl;
    import java.util.map;
    import javax.annotation.resource;
    import org.springframework.stereotype.service;
    import testwebapp.com.wangzuojia.dao.sequencemapper;
    import testwebapp.com.wangzuojia.service.human;
    import testwebapp.com.wangzuojia.service.sequenceservice;
    @service
    public class sequenceserviceimpl implements sequenceservice {
    @resource
    private sequencemapper sequencemapper;
    public void generateid(map map) {
    sequencemapper.generateid(map);
    }
    @resource(name = "manimpl")//注意是manimpl不是manimpl,因为使用@service,容器为我们创建bean时默认类名首字母小写
    private human human;
    }

这样启动服务就不会报错了。如果是使用的@autowired注解,要配上@qualifier("manimpl"),代码如下:

      package testwebapp.com.wangzuojia.service.impl;
      import java.util.map;
      import javax.annotation.resource;
      import org.springframework.beans.factory.annotation.autowired;
      import org.springframework.beans.factory.annotation.qualifier;
      import org.springframework.stereotype.service;
      import testwebapp.com.wangzuojia.dao.sequencemapper;
      import testwebapp.com.wangzuojia.service.human;
      import testwebapp.com.wangzuojia.service.sequenceservice;
      @service
      public class sequenceserviceimpl implements sequenceservice {
      @resource
      private sequencemapper sequencemapper;
      public void generateid(map map) {
      sequencemapper.generateid(map);
      }
      @autowired
      @qualifier("manimpl")
      private human human;
      }

@resource与@autowired注解的区别的相关教程结束。

网站地图