A, first introduced in the following Springmvc.xml file (this example is implemented in the frame base ssm)
1, the introduction of namespaces
xmlns:aop="http://www.springframework.org/schema/aop"
2, the xsi: schemaLocation in the introduction of the following ( note see their spring version )
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
3, the <beans> </ beans> add the following node
<! - Open scan packages, without the need to remove the service layer is scanned, avoid transaction failure -> < context: Scan-Component Base-Package = "com.hp" > ! <- Configure the service layer annotations need to scan - -> < context: the exclude filter- type = "annotation" expression The = "org.springframework.stereotype.Service" /> </ context: Component-Scan > ! <- open aop annotations embodiment, this step is not less s, so Notes aop java classes to take effect -> < aop: AspectJ-autoproxy />
Second, cut class
/ ** * the @Aspect will be declared as such cut class * @Component will be handed over to such a spring management * @author WSL * * / @Component the @Aspect public class MyAop { / * * Sayings () method is only a proxy object, personal understanding is that this method plus * after @Pointcut notes other ways just to name value based method to do this, please correct me if wrong * @Pointcut notes the role of meaning prevail exact point where the cut-off point for the definition of value value All methods are at com.hp.controller packet * / @Pointcut (value = "Execution (com.hp.controller .. * *. * (..))" ) Private void Sayings () {} Long Time = 0 ; // Caution method herein before performing a proxy object value defined above sayings ()
@Before (value = "Sayings ()" ) Private void before () { Time = new new . A Date () the getTime (); System.out.println ( "before execution method" ); } // Methods execution @ the after (value = "Sayings ()" ) Private void {after () (System.out.println "executed after execution method, the execution time of" + ( new new a Date () the getTime () -. time)); } // around advice. Note that there must be ProceedingJoinPoint argument. At the same time must have a return value, whether the person would interrupt program execution, web applications will report page 404; @Around (value = "Sayings ()" ) publicSayAround Object (ProceedingJoinPoint PJP) { Object the proceed = null ; System.out.println ( "Surround Type Annotation front surround .. notification" ); Long TIME2 = new new a Date () the getTime ();. The try { the proceed = pjp.proceed ( ); } the catch (the Throwable E) { e.printStackTrace (); } System.out.println ( "the type of annotation .. surround around advice, execution time" + ( new new a Date () the getTime () -. TIME2)); return the proceed; } }
Third, to expand on @Pointcut annotations
1, any method of Public
execution(public * *(..))
2 to set the beginning of the method
execution(* set*(..))
3, methods defined in the interface com.hp.in
execution(* com.hp.in.*(..))
4, com.hp.wsl package All methods
execution(* com.hp.wsl.*.*(..))
5, com.hp.wsl package and all methods subpackages
execution(* com.hp.wsl..*.*(..))
Fourth, thanks
This article draws on:
trayvon predecessors "Spring AOP bis: Pointcut annotation expression," a text, the original address: https://my.oschina.net/u/2474629/blog/1083448
Small Cai Sen first predecessors, " the Spring aop notes in a simple example of implementation ," a text, the original address: https://www.cnblogs.com/caijh/p/7154691.html
If wrong, thank you correct me;