Problem, the Dubbo service cannot be automatically injected into the controller by annotations.
However, it can be injected automatically in the service.
Package Com.sl;br/> @Controller
Expect to inject Dubbo service
@Reference (version= "1.0.0")
Inject Service
@Autowired
@RequestMapping ("P")
public void P () throws ioexception{
O is null! not automatically injected
s auto-injection and S.S () Dubbo service is also injected successfully
}
}
Package Com.sl;br/> @Service
Successfully injected Dubbo
@Reference (version= "1.0.0")
< p="">
public void S () throws ioexception{
O is not null! can be used directly
}
}
After analysis, the original configuration resulted in two contexts, one is the root context, one is the context of the SPRINGMVC,
Dubbo annotations are configured in the root context, so the injection of the SPRINGMVC bean cannot be resolved.
Current configuration:
Xml
....
<!--
After Tomcat starts, this setting causes spring to generate the root context and uses the default Applicationcontext.xml to get additional settings.
The root context is saved in ServletContext, and key is Webapplicationcontext.root_web_application_context_attribute
Can be obtained using the Spring tool class: Webapplicationcontextutils.getwebapplicationcontext (ServletContext);
-
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
...
springmvc Org.springframework.web.servlet.DispatcherServle T 1 </servlet>applicationcontext.xml ...!--Dubbo use annotations- -> !--The package that needs to be scanned for annotations--> <!-- Dubbo will register postprocessbeforeinitialization in the root context, so the @reference in the beans generated here will be parsed correctly into-->springmvc-servlet.xml ... <!--MVC Annotations Drive-- <!--sprint MVC Annotations Scan Package-- " is not Dubbo configured in the!--Springmvc so the controll bean generated here does not parse @Reference--> ... WORKAROUND: 1. Inject only the Dubbo service into the service and repository instead of the controller, but most of the time it is 2. Remove the Listener in Web. XML and place all the configurations in the Springmvc-servlet.xml to generate only one A context. 3. The springmvc-servlet.xml also joins the Duboo configuration, which, although redundant, guarantees two contexts. You can pay attention to the small, small series to help you answer all doubts.
Big guy teaches you the problem with Dubbo annotation configuration in Springmvc