Issues that spring + spring MVC may encounter

Source: Internet
Author: User

The spring container first loads the parent container generated by Servletcontextlistener (corresponding applicationcontext.xml), and SPRINGMVC (corresponding Mvc_dispatcher_ Servlet.xml) produces a sub-container. The instance of the @service annotation assembled when the sub-container controller is scanned for assembly is not transacted, that is, a service with no transaction capability, and the service that the parent container initializes is a guarantee of the transaction's enhanced processing power. If the service is not exclude in the sub-container, the service with no transaction capability will be available at this time. Therefore, the service scan should not be included in the Spring MVC configuration file.

Spring MVC:

<Context:component-scanBase-package= "Com.pamirs.pradar.controller">        <Context:include-filtertype= "Annotation"expression= "Org.springframework.stereotype.Controller" />        <Context:exclude-filtertype= "Annotation"expression= "Org.springframework.stereotype.Service" />    </Context:component-scan>

Spring

<base-package= "Com.pamirs.pradar.controller">          <  type= "annotation"            expression= " Org.springframework.stereotype.Controller "/>    </Context: Component-scan>

Add @transactional main attention to the issue:

<tx:annotation-driven/> looks for @Transactional on beans in the same application context it's defined in. This means, if you put <tx:annotation-driven/> a webapplicationcontext for a dispatcherservlet, it is only CHEC KS for @Transactional beans in your controllers, and not your services.

This means that,<tx:annoation-driven/> only looks for the @transactional annotation above the bean that is defined in the same application context file, and if you put it in the dispatcher application context, It only checks the @transactional annotations on the controller, not the @transactional annotations on your services.

I then defined the transaction configuration in the application context of Spring MVC (*-servlet.xml), @transactional the annotations on the controller, and finally the transaction worked.

Issues that spring + spring MVC may encounter

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.