controller层中注入controller

简介: 在controller中注入了另一个controller,按理说,三个注解controller、service、respository都是component衍生而来,任何被注入容器中的bean都可以以同样方式获取,但是在使用springboot项目时,controller中注入另一个controller发现失败。改成注入service成功。

问题描述:在controller中注入了另一个controller,按理说,三个注解controller、service、respository都是component衍生而来,任何被注入容器中的bean都可以以同样方式获取,但是在使用springboot项目时,controller中注入另一个controller发现失败。改成注入service成功。


报错:


2020-12-08 15:17:18.812 WARN 12352 — [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘testController’: Injection of @DubboReference dependencies is failed; nested exception is java.lang.IllegalArgumentException: @Service interfaceClass() or interfaceName() or interface class must be present!

2020-12-08 15:17:18.813 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy11@1410d645 was destroying!

2020-12-08 15:17:18.814 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy9@4ccdacf5 was destroying!

2020-12-08 15:17:18.814 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy6@5aa6da2 was destroying!

2020-12-08 15:17:18.814 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy0@1a0d313 was destroying!

2020-12-08 15:17:18.814 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy8@13908f9c was destroying!

2020-12-08 15:17:18.815 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy6@5aa6da2 was destroying!

2020-12-08 15:17:18.815 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy15@329b331f was destroying!

2020-12-08 15:17:18.815 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy7@640a8f93 was destroying!

2020-12-08 15:17:18.815 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy4@6335f04a was destroying!

2020-12-08 15:17:18.815 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy9@4ccdacf5 was destroying!

2020-12-08 15:17:18.816 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy5@74ce7fdf was destroying!

2020-12-08 15:17:18.816 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy14@1f26b992 was destroying!

2020-12-08 15:17:18.816 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy10@2f60cbf2 was destroying!

2020-12-08 15:17:18.816 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy17@631bc9f4 was destroying!

2020-12-08 15:17:18.817 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy12@642c72cf was destroying!

2020-12-08 15:17:18.817 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy16@4e6cbdf1 was destroying!

2020-12-08 15:17:18.817 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy3@67fac095 was destroying!

2020-12-08 15:17:18.818 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy2@5a13f1f7 was destroying!

2020-12-08 15:17:18.818 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy13@5dae5a70 was destroying!

2020-12-08 15:17:18.819 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy1@7686f701 was destroying!

2020-12-08 15:17:18.819 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy8@13908f9c was destroying!

2020-12-08 15:17:18.819 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : org.apache.dubbo.common.bytecode.proxy9@4ccdacf5 was destroying!

2020-12-08 15:17:18.820 INFO 12352 — [ main] f.a.ReferenceAnnotationBeanPostProcessor : class org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor was destroying!

2020-12-08 15:17:18.828 INFO 12352 — [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]

2020-12-08 15:17:18.837 ERROR 12352 — [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Failed to destroy the filter named [Tomcat WebSocket (JSR356) Filter] of type [org.apache.tomcat.websocket.server.WsFilter]


java.lang.AbstractMethodError: null

at org.apache.catalina.core.ApplicationFilterConfig.release(ApplicationFilterConfig.java:301)


问题原因:使用了@Reference去注入对象了,Reference是dubbo的一种注解,作用是注入dubbo容器中对象,dubbo容器与spring容器自然不是一个容器,不可以混用,这里想要controller调用controller只需使用spring的注解@Autowired注解即可


改成service成功原因:@service注解也是dubbo的注解,@Reference注入的就是使用了dubbo的@Service的类的对象。故可以正常使用。


深思:以前学习过boot原理,已经很模糊,需要复习了

一个项目的启动就会有一个spring的容器(不是一个看做一个好了),其他的项目自然是调用不到该容器中的bean了,所以分布式项目中是没有机会使用controller调用controller的了,根本不在一个容器中啊,咋注入。


相关文章
|
3月前
|
前端开发 Java 调度
女朋友要我讲解@Controller注解的原理,真是难为我了
该文章详细解析了Spring MVC中@Controller注解的工作原理,包括Spring如何解析该注解、处理标注的方法以及调度控制器方法以完成HTTP请求的过程。
女朋友要我讲解@Controller注解的原理,真是难为我了
接口模板,文本常用的接口Controller层,常用的controller层模板,Mybatisplus的相关配置
接口模板,文本常用的接口Controller层,常用的controller层模板,Mybatisplus的相关配置
|
6月前
|
前端开发 Java Spring
如果一个控制器上面没有写Controller会怎么样
如果一个控制器上面没有写Controller会怎么样
|
存储 前端开发 Java
Service 层异常抛到 Controller 层处理还是直接处理?
Service 层异常抛到 Controller 层处理还是直接处理?
414 1
|
前端开发 Java Spring
controller层注入的service为null
controller层注入的service为null
176 0
|
SQL Java 程序员
为啥SpringBoot更多普遍采用Controller+Service接口层+ServiceImpl接口实现层
为啥SpringBoot更多普遍采用Controller+Service接口层+ServiceImpl接口实现层
404 0
|
SQL 前端开发 JavaScript
JAVA框架的层级和各层理解 controller,service,dao,mapper,model等层和各包的作用
JAVA框架的层级和各层理解 controller,service,dao,mapper,model等层和各包的作用
896 0
|
Dubbo Java 应用服务中间件
compoment注解与Controller、Service、Repository注解的关系
Controller、Service、Repository这三个注解都是Spring提供的,他们三个有着相似的作用,即将被注解类的实例放入到Spring容器中,作用很相似那他们有什么区别呢?
155 0
compoment注解与Controller、Service、Repository注解的关系
|
JSON 前端开发 JavaScript
如何优雅的写 Controller 层代码?
如何优雅的写 Controller 层代码?
如何优雅的写 Controller 层代码?