------------I do not have him, but the hand is ripe, humble and foolish, and eager to be hungry-------------
Aspectj
AspectJ is a tangent-oriented framework that extends the Java language, defines the AOP syntax, and provides the ability to weave code at compile time
@AspectJ is a new feature of AspectJ 5 , using the JDK 5.0 Annotation Technology and formal AspectJ Tangent-point expression language description facets
Spring enables annotations to define enhanced classes with integrated AspectJ, greatly reducing the amount of work in the configuration file
with @AspectJ, first make sure that the JDK you are using is 5.0 or later
Annotated version of the case:
An interface, a class, a generic class that implements the annotated
Isomeservice Interface:
Package CN.DAWN.DAY19ASPECTJ; /* */Publicinterface isomeservice {publicvoid Insert (); Public void Delete (); Public void Select (); Public void update ();}
Someserviceimpl class, the implementation class of the above
Package CN.DAWN.DAY19ASPECTJ;/** * Created by Dawn on 2018/3/8.*/ Public classSomeserviceimpl implements Isomeservice { Public voidInsert () {System. out. println ("Insert OK"); } Public voidDelete () {System. out. println ("Delete OK"); } Public void Select() {System. out. println ("Select OK"); } Public voidUpdate () {System. out. println ("Update OK"); intA=5/0; System. out. println (a); }}
He has a method that simulates an arithmetic exception of zero, which makes it easy to test for exception enhancement:
I just said the class that implements the AspectJ annotation
Package cn.dawn.day19aspectj;import org.aspectj.lang.proceedingjoinpoint;import org.aspectj.lang.annotation.*;/** * Created by Dawn on 2018/3/12.*/@Aspect Public classMYASPECTJ {/*the note that is carried out alone, the following can be called with his method name to his pointcut expression*/@Pointcut (Value="Execution (* *). Day19aspectj.*.select (..))") Public void Select() {} @Pointcut (value="Execution (* *). Day19aspectj.*.update (..))") Public voidupdate () {}/*Post Enhancement*/@AfterReturning ("Execution (* *). Day19aspectj.*.select (..))") Public voidmyafterreturning () {System. out. println ("Post Enhancement"); } /*front-mounted enhancements*/@Before ("Execution (* *). Day19aspectj.*.insert (..))") Public voidMybefore () {System. out. println ("front-mounted enhancements"); } /*Exception Enhancement*/@AfterThrowing ("Execution (* *). Day19aspectj.*.update (..))") Public voidmyafterthrowing () {System. out. println ("Exception Enhancement"); } /*Final Enhancements*//*@After ("Execution (* *). Day19aspectj.*.update (..)) | | Execution (* *). Day19aspectj.*.select (..)) ") public void Myafter () {System.out.println ("I am the final enhancement"); }*/ /*Final Enhancements*/@After ("update () | | Select ()") Public voidMyafter () {System. out. println ("I was the ultimate enhancement"); } /*Surround Enhancement*/@Around ("Execution (* *). Day19aspectj.*.insert (..))") Public voidMyaround (Proceedingjoinpoint pjp) throws Throwable {System. out. println ("before surround enhancement"); Pjp.proceed (); System. out. println ("after surround enhancement"); }}
There's only one thing to say here. When you want to pass in multiple conditions, match any one to enhance the method, just use | | Logic or to split, he above I wrote a way, can put the pointcut expression, can be used below, a bit like declaration in advance, rather than use the time to declare the feeling
Single test method:
Package Cn.dawn.day19aspectj;import Org.junit.test;import org.springframework.context.ApplicationContext; Import Org.springframework.context.support.ClassPathXmlApplicationContext;/** * Created by Dawn on 2018/3/3.*/ Public classtest20180312 {@Test/*AOP Agent Factory bean Exception Enhancement*/ Public voidt01 () {ApplicationContext context=NewClasspathxmlapplicationcontext ("Applicationcontext-day19aspectj.xml"); Isomeservice Service= (Isomeservice) Context.getbean ("Service"); Try{service.update (); }Catch(Exception e) {e.printstacktrace (); } service.delete (); Service.Select(); Service.insert (); }}
I know this piece is more cumbersome, not too good language interpretation, more practice, after all, the way the blog is not as Live and video, live, etc., more practice, practice the time to understand
Ssm-spring-17:spring in AspectJ annotated version