凯发娱发k8

@resource与@autowired注解的区别踩坑者入 -凯发娱发k8

2023-08-17,,

一、写本博文的原因

有些童鞋搞不为什么要用@或者@autowired,咱们一起研究下

@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

    public interface human {
    public void speak();
    public void walk();
    }

实现类manimpl

@service
public class manimpl implements human {

public void speak() {
system.out.println(" man speaking!");

}

public void walk() {
system.out.println(" man walking!");

}

}

实现类womanimp

@service
public class womanimpl implements human {

public void speak() {
system.out.println(" woman speaking!");

}

public void walk() {
system.out.println(" woman walking!");

}

}

主调类sequenceserviceimpl

@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的解决方案如下:

@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"),代码如下:

@service
public class sequenceserviceimpl implements sequenceservice {

@resource
private sequencemapper sequencemapper;
public void generateid(map map) {
sequencemapper.generateid(map);

}

@autowired
@qualifier("manimpl")
private human human;

}

希望对你有帮助,欢迎交流 qq3245792286

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

网站地图