Spring AOP: Problems with the implementation of AOP for controllers with annotations

Source: Internet
Author: User

In the premise 1:web.xml:

<servlet><servlet-name>Spring-mvc</servlet-name><servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class><init-param><param-name> Contextconfiglocation</param-name><param-value>/web-inf/dispatcherservlet.xml</param-value> </init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping>< Servlet-name>spring-mvc</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping >

  

Premise 2:dispatcherservlet.xml (another is a few interceptor)

Premise 3: The case described later, in <aop:aspectj-autoproxy proxy-target-class= "true"/> with <aop:aspectj-autoproxy Proxy-target-class= "false"/> Two premise, the result is no difference.

<context:component-scan base-package= "com" ></context:component-scan><mvc:annotation-driven/> <aop:aspectj-autoproxy/>

The premise 4:controller is com. Basecontroller, <context:component-scan base-package= "com" ></context:component-scan> generated

@Component @aspectpublic ClassAopdefault {Private LongBeginTime; @Pointcut ("Execution (* org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle (..))"private void Aspect () {}//defines a pointcut@Pointcut ("Execution (* com. basecontroller.* (..)) "private void _aspect () {}//defines a pointcut@Before ("Execution (* com. basecontroller.* (..)) ") Defining a pre-notification public voidDobefore () {beginTime =System.currenttimemillis (); System.out.println ("Dobefore"); } @AfterReturning ("Execution (* com. basecontroller.* (..)) ") Define post notification public voidDoafter (Joinpoint joinpoint) {String ClassName =joinpoint.gettarget (). GetClass (). GetName (); String method = joinpoint.getsignature (). GetName (); Long endTime = system.currenttimemillis (); Long duration= endtime-BeginTime; System.out.println ("Doafter"); System.out.println ("Method:" +classname+ ".") +method); System.out.println ("Elapsed time:" +duration + "MS"); System.out.println ("");} Doaround Annotation Method 1 @Around ("aspect ()") public void Doaround () {System.out.println ("doaround_1");}// Doaround Annotation Method 2 @Around ("_aspect ()") public void _doaround () {System.out.println ("doaround_2");}// Doaround Annotation Method 3 @Around ("Execution (* Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle (..)) " Public void _doaround_ () {System.out.println ("doaround_3");}}      

Results :

Console (Obviously, "doaround annotation Method 1" and "Doaround annotation Method 3" Do not work at all, and before,afterreturning is OK, indicating

This notation is not valid for defining @pointcut, but it is valid to write directly in the method, and

("Execution (* org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle (..))")

This thing, anyway, in my case it's not a bit of a use, say again proxy-target-class= "true" or "false" is useless):

Doaround_2doaftermethod:com. Basecontroller.loginelapsed time:1466612786129ms

Page partial result (error, even if the "return value JSON" condition is removed and no return value is obtained):

ajax.js:51ajax.js:52 4ajax.js:53 parsererrorajax Code is $.ajax ({        URL: "login.do",         type: " Post ",        dataType:" JSON "      

but !!! Once the "Doaround annotation Method 2" is commented out, it is not used ("execution" (* com. basecontroller.* (..)) ") form of @pointcut, the console output and the operation of the page are all normal (here interceptor completely irrelevant), completely normal.

in the comprehensive!!! , I see from the various posts of the various methods of implementing AOP through annotations to the Controller, at least in my practice is not very reliable (also included in expression is ... org.springframework.stereotype.Controller ... Wait, there is no example here), there is only one way to be reliable:

Do not use pointcut, but directly after Before,afterreturning,doaround and other methods write similar ("Execution" (* com. basecontroller.* (..)) ") The form


BTW, sorry to write may be very dizzy, the level is limited.

This is the reference post:

http://itindex.net/detail/50710-springaop-controller-service/

http://yjian84.iteye.com/blog/1920787

http://phoenixfu.iteye.com/blog/2037598

Http://www.oschina.net/question/222929_124314?fromerr=BICW0JoJ

http://usherlight.iteye.com/blog/1306111



Spring AOP: Problems with the implementation of AOP for controllers with annotations

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.