Method Implementation Interface
Package Com.cn.spring.aop.impl;//interface classes for subtraction Public InterfaceArithmeticcalculator {intAddintIintj); intSubintIintj); intMulintIintj); intDivintIintj);}
Interface Implementation Class
Package Com.cn.spring.aop.impl;import org.springframework.stereotype.Component;//Implementation Class@Component Public classArithmeticcalculatorimpl implements Arithmeticcalculator {@Override Public intAddintIintj) {intresult = i +J; returnresult; } @Override Public intSubintIintj) {intresult = i-J; returnresult; } @Override Public intMulintIintj) {intresult = i *J; returnresult; } @Override Public intDivintIintj) {intresult = I/J; returnresult; }}
XML file Configuration
<?xml version="1.0"encoding="UTF-8"? ><beans xmlns="Http://www.springframework.org/schema/beans"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"Xmlns:context="Http://www.springframework.org/schema/context"XMLNS:AOP="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scanBase-package="Com.cn.spring.aop.impl"> </context:component-scan> <!--make ASPJECTJ annotations work: Automatically generate proxy objects for matching classes--<aop:aspectj-autoproxy></ Aop:aspectj-autoproxy></beans>
Notification slices
package Com.cn.spring.aop.impl;import Org.aspectj.lang.joinpoint;import Org.aspectj.lang.proceedingjoinpoint;import Org.aspectj.lang.annotation.after;import Org.aspectj.lang.annotation.afterreturning;import Org.aspectj.lang.annotation.afterthrowing;import Org.aspectj.lang.annotation.aspect;import Org.aspectj.lang.annotation.before;import Org.springframework.stereotype.component;import Java.util.arrays;import java.util.List; // Declare this class as a tangent: first you need to put the class into an IOC container, declared as a tangent @Aspect @component public class Loggingaspect {
@Pointcut ("Execution (* * arithmeticcalculator.* (..))")
public void Declarejointpointexpression () {
}
Declares that the method is a pre-notification: Executes before the target method starts
@Before ("Execution (public int arithmeticcalculator.* (int, int))")
Front-facing notifications
@Before ("Declarejointpointexpression ()")
public void Beforemethod (Joinpoint joinpoint) {
String methodName = Joinpoint.getsignature (). GetName ();
list<object> args = Arrays.aslist (Joinpoint.getargs ());
Object target = Joinpoint.gettarget ();
System.out.println ("the method" + MethodName + "begins with" + args + target);
}
//Post Notification@After ("Execution (* * arithmeticcalculator.* (int, int))") Public voidAftermethod (Joinpoint joinpoint) {String methodName=joinpoint.getsignature (). GetName (); System. out. println ("The method"+ MethodName +"ends"); } //returns a method that can access the return value of a method when it is executed after the method has finished gracefully@AfterReturning (value="Execution (* * arithmeticcalculator.* (int, int))", returning="result") Public voidafterreturning (joinpoint joinpoint, Object result) {String methodName=joinpoint.getsignature (). GetName (); System. out. println ("The method"+ MethodName +"ends with"+result); } //Exception Notification@AfterThrowing (value="Execution (* * arithmeticcalculator.* (int, int))", throwing="ex") Public voidafterthrowing (Exception ex) {System. out. println (ex); } }/** Joinpoint access to the connection point context information * joinpoint? Java.lang.object[] Getargs (): Gets the parameter list of the connection point method runtime; Signature getsignature (): Gets the method signature object of the connection point;? Java.lang.Object gettarget (): Gets the target object where the connection point resides; Java.lang.Object getthis (): Gets the proxy object itself; **/
Surround notification is similar to the whole process of dynamic agents
If the surround notification returns 100, the result is 100
//the most powerful surround notification//a parameter of type Proceedingjoinpoint can decide whether to execute the target method@Around ("Execution (* * arithmeticcalculator.* (..))") PublicObject Aroundmethod (Proceedingjoinpoint pjd) {object result=NULL ; String MethodName=pjd.getsignature (). GetName (); Try{System. out. println ("before"); Result=pjd.proceed (); System. out. println (" After"+result); } Catch(Throwable e) {//TODO auto-generated Catch blockE.printstacktrace (); System. out. println ("Exception"+e); } System. out. println ("return"+result); returnresult; }
Spring 5 Notifications