Guide package and so on do not repeat;
Establish an interface: Arithmeticcalculator, no instantiation method;
PackageCom.atguigu.spring.aop.impl.panpan; Public InterfaceArithmeticcalculator {//create an interface that is an abstract class and cannot be instantiated intAddintIintj); intSubintIintj); intMulintIintj); intDivintIintj);}
Create a class: Arithmeticcalculatorimpl inherits from Interface: Arithmeticcalculator, implements a method that does not instantiate in the interface
PackageCom.atguigu.spring.aop.impl.panpan;Importorg.springframework.stereotype.Component; Public classArithmeticcalculatorimplImplementsArithmeticcalculator {@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; }}
Build class: Arithmeticcalculatorloggingproxy, instantiate some method of aspect-oriented programming
PackageCom.atguigu.spring.aop.impl.panpan;Importjava.util.Arrays;ImportOrg.aspectj.lang.JoinPoint;ImportOrg.aspectj.lang.ProceedingJoinPoint; Public classArithmeticcalculatorloggingproxy {//method of the preceding notification, in the XML file can get the method name of the method by this property, Joinpoint is the tangent point, Public voidBeforemethod (Joinpoint joinpoint) {//get the method name, and parameter value, the parameter value is multiple, so the method of array collectionString methodname=joinpoint.getsignature (). GetName (); Object[] args=Joinpoint.getargs (); System.out.println ("The method" +methodname+ "Begains" +arrays.aslist (args)); } //method of post-notification Public voidAftermethod (Joinpoint joinpoint) {//Gets the method name, and the parameter valueString methodname=joinpoint.getsignature (). GetName (); System.out.println ("The method" +methodname+ "ends"); } //ways to return notifications Public voidafterreturning (joinpoint joinpoint,object result) {//Gets the method name, and the parameter valueString methodname=joinpoint.getsignature (). GetName (); System.out.println ("The method" +methodname+ "* * * * Ends:" +result); } //method of Exception notification Public voidafterthrowing (Joinpoint joinpoint, Exception e) {String methodName=joinpoint.getsignature (). GetName (); System.out.println ("The method" +methodname+ "Exception:" +e); } //surround notifications. PublicObject Aroundmethod (Proceedingjoinpoint pjp) {object result=NULL; //Get method NameString methodname=pjp.getsignature (). GetName (); Try { //pre-notification, Arrays.aslist (Pjp.getargs ()) is the number of parameters for the method, the array setSystem.out.println ("The Method" +methodname+ "Begains" +arrays.aslist (Pjp.getargs ())); //Execute target Methodresult=pjp.proceed (); //Back to NotificationsSystem.out.println ("The Method" +methodname+ "ends with:" +result); } Catch(Throwable e) {//Exception NotificationSystem.out.println ("The method" +methodname+ "occurs exception" +e); E.printstacktrace (); } //Post NotificationSystem.out.println ("the method" + MethodName + "ends"); returnresult; }}
Create a spring XML file for bean and AOP configuration
<!--config Bean, full class name is the full class name of the inheriting interface class--><bean id= "Arithmeticcalculator"class= "Com.atguigu.spring.aop.impl.panpan.ArithmeticCalculatorImpl" ></bean> <!--configuration slices of beans, full class names are classes that implement slice programming Full class name--<bean id= "Arithmeticcalculatorloggingproxy"class= "Com.atguigu.spring.aop.impl.panpan.ArithmeticCalculatorLoggingProxy" ></bean> <!--configuration AOP--< ;aop:config> <!--Configure pointcut expressions, full class name of the interface----<aop:pointcut expression= "Execution (* Com.atguigu.sprin G.aop.impl.panpan.arithmeticcalculator.* (Int,int)) "ID= "Pointcut"/> <!--configuration facets and Notification methods in method name,--> <aop:aspect ref= "arithmeticcalculatorlogging Proxy "order=" 1 "> <aop:before method=" Beforemethod "pointcut-ref=" Pointcut "/> <aop:after Method= "Aftermethod" pointcut-ref= "Pointcut"/> <aop:after-throwing method= "afterthrowing" pointcut-ref= " Pointcut "throwing=" E "/> <aop:after-returning method=" afterreturning "pointcut-ref=" Pointcut "returning=" Result "/> <aop:around method=" Aftermethod "pointcut-ref=" Pointcut "/> </aop:aspect> &l T;/aop:config>
Build the class main and test
ApplicationContext ctx=New classpathxmlapplicationcontext ("ApplicationContext.test.xml"); // Arithmeticcalculator, is an interface class, annotated by the class that inherits the interface Arithmeticcalculator impl= (arithmeticcalculator) Ctx.getbean ("Arithmeticcalculator"); System.out.println (Impl.getclass (). GetName ()); int result=impl.add (12,2); SYSTEM.OUT.PRINTLN (result); Double Result2=impl.div (2); System.out.println (RESULT2);
Spring AOP: Tangent-oriented programming, AspectJ, is a spring-based XML file approach