AOP (Aspect oriented programming, tangent-oriented programming) is an important feature of the spring framework
Spring recommends using interface programming
Spring provides three types of interceptors: Method Front Interceptor, back interceptor, exception throw interceptor
Interceptor definition
Service interface public interface iaopservice{public void Withaop () throws exception;//will be blocked public void Withoutaop () throws exception;//will not be intercepted}public class Aopserviceimpl implements Iaopservice{private String name;public void Withaop () throws EXCEPTION{SYSTEM.OUT.PRINTLN ("The function with AOP is running. Name: "+name"), if (Name.trim (). Length () = = 0) {throw new Accountexception ("name attribute cannot be null");}} public void Withoutaop () throws Exception{system.out.println ("functions without AOP run. ");} public void SetName (String name) {this.name = name;} Public String GetName () {return this.name;}}
Method before the Interceptor, check if name is empty import Org.springframework.aop.methodbeforeadvice;public class Methodbeforeinterceptor Implements methodbeforeadvice{//executes this method before calling the method of the object//parameters are called methods, parameters of the called method, Object public void before (method Method,object [] Args,object instance) throws Throwable{system.out.println ("upcoming Methods:" +method.getname ());//if (Instance instanceof Aopserviceimpl) {String name = ((Aopserviceimpl) instance). GetName (); if (name = = null) {throw new NullPointerException (" Name cannot be empty ");}}}}
Return after interceptor import Org.springframework.aop.afterreturningadvice;public class Methodafterinteceptor implements The afterreturningadvice{//parameters are method return values, called methods, method parameters, blocked objects public void afterreturning (object Value,method Method,object [] Args,object instance) throws Throwable{system.out.println ("Method:" +method.getname () + "execution completed with return value:" +value);}}
Exception Interceptor catch Exception import Org.springframework.aop.throwadvice;public class Throwsinterceptor implements throwadvice{// Parameters are called method, method parameter, object instance, thrown exception//first three parameters can be omitted, fourth parameter is necessary, so the design is to enable developers to flexibly define multiple methods to capture a variety of different exceptions public void afterthrowing (method Method,object [] Args,object instance,accountexception ex) throws Throwable {System.out.println ("Method:" +method.getname ( ) + "Throws an exception:" +ex);} public void afterthrowing (NullPointerException ex) throws Throwable {System.out.println ("throws an exception:" +ex);}}
Interceptor configuration Spring cannot assemble the implementation class of the service directly with the interceptor because there is no corresponding setter, getter method. With the Spring proxy class installed, the interceptor is installed into the namematchmethodpointcutadvisor, the automatic service is installed into the Proxyfactorybean, and then assembled into a
<!--method Front interceptor Methodbeforeinterceptor installed into Namematchmethodpointcutadvisor--><bean id= " Aopmethodbeforeinterceptor "class=" Org.springframework.aop.support. Namematchmethodpointcutadvisor "><!--Interceptor Implementation Class--><property Name=" advice "><bean class=" com.clf.spring. Methodbeforeinterceptor "></property><!--the method name to intercept--><property name=" Mappedname " Value= "WITHAOP" > </property></bean><!--a similar way to configure the back of the pre-interceptor methodafterinteceptor--><bean id= " Aopmethodafterinterceptor "class=" Org.springframework.aop.support. Namematchmethodpointcutadvisor "><property name=" advice "><bean class=" com.clf.spring. Methodafterinteceptor "></property><property name=" Mappedname "value=" WithAop "> </property> </bean><!--A similar way to configure an exception before the Interceptor Methodafterinteceptor--><bean id= "Aopthrowsinterceptor" class= " Org.springframework.aop.support. Namematchmethodpointcutadvisor "><property name=" advice "><bean class= "Com.clf.spring. ThRowsinterceptor "></property><property name=" Mappedname "value=" WITHOUTAOP "> </property></ bean><!--Service Implementation class, install to Proxyfactorybean--><bean id= "Aopservice" class= " Org.springframework.aop.ProxyFactoryBean "><!--installing interceptors--><property name=" Interceptornames "><list ><value>aopMethodBeforeInterceptor</value><value>aopMethodAfterInterceptor</value> <value>aopthrowsinterceptor</value></list></property><!-intercepted Object--><property Name= "target" ><bean class= "Com.clf.spring.AopServiceImpl" ><property name= "name" value= "Hellospring" ></property></bean></property></bean>
AOP-related concepts facets aspect
In this case, there is some code in the method Withaop (), WITHOUTAOP (), which can be seen as a facet in AOP, which can be understood as a module
Notify Advisor
The three interceptors in this example are implemented from an interface, and it is known from the class name that three interceptors are notifications in AOP. Once spring meets the criteria, it sends out a notification that some code needs to be executed to implement a function
Pointcut Pointcut
When you configure the Interceptor, only the WITHAOP () method is configured in XML to use the interceptor, and the WITHOUTAOP () method does not have an interceptor configured with the help of Org.springframework.aop.support. Namematchmethodpointcutadvisor completed. This is a pointcut, which can be used when configuring a wildcard character. The class is also provided with a advisor because it is also implemented with notifications
Simply put, the pointcut is responsible for inserting the code somewhere, and the notification is responsible for inserting what code
Spring provides flexible pointcut configurations, such as the use of regular expressions
<bean id= "Advisortest" class= "Org.springframework.aop.support.RegexMethodPointcutAdvisor" ><property name = "Interceptor" ><ref local= "Someinterceptor" ></property><property name= "pattern" ><list ><value>.*get*</value><value>.*set*</value></list></property></bean >