Previously, we created AOP with Spring framework via implementing interface of spring AOP. And now we can use XML Schema to achieve it. We need to add below AspectJ libraries to our project. So downloading and adding them to the CLASSPATH of application, and also we can use the maven to build our application.
Create logic class bean and pointcut with `<aop:pointcut>` , define the expression matcher to match the methods which will be executed with AOP. To see the expression of pointcut, go to [http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html).
Use <aop:before>, <aop:after>, <aop:after-returning> and <aop:after-throwing> to create each advices. Reference to pointcut bean and appoint which method of service can be injected. Note, for after-returning and after-throwing, we should add attributes returning and throwing to access their variables.
publicvoidbeforeSaveData() { System.out.println("The Data is going to be saved"); }
publicvoidafterSaveData() { System.out.println("The Data has been saved"); } publicvoidbeforeGetData() { System.out.println("Ready to get the data"); } publicvoidafterGetData() { System.out.println("The data has been got"); }
publicvoidafterReturningAdvice(Object returnValue) { System.out.println(String.format( "The return value is: %s ", returnValue.toString())); }
publicvoidthrowingAdvice(NullPointerException ex) { System.out.println(String.format( "Throw exception is: %s", ex.getMessage())); }
}
In our Logging service, the afterReturningAdvice and throwingAdvice methods should take returnValue which is Object type and Exception type ex paramters.
myData.saveData("This is my data"); System.out.println("-----------------"); myData.getData(); }
}
See the output of application:
he Data is going to be saved
Saving the data: This is my data
The Data has been saved
-----------------
Ready to get the data
Get the data: This is my data
The return value is: This is my data
The data has been got
-----------------
Throw exception is: null
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.concat(String.java:1970)
at spring.test.aop.MyData.concatData(MyData.java:17)
at spring.test.aop.MyData$$FastClassBySpringCGLIB$$731fbdfd.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:58)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at spring.test.aop.MyData$$EnhancerBySpringCGLIB$$5a76fbda.concatData(<generated>)
at spring.test.aop.App.main(App.java:21)