Big guy teaches you the problem of using Dubbo annotation configuration in Springmvc

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.