Spring AOP Module
AOP (Aspect Oriented Programming) is an important feature of Spring framework.
Interface Programming is recommended for Spring
Spring provides three types of interceptors: method-front interceptor, return interceptor, and exception throw interceptor.
Interceptor Definition
// Service interface public interface IAopService {public void withAop () throws Exception; // public void withoutAop () throws Exception will be blocked; // not blocked} public class AopServiceImpl implements IAopService {private String name; public void withAop () throws Exception {System. out. println (function running with AOP. Name: + name); if (name. trim (). length () = 0) {throw new AccountException (name attribute cannot be blank);} public void withoutAop () throws Exception {System. out. println (function running Without AOP .);} Public void setName (String name) {this. name = name;} public String getName () {return this. name ;}}
// Interceptor before the method, check whether the name is empty import org. springframework. aop. methodBeforeAdvice; public class MethodBeforeInterceptor implements MethodBeforeAdvice {// execute this Method before calling the object method // The parameters are called methods, called Method parameters, object public void before (method Method method, object [] args, Object instance) throws Throwable {System. out. println (method to be executed: + method. getName (); // if (instance instanceof AopServiceImpl) {String name = (AopServiceImpl) instance ). getName (); if (name = null) {throw new NullPointerException (name cannot be blank );}}}}
// The interceptor import org. springframework. aop. afterReturningAdvice; public class MethodAfterInteceptor implements AfterReturningAdvice {// The parameters are the Method return value, called method, Method parameter, and blocked Object public void afterReturning (Object value, method Method method, object [] args, Object instance) throws Throwable {System. out. println (method: + method. getName () + execution completed, return value: + value );}}
// Exception interceptor capture exception import org. springframework. aop. throwAdvice; public class ThrowsInterceptor implements ThrowAdvice {// parameters are called methods, method parameters, object instances, thrown exceptions, and the first three parameters can be omitted, the fourth parameter is required. This design allows developers to flexibly define multiple methods to capture various exceptions. public void afterThrowing (Method method, Object [] args, Object instance, accountException ex) throws Throwable {System. out. println (method: + method. getName () + throw exception: + ex);} public void afterThrowing (NullPointerException ex) throws Throwable {System. out. println (thrown exception: + ex );}}
The interceptor configuration Spring cannot directly assemble the Service implementation class and the interceptor, because there is no corresponding setter or getter method. Install the Interceptor to NameMatchMethodPointcutAdvisor by using the Spring proxy class, install the automatic Service to ProxyFactoryBean, And Then assemble it into a piece
AopMethodBeforeInterceptor
AopMethodAfterInterceptor
AopThrowsInterceptor
AOP concepts Aspect
In this example, the methods withAop () and withoutAop () all have some code, which can be seen as the aspect in AOP and can be understood as a module.
Notification Advisor
In this example, the three interceptors are implemented from a specific interface. From the class name, we can see that the three interceptors are notifications in AOP. Once Spring meets the conditions, it will send a notification, that is, some code to be executed, can implement a certain function
Pointcut
When configuring the interceptor, only the withAop () method is configured in XML to use the interceptor, but the withoutAop () method is not configured with the Interceptor. This configuration relies on org. springframework. aop. support. nameMatchMethodPointcutAdvisor. This is an entry point. Wildcards can be used for configuration. This class also carries the Advisor because it is also implemented using notifications.
Simply put, the entry point is responsible for inserting code into somewhere, and the notification is responsible for inserting code.
Spring provides flexible entry point configuration, such as using regular expressions.
.*get*
.*set*