1 Problem Description

When using Spring AOP to facet intercept methods of the Controller class at the Controller layer, it does not work. There are no problems with the AOP configuration.

2 Troubleshooting Process

  1. There are no problems with the Spring AOP configuration; “Normal”
  2. Breakpoint debugging: Spring source breakpoint debugging, when the Controller method is called, the Controller instance is dynamically propped by the JDK; 【 abnormal 】
  3. Spring’s default proxy is JDK dynamic proxy; “Normal”

3 Problem Solving

Some people say AOP can’t intercept Controller. Some say try AnnotationMethodHandlerAdapter cut to stop the Controller have to intercept org. Springframework. Web. Servlet. MVC. The annotation. AnnotationMethodHand LerAdapter.

First AOP can intercept to the Controller, the second is no doubt should intercept AnnotationMethodHandlerAdapter is not a must. At least I haven’t been able to verify this. This method will not be introduced here.

The reason some people in AOP say Controller cannot be intercepted is because the annotated Controller is already propped internally by the Spring container. We just hand it over to the Cglib agent. Spring MVC configuration file dispatcher-servlet.xml:

<! -- Tell Spring to use cglib instead of JDK to generate proxy methods AOP can intercept Controller -->
<aop:aspectj-autoproxy proxy-target-class="true" />
Copy the code

4. Problem Summary

When SpringMVC is integrated with Spring, the SpringMVC dispatcher-servlet. XML file is configured to scan the package and does not include annotations for the service. When configuring the scan package in Spring’s applicationContext.xml file, do not include the controller annotation, as follows:

Spring MVC dispatcher – servlet. XML:

<context:component-scan base-package="com.qding">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
Copy the code

The configuration file at Spring MVC startup contains component scanning, URL mapping, and setting the freemarker parameter so that Spring does not scan classes annotated with @Service. Why do I do this? Since springMVC.xml and ApplicationContext.xml are not loaded at the same time, if this is not done, Spring will scan all @Service annotated classes into the container. When the applicationContext is loaded, cglib will not delegate the Service because the container already has the Service class. As a result, the transaction configuration in the applicationContext will not work. Data cannot be rolled back. That’s why.

Likewise, the applicationContext.xml configuration in Spring is as follows:

<context:component-scan base-package="com.qding">           
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
Copy the code

Context :component-scan Scans annotations on classes in the specified package. Common annotations include: context:component-scan

@controller Action component @service Service component @service ("myMovieLister"@requestMapping (RequestMapping) @requestMapping (RequestMapping)"/menu"The request maps @Resource for injection, which (provided by J2EE) is assembled by name by default, @resource (name="beanName"@transactional (rollbackFor={exception.class}) transaction manager@responseBody @scope ()"prototype") sets the scope of the beanCopy the code