Using proxy objects to implement AOP, while satisfying the requirements, is more complex, and Spring provides a simple way to implement AOP ASPECTJ
Demo of the same calculator
Configure Applicationcontext.xml First
<!---<base-package= "COM.ATGUIGU.SPRING.AOP" ></context:component-scan> <!---- ><aop:aspectj-autoproxy></aop:aspectj-autoproxy >
Annotated @component ("Arithmeticcalculator") on the Calculator implementation class Arithmeticcalculatorimpl
Create Class Loggingaspect as a slice of the calculator, add annotations @aspect and @component
Add a method to the Loggingaspect Beforemethod that is, the predecessor notification
/** * in Each implementation class of the Com.atguigu.spring.aop.ArithmeticCalculator interface executes a section of code before each method begins */
@Before ( "Execution (public int com.atguigu.spring.aop.arithmeticcalculator.* (..)) " ) public void Beforemethod (Joinpoint joinpoint) {String methodName = Joinpoint.getsignature (). GetName (); Object [] args = Joinpoint.getargs (); System.out.println ( the method + MethodName + "begins with" + arrays.aslist (args) );}
Pointcut expression
Execution (Public < return type >int < interface method, * is a wildcard character (.. ) is a parameter that represents each method of each implementation class for this interface >com.atguigu.spring.aop.arithmeticcalculator.* (..))
You can also specify a method here, such as Add Pointcut expressions can also use | | &&! These connectors
Execution (public * *.add (..)) | | Execution (Public * *.div (..))
Connection point parameter type
joinpoint can access more details such as method names and parameters
complete these to run the Main method
New Classpathxmlapplicationcontext ("Applicationcontext.xml"); = (arithmeticcalculator) ctx.getbean ("Arithmeticcalculator"); System.out.println (Arithmeticcalculator.getclass (). GetName ()); int result = Arithmeticcalculator.add (1, 2); System.out.println ("Result:" + result); = Arithmeticcalculator.div (+); System.out.println ("Result:" + result);
Similarly, you can add additional notifications
AspectJ supports 5 types of notification annotations:
@Before: Pre-notification, executed before method execution
@After: Post notification, executed after method execution
@AfterRunning: Returns a notification that executes after the method returns the result
@AfterThrowing: Exception notification, after the method throws an exception
@Around: Surround notifications, execute around methods
Log Slice Detail Code
/*** You can use @Order annotations to specify the priority of the slice, the smaller the value the higher the priority*/@Order (2) @Aspect @component Public classLoggingaspect {/*** Defines a method that declares a pointcut expression. In general, no additional code needs to be added to the method. * Use @Pointcut to declare pointcut expressions. * The following additional notification uses the method name directly to refer to the current pointcut expression. */@Pointcut ("Execution (public int com.atguigu.spring.aop.arithmeticcalculator.* (..))") Public voiddeclarejointpointexpression () {}/*** Execute a piece of code before each method of each implementation class of the Com.atguigu.spring.aop.ArithmeticCalculator interface begins*/@Before ("Execution (public int com.atguigu.spring.aop.arithmeticcalculator.* (..))") Public voidBeforemethod (Joinpoint joinpoint) {String methodName=joinpoint.getsignature (). GetName (); Object [] args=Joinpoint.getargs (); System.out.println ("The method" + MethodName + "begins with" +arrays.aslist (args)); } /*** Code that executes after the method executes. Regardless of whether the method has an exception*/@After ("Declarejointpointexpression ()") Public voidAftermethod (Joinpoint joinpoint) {String methodName=joinpoint.getsignature (). GetName (); System.out.println ("The method" + MethodName + "ends"); } /*** Return notification is accessible to the return value of the method when the method method normally ends the code being executed *! */@AfterReturning (Value= "Declarejointpointexpression ()", returning= "Result") Public voidafterreturning (joinpoint joinpoint, Object result) {String methodName=joinpoint.getsignature (). GetName (); System.out.println ("The method" + MethodName + "ends with" +result); } /*** Code that executes when an exception is encountered in the target method. * Can access the exception object; And you can specify that the notification code is executed when a specific exception occurs*/@AfterThrowing (Value= "Declarejointpointexpression ()", throwing= "E") Public voidafterthrowing (Joinpoint joinpoint, Exception e) {String methodName=joinpoint.getsignature (). GetName (); System.out.println ("The method" + MethodName + "occurs excetion:" +e); }}
Surround Notification Sample Code
Spring4.0 Support @order Annotations You can use @order to sort the execution order of a slice when there are multiple slices
For example, add a validation slice to add annotations @order (1), log slices add annotations @order (2)
Spring Framework Learning Notes (8)--ASPECTJ implementing AOP