Spring's aspect slice class does not intercept the controller's methods

Source: Internet
Author: User
Tags aop

The root cause is the <aop:aspectj-autoproxy/> in the spring configuration file, or in the SPRINGMVC configuration file. If it is within the spring configuration file, the methods in @controller are not intercepted.

Take a look at the configuration of the bean scan in Applicationcontext.xml, which excludes the controller layer scan:

<Context:component-scanBase-package= "com"><Context:exclude-filtertype= "Annotation"expression= "Org.springframework.stereotype.Controller" /></Context:component-scan><Context:component-scanBase-package= "Icom"><Context:exclude-filtertype= "Annotation"expression= "Org.springframework.stereotype.Controller" /></Context:component-scan>

Take a look at the configuration of the bean scan in Springmvc-servlet.xml, which excludes the service layer scan:

<Context:component-scanBase-package= "com"><Context:include-filtertype= "Annotation"expression= "Org.springframework.stereotype.Controller" /><Context:exclude-filtertype= "Annotation"expression= "Org.springframework.stereotype.Service" /></Context:component-scan><Context:component-scanBase-package= "Icom"><Context:include-filtertype= "Annotation"expression= "Org.springframework.stereotype.Controller" /><Context:exclude-filtertype= "Annotation"expression= "Org.springframework.stereotype.Service" /></Context:component-scan>

So to intercept the methods in which layer of class, you should add <aop:aspectj-autoproxy in the corresponding configuration file/>

Workaround:

Comment out the <aop:aspectj-autoproxy/> in the Applicationcontext.xml, and configure it in Springmvc-servlet.xml:

<id= "Aspectbean"  class= "Icom.axx.action.AopAspect"/ ></>

Also, don't forget to add:

xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi:schemalocation=http://www.springframework.org/schema/ Aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd

Unless only one <COMPONENT-SCAN> is configured in the entire Web application, and the base-package contains all the packages to be scanned, the controller's bean is initialized earlier than the other beans.

If the bean scan file in spring is separate from the bean scanning file in Spring-mvc-servlet, then the spring Bean is scanned first, and then the Spring-mvc Bean is scanned. Because spring beans are scanned by the listener Contextloaderlistener, and SPRING-MVC is configured in the Servlet-config.xml <component-scan> The bean is at the servlet level, and the listener's boot order is naturally earlier than the servlet's boot order.

The real reason that AOP cannot be cut to the controller is the position of <aop:aspectj-autoproxy/>.

More specific reason: if you are in the two configuration files: Spring-config.xml Springmv-servlet.xml is configured to scan all the packages, then you will find all the beans are successively instantiated once, first Spring-config.xml ; after Springmv-servlet.xml. The beans that were instantiated are placed in a parent-child container.

When Acontroller is initialized for the first time, it is in Spring-config.xml (Aspectj-autoproxy configuration), and this acontrollerbean is initialized to the AOP proxy object, which exists in the parent container.

Then there is no aspectj-autoproxy in the Springmv-servlet.xml, so Acontrollerbean is a common bean, which exists in the sub-container and is not represented by an AOP agent.

When a URL accesses a controller, it uses the normal controllerbean in the child container, so AOP cannot be cut.

Therefore, in the project development, it is best to clear the boundaries of responsibility, Spring-application-config.xml is only responsible for kernel bean initialization; Springmvc-servlet-config.xml is only responsible for the bean associated with MVC: controller, Handlermapping, Handleradapter.

Reference:

Https://www.cnblogs.com/Frank-Hao/p/5787813.html

Spring's aspect slice class does not intercept the controller's methods

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.