注解 @Autowired @Qualifier @Resource 的区别
参考:https://blog.csdn.net/u010502101/article/details/78950045
参考:https://www.cnblogs.com/joe-tang/p/9213545.html
参考:https://blog.51cto.com/qiangmzsx/1359952
参考:https://www.cnblogs.com/think-in-java/p/5474740.html 推荐这个
一、区别
1、@Autowired 按类型自动装配
2、@Qualifier 按名称进行装配,与@Autowired搭配使用
3、@Resource 可以先按名称再按类型自动进行装配,如果指定了名称,只能按名称进行装配,是J2EE规范,@Autowired和@Qualifier 是spring规范。
二、说明
1、@Autowired这个注解是spring定义的,根据类型自动注入,如果Spring配置了component scan,并且要注入的接口只有一个实现的话,那么spring框架可以自动将interface于实现组装起来。
2、一个接口有多个实现,那么就需要@Qualifier和@Autowired一起使用来标明。
3、@Resource是JDK1.6支持的注解,默认按照名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名,按照名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。如果name和type都使用,还是按name去匹配,匹配不到报错(这个是不容易被理解的地方)。
特别说明:@Resource 的type就是个鸡肋,不建议使用,容易引起歧义与混淆。
三、实战
1、@Autowired 按类型匹配。
A、定义了接口
1 package com.yzc.springMVC.service; 2 3 /** 4 * 5 * @Title: AutowiredController.java 6 * @Package: com.yzc.springMVC 7 * @Description: 描述该文件做什么 8 * @author yangzhancheng 9 * @2020年4月19日:下午10:39:55 10 * 11 */ 12 13 public interface IAutowiredService { 14 15 String action(); 16 }
B、一个实现类
1 package com.yzc.springMVC.service; 2 3 import org.springframework.stereotype.Service; 4 5 /** 6 * 7 * @Title: AutowiredServiceOne.java 8 * @Package: com.yzc.springMVC.service 9 * @Description: 按类型进行注解 10 * @author yangzhancheng 11 * @2020年4月19日:下午10:56:42 12 * 13 */ 14 @Service 15 public class AutowiredServiceTYPE implements IAutowiredService{ 16 17 public String action(){ 18 String msg = "AutowiredServiceTYPE:是按类型。"; 19 return msg; 20 } 21 }
C、Controller 类中根据类型自动匹配
1 package com.yzc.springMVC.controller; 2 3 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.ResponseBody; 8 9 import com.yzc.springMVC.service.IAutowiredService; 10 11 12 /** 13 * 14 * @Title: AutowiredController.java 15 * @Package: com.yzc.springMVC 16 * @Description: 描述该文件做什么 17 * @author yangzhancheng 18 * @2020年4月19日:下午10:39:55 19 * 20 */ 21 @Controller 22 @RequestMapping(value = "/autowired") 23 public class AutowiredController { 24 25 /** 26 * 通过类自动匹配 27 */ 28 @Autowired 29 private IAutowiredService autowiredService; 30 31 @RequestMapping(value = "/action",produces = "application/json;charset=UTF-8") 32 @ResponseBody 33 public String action(){ 34 String msg = autowiredService.action(); 35 System.out.print(msg); 36 return msg; 37 } 38 39 40 }
E、运行结果
AutowiredServiceTYPE:是按类型。
2、@Autowired 按类型匹配,如果两个接口实现了本接口,通过类型匹配时出错。
1 package com.yzc.springMVC.service; 2 3 import org.springframework.stereotype.Service; 4 5 /** 6 * 7 * @Title: AutowiredServiceOne.java 8 * @Package: com.yzc.springMVC.service 9 * @Description: 按名称进行注解 10 * @author yangzhancheng 11 * @2020年4月19日:下午10:56:42 12 * 13 */ 14 @Service 15 public class AutowiredServiceNAME implements IAutowiredService{ 16 17 public String action(){ 18 String msg = "AutowiredServiceTYPE:是按名称。"; 19 return msg; 20 } 21 }
系统运行时报错,说明@Autowired 在spring加载时出现问题。
1 四月 23, 2020 3:48:03 下午 org.springframework.context.support.AbstractApplicationContext refresh 2 警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'autowiredController': Unsatisfied dependency expressed through field 'autowiredService'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.yzc.springMVC.service.IAutowiredService' available: expected single matching bean but found 2: autowiredServiceNAME,autowiredServiceTYPE 3 四月 23, 2020 3:48:03 下午 org.springframework.web.servlet.FrameworkServlet initServletBean 4 严重: Context initialization failed 5 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'autowiredController': Unsatisfied dependency expressed through field 'autowiredService'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.yzc.springMVC.service.IAutowiredService' available: expected single matching bean but found 2: autowiredServiceNAME,autowiredServiceTYPE 6 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) 7 at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) 8 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) 9 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) 10 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) 11 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) 12 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) 13 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 14 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) 15 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 16 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882) 17 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) 18 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) 19 at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:702) 20 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:668) 21 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:716) 22 at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:591) 23 at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:530) 24 at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:170) 25 at javax.servlet.GenericServlet.init(GenericServlet.java:158) 26 at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1144) 27 at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1091) 28 at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:985) 29 at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4887) 30 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5201) 31 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) 32 at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1412) 33 at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1402) 34 at java.util.concurrent.FutureTask.run(FutureTask.java:266) 35 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 36 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 37 at java.lang.Thread.run(Thread.java:745) 38 Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.yzc.springMVC.service.IAutowiredService' available: expected single matching bean but found 2: autowiredServiceNAME,autowiredServiceTYPE 39 at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:220) 40 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1268) 41 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1210) 42 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) 43 ... 31 more 44 45 四月 23, 2020 3:48:03 下午 org.apache.catalina.core.ApplicationContext log 46 严重: StandardWrapper.Throwable
其中本提示十分明确:
No qualifying bean of type 'com.yzc.springMVC.service.IAutowiredService' available: expected single matching bean but found 2: autowiredServiceNAME,autowiredServiceTYPE
2、@Autowired 和 @Qualifer 结合使用,在原来的注解上增加@Qualifer,指定名称
A、指定service的名称
1 package com.yzc.springMVC.service; 2 3 import org.springframework.stereotype.Service; 4 5 /** 6 * 7 * @Title: AutowiredServiceOne.java 8 * @Package: com.yzc.springMVC.service 9 * @Description: 按名称进行注解 10 * @author yangzhancheng 11 * @2020年4月19日:下午10:56:42 12 * 13 */ 14 @Service("autowiredServiceBYNAME") 15 public class AutowiredServiceNAME implements IAutowiredService{ 16 17 public String action(){ 18 String msg = "AutowiredServiceTYPE:是按名称。"; 19 return msg; 20 } 21 }
B、默认Service的名称是类名小写 autowiredServiceTYPE
1 package com.yzc.springMVC.service; 2 3 import org.springframework.stereotype.Service; 4 5 /** 6 * 7 * @Title: AutowiredServiceOne.java 8 * @Package: com.yzc.springMVC.service 9 * @Description: 按类型进行注解 10 * @author yangzhancheng 11 * @2020年4月19日:下午10:56:42 12 * 13 */ 14 @Service 15 public class AutowiredServiceTYPE implements IAutowiredService{ 16 17 public String action(){ 18 String msg = "AutowiredServiceTYPE:是按类型。"; 19 return msg; 20 } 21 }
C 、Controller类中内容
1 package com.yzc.springMVC.controller; 2 3 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.beans.factory.annotation.Qualifier; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.ResponseBody; 9 10 import com.yzc.springMVC.service.IAutowiredService; 11 12 13 /** 14 * 15 * @Title: AutowiredController.java 16 * @Package: com.yzc.springMVC 17 * @Description: 描述该文件做什么 18 * @author yangzhancheng 19 * @2020年4月19日:下午10:39:55 20 * 21 */ 22 @Controller 23 @RequestMapping(value = "/autowired") 24 public class AutowiredController { 25 26 /** 27 * 通过类自动匹配 28 */ 29 @Autowired 30 @Qualifier("autowiredServiceBYNAME") 31 private IAutowiredService autowiredService; 32 33 @Autowired 34 @Qualifier("autowiredServiceTYPE") 35 private IAutowiredService autowiredServiceTYPE; 36 37 @RequestMapping(value = "/action",produces = "application/json;charset=UTF-8") 38 @ResponseBody 39 public String action(){ 40 String msg = autowiredService.action(); 41 System.out.println(msg); 42 return msg; 43 } 44 45 46 47 @RequestMapping(value = "/actionName",produces = "application/json;charset=UTF-8") 48 @ResponseBody 49 public String actionByName(){ 50 String msg = autowiredServiceTYPE.action(); 51 System.out.println(msg); 52 return msg; 53 } 54 55 56 }
运行结果
postMan请求地址:
http://localhost:8888/webdemo/autowired/action
运行结果
"AutowiredServiceTYPE:是按名称。"
postMan请求地址:
http://localhost:8888/webdemo/autowired/actionName
运行结果
"AutowiredServiceTYPE:是按类型。"
3、@Resource 使用情况
A、默认先按名字查找,如果没有名字,按类型匹配
1 package com.yzc.springMVC.service; 2 3 /** 4 * 5 * @Title: IResourceService.java 6 * @Package: com.yzc.springMVC.service 7 * @Description: 通过Resource资源实现 8 * @author yangzhancheng 9 * @2020年4月23日:下午4:35:45 10 * 11 */ 12 public interface IResourceService { 13 14 String action(); 15 }
1 package com.yzc.springMVC.service; 2 3 import org.springframework.stereotype.Service; 4 5 /** 6 * 7 * @Title: ResourceServiceA.java 8 * @Package: com.yzc.springMVC.service 9 * @Description: 描述该文件做什么 10 * @author yangzhancheng 11 * @2020年4月23日:下午4:37:20 12 * 13 */ 14 @Service 15 public class ResourceServiceA implements IResourceService{ 16 17 public String action(){ 18 String msg = "ResourceServiceA"; 19 return msg; 20 } 21 }
1 package com.yzc.springMVC.controller; 2 3 import javax.annotation.Resource; 4 5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.ResponseBody; 8 9 import com.yzc.springMVC.service.IResourceService; 10 11 /** 12 * 13 * @Title: ResourceController.java 14 * @Package: com.yzc.springMVC.controller 15 * @Description: 测试Resource 16 * @author yangzhancheng 17 * @2020年4月23日:下午4:34:08 18 * 19 */ 20 @Controller 21 @RequestMapping(value = "/resource") 22 public class ResourceController { 23 24 /** 25 * 先通过名字匹配,如果匹配不到,报错.本例子为找不到,报错 26 */ 27 @Resource 28 private IResourceService resourceService; 29 30 @RequestMapping(value = "/action", produces = "application/json;charset=UTF-8") 31 @ResponseBody 32 public String action() { 33 String msg = resourceService.action(); 34 System.out.println(msg); 35 return msg; 36 } 37 38 }
POSTman调用
1 http://localhost:8888/webdemo/resource/action
运行结果
1 "ResourceServiceA"
B、默认先按名字查找,如果匹配到对应的名称的资源,不再按类型匹配
1 package com.yzc.springMVC.service; 2 3 import org.springframework.stereotype.Service; 4 5 /** 6 * 7 * @Title: ResourceServiceA.java 8 * @Package: com.yzc.springMVC.service 9 * @Description: 描述该文件做什么 10 * @author yangzhancheng 11 * @2020年4月23日:下午4:37:20 12 * 13 */ 14 @Service 15 public class ResourceServiceA implements IResourceService{ 16 17 public String action(){ 18 String msg = "ResourceServiceA"; 19 return msg; 20 } 21 }
定义名称为resourceServiceByName
1 package com.yzc.springMVC.service; 2 3 import org.springframework.stereotype.Service; 4 5 /** 6 * 7 * @Title: ResourceServiceA.java 8 * @Package: com.yzc.springMVC.service 9 * @Description: 描述该文件做什么 10 * @author yangzhancheng 11 * @2020年4月23日:下午4:37:20 12 * 13 */ 14 @Service("resourceServiceByName") 15 public class ResourceServiceB implements IResourceService{ 16 17 public String action(){ 18 String msg = "ResourceServiceB"; 19 return msg; 20 } 21 }
在ResourceController,用@Resource匹配名称类型
1 package com.yzc.springMVC.controller; 2 3 import javax.annotation.Resource; 4 5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.ResponseBody; 8 9 import com.yzc.springMVC.service.IResourceService; 10 11 /** 12 * 13 * @Title: ResourceController.java 14 * @Package: com.yzc.springMVC.controller 15 * @Description: 测试Resource 16 * @author yangzhancheng 17 * @2020年4月23日:下午4:34:08 18 * 19 */ 20 @Controller 21 @RequestMapping(value = "/resource") 22 public class ResourceController { 23 24 /** 25 * 先通过名字匹配,如果匹配到,不在匹配类型 26 */ 27 @Resource 28 private IResourceService resourceServiceByName; 29 30 @RequestMapping(value = "/action", produces = "application/json;charset=UTF-8") 31 @ResponseBody 32 public String action() { 33 String msg = resourceServiceByName.action(); 34 System.out.println(msg); 35 return msg; 36 } 37 38 }
POSTMAN 执行
1 http://localhost:8888/webdemo/resource/action
运行结果
1 "ResourceServiceB"
先通过名字找的。
C、如果指定了名称,只找对应名称,否则报错
注解了一个不存名字的,虽然类型都存在 @Resource(name="resourceServiceByNameOther")
1 package com.yzc.springMVC.controller; 2 3 import javax.annotation.Resource; 4 5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.ResponseBody; 8 9 import com.yzc.springMVC.service.IResourceService; 10 11 /** 12 * 13 * @Title: ResourceController.java 14 * @Package: com.yzc.springMVC.controller 15 * @Description: 测试Resource 16 * @author yangzhancheng 17 * @2020年4月23日:下午4:34:08 18 * 19 */ 20 @Controller 21 @RequestMapping(value = "/resource") 22 public class ResourceController { 23 24 /** 25 * 先通过名字匹配,如果匹配到,不在匹配类型 26 */ 27 @Resource(name="resourceServiceByNameOther") 28 private IResourceService resourceServiceByName; 29 30 @RequestMapping(value = "/action", produces = "application/json;charset=UTF-8") 31 @ResponseBody 32 public String action() { 33 String msg = resourceServiceByName.action(); 34 System.out.println(msg); 35 return msg; 36 } 37 38 }
启动报错
1 四月 23, 2020 6:18:43 下午 org.springframework.context.support.AbstractApplicationContext refresh 2 警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'resourceServiceByNameOther' available 3 四月 23, 2020 6:18:43 下午 org.springframework.web.servlet.FrameworkServlet initServletBean 4 严重: Context initialization failed 5 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'resourceServiceByNameOther' available 6 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:337) 7 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) 8 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) 9 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) 10 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) 11 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 12 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) 13 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 14 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882) 15 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) 16 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) 17 at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:702) 18 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:668) 19 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:716) 20 at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:591) 21 at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:530) 22 at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:170) 23 at javax.servlet.GenericServlet.init(GenericServlet.java:158) 24 at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1144) 25 at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1091) 26 at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:985) 27 at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4887) 28 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5201) 29 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) 30 at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3761) 31 at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:297) 32 at org.apache.catalina.core.StandardContext.backgroundProcess(StandardContext.java:5539) 33 at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381) 34 at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1385) 35 at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1385) 36 at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1353) 37 at java.lang.Thread.run(Thread.java:745) 38 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'resourceServiceByNameOther' available 39 at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:808) 40 at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1279) 41 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297) 42 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207) 43 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeanByName(AbstractAutowireCapableBeanFactory.java:454) 44 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:543) 45 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:513) 46 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:653) 47 at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:239) 48 at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) 49 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:334) 50 ... 31 more 51 52 四月 23, 2020 6:18:43 下午 org.apache.catalina.core.ApplicationContext log 53 严重: StandardWrapper.Throwable 54 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'resourceServiceByNameOther' available 55 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:337) 56 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) 57 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) 58 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) 59 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) 60 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 61 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) 62 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 63 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882) 64 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) 65 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) 66 at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:702) 67 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:668) 68 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:716) 69 at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:591) 70 at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:530) 71 at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:170) 72 at javax.servlet.GenericServlet.init(GenericServlet.java:158) 73 at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1144) 74 at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1091) 75 at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:985) 76 at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4887) 77 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5201) 78 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) 79 at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3761) 80 at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:297) 81 at org.apache.catalina.core.StandardContext.backgroundProcess(StandardContext.java:5539) 82 at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381) 83 at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1385) 84 at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1385) 85 at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1353) 86 at java.lang.Thread.run(Thread.java:745) 87 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'resourceServiceByNameOther' available 88 at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:808) 89 at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1279) 90 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297) 91 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207) 92 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeanByName(AbstractAutowireCapableBeanFactory.java:454) 93 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:543) 94 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:513) 95 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:653) 96 at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:239) 97 at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) 98 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:334) 99 ... 31 more 100 101 四月 23, 2020 6:18:43 下午 org.apache.catalina.core.StandardContext loadOnStartup 102 严重: Servlet [spring] in web application [/webdemo] threw load() exception 103 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'resourceServiceByNameOther' available 104 at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:808) 105 at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1279) 106 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297) 107 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207) 108 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeanByName(AbstractAutowireCapableBeanFactory.java:454) 109 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:543) 110 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:513) 111 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:653) 112 at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:239) 113 at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) 114 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:334) 115 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) 116 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) 117 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) 118 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) 119 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 120 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) 121 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 122 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882) 123 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) 124 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) 125 at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:702) 126 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:668) 127 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:716) 128 at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:591) 129 at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:530) 130 at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:170) 131 at javax.servlet.GenericServlet.init(GenericServlet.java:158) 132 at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1144) 133 at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1091) 134 at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:985) 135 at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4887) 136 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5201) 137 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) 138 at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3761) 139 at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:297) 140 at org.apache.catalina.core.StandardContext.backgroundProcess(StandardContext.java:5539) 141 at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381) 142 at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1385) 143 at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1385) 144 at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1353) 145 at java.lang.Thread.run(Thread.java:745) 146 147 四月 23, 2020 6:18:43 下午 org.apache.catalina.core.StandardContext reload 148 信息: Reloading Context with name [/webdemo] is completed
无法识别bean :No bean named 'resourceServiceByNameOther' available