Spring Aspect Usage Brief

Source: Internet
Author: User

"Configure aspect" to enable ASPECTJ-style annotations You must import the ASPECTJ jar package in addition to the spring configuration file, and configure it in two ways; I. Configuring in the schema of a configuration file first step: Add xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA.AOP" to the schemaSecond step: Add two lines to the xsi:schemalocation in the schema: http://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP and http://Www.springframework.org/schema/aop/spring-aop.xls Step Three: Add <aop:aspectj-autoproxy/> in the beans tag bodyTwo. Add a bean to the beans tag body The first step: add <beanclass= "Org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" >Step Two: Add <aop:aspectj-autoproxy/> in the beans tag bodyThere are two ways to use aspect: the first is to use ASPECTJ-style annotations, and the second is to use an XML configuration file 0. Prepare execution (modifier returntype ClassType methodName params Throwstype)--pointcut indicator usage explanation Six parameters all support wildcard "*", but MethodName also support "(..)"Wildcard characters used to match one or more parameters, six parameters returntype,methodname,params these three can not be omitted, the rest can be omitted usage example execution (* *(..)) --Match any return type, any method name, any parameter method, that is, all methods can be execution (Public * *controller get* (*, string))--matches any class access modifier to public, any return value type that belongs to a class that ends with a controller, a method name that begins with a get, and a method that execution a String of any type (* Org.xt.controller.*controller.*(..)) This usage is self-evident. Using ASPECTJ-style annotations, such annotations spring supports: @Aspect-this annotation is used to define a class as an example of a facet usage @aspectclassmodifierClassclassname{} @Before-this annotation is used to label the enhancement method, which is intended to cut the enhancement method into the connection point so that it executes the usage example before the connection point method executes @aspectclassmodifierClassclassname{@Before ("Execution (* org.xt.controller.*.* (..))") Methodmodifier Methodreturntype MethodName () {}} @AfterReturning-this annotation is used to label the enhancement method, which is to cut the enhancement method into the connection point, To execute after a successful return of the Pointcut method execution, it has two properties for pointcut and returning usage examples of returning used to specify the return value object of a pointcut @aspectclassmodifierClassclassname{@AfterReturning (returning= "Desreturnvalue", pointcut= "Execution (* org.xt.controller.*.* (..))") Methodmodifier Methodreturntype methodName (Object desreturnvalue) {//Print return value ObjectSystem.out.println (string.valueof (Desreturnvalue)); }} @AfterThrowing-this annotation is used to annotate the enhancement method, which is used to cut the enhancement method into the connection point to handle the unhandled exception of the Pointcut method. It has two properties for pointcut and throwing usage examples of exception object usages throwing used to specify Pointcut throws @aspectclassmodifierClassclassname{@AfterThrowing (throwing= "Desthrowvalue", pointcut= "Execution (* org.xt.controller.*.* (..))") Methodmodifier Methodreturntype methodName (Throwable desthrowvalue) {//Printing exception messagesSystem.error.println (Desthrowvalue.getmessage ()); }} @After-this annotation is used to label the enhancement method, which is to cut the enhancement method into the connection point and make it execute after the Pointcut method executes, and this callout does not require that the Pointcut method must successfully execute the completion usage example @aspectclassmodifierClassclassname{@After ("Execution (* org.xt.controller.*.* (..))") Methodmodifier Methodreturntype MethodName () {}} @Around-this annotation features the most powerful, because its labeling enhancements can directly interfere with the execution usage of the Pointcut method. The enhancement method that is annotated must have a parameter , the parameter type is Proceedingjoinpoint usage example @aspectclassmodifierClassclassname{@Around ("Execution (* org.xt.controller.*.* (..))") Methodmodifier Methodreturntype methodName (Proceedingjoinpoint point) {}} supplement: There are 5 types of enhancements to the markup, in the enhancement method to access some data in the Pointcut, For example, the parameters of the Pointcut method, the class object to which the Pointcut method belongs, the proxy class object of the Pointcut method, and some data of the Pointcut method itself need to explain the first step in the Joinpoint class usage: Define a joinpoint type parameter for the enhanced method (@ The around enhancement method does not have to, because it has a proceedingjoinpoint) The second step: you can use the Joinpoint type parameter object to call object[] Getargs () to get the parameters of the Pointcut method, Object Gettarget () to get the class object of the Pointcut method, Object Getthis () to get the proxy class object for the Pointcut method, Signature getsignature () To get the Pointcut method itself information usage Example @aspectclassmodifierClassclassname{@Before ("Execution (Protected * org.xt.controller.*.* (..))") Methodmodifier Methodreturntype methodName (joinpoint point) {object[] args=Point.getargs ();For(Object Arg:args) {System.out.println (string.valueof (ARG));} } @AfterReturning (returning= "", pointcut= "Execution (Protected * org.xt.controller.*controller.* (..))") Methodmodifier Methodreturntype methodName (joinpoint point) {Object target=Point.gettarget ();//The object that prints the Pointcut method System.out.println (the class object of the Pointcut method is "+Target.tostring ()); } @AfterThrowing (throwing= "", pointcut= "Execution (* org.xt.controller.*.* (*,string,object))") Methodmodifier Methodreturntype methodName (joinpoint point) {object[] args=Point.getargs ();For//) Methodmodifier Methodreturntype MethodName (proceedingjoinpoint point) {Object proxy= point.getthis (); SYSTEM.OUT.PRINTLN ("The proxy object for the Pointcut method is" +proxy.tostring ()); // execute pointcut function, you can change the parameters of the Pointcut function Object returnvalue= Point.proceed (new string[]{});}     

Spring Aspect Usage Brief

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.