What is ASPECTJ?
ASPECTJ is a tangent-oriented framework that extends the Java language. ASPECTJ defines the AOP syntax, so it has a dedicated compiler to generate class files that conform to the Java Byte encoding specification.
Aspect Annotated version
ASPECTJ Auto Agent
1. Configure the following in the XML file
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:context= "Http://www.springframework.org/schema /context " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " xsi:schemalocation="/http/ Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http ://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd/ http Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--target type--><bean id= "service" class= "Cn.happy.Aspect01.UserServiceImpl" ></bean> <! --enhanced-- <bean id= "Myaspect" class= "Cn.happy.Aspect01.UserTest" ></bean> <!-- ASPECTJ Auto Agent- <aop:aspectj-autoproxy/></beans>
2. Creating interfaces, classes
Package cn.happy.aspect01;/** * Created by Administrator on 2018/3/10. */public interface Iuserservice { void Select (); void update ();}
Package cn.happy.aspect01;/** * Created by Administrator on 2018/3/10. */public class Userserviceimpl implements Iuserservice {public void Select () { System.out.println ("select ok!"); } public void Update () { int i=5/0; System.out.println ("Update ok!");} }
Usertest class
Package Cn.happy.aspect01;import Org.aspectj.lang.proceedingjoinpoint;import org.aspectj.lang.annotation.*;/** * Created by Administrator on 2018/3/10. */@Aspectpublic class Usertest {//Pre-enhanced/* @Before ("Execution (* *). Aspect01.*.select (..)) ") public void Myaspectbefore () {System.out.println ("before==="); }*///Post Enhancement/* @AfterReturning ("Execution (* *). Aspect01.*.select (..)) ") public void Myaspectafter () {System.out.println ("after==="); }*//*//Surround Enhanced @Around ("Execution (* *). Aspect01.*.select (..)) ") public void Myaround (Proceedingjoinpoint pjp) throws Throwable {System.out.println ("surround enhancement a"); Pjp.proceed (); SYSTEM.OUT.PRINTLN ("Surround enhancement B"); }*//*//Exception Enhancement @AfterThrowing ("Execution (* *). Aspect01.*.update (..)) ") public void myafterthrowing () {System.out.println ("network exception"); }*///Final enhancement @After ("Execution (* *). Aspect01.*.update (..)) ") public void Myafter () {System.out.println ("final Enhancement"); }//pointcut Annotations @Pointcut (value= "Execution (* *). Aspect01.*.select (..)) ") public void Selects () {} @Pointcut (value= "Execution (* *): Aspect01.*.update (..)) ") public void Update () {} @AfterReturning ("selects () | | | Update () ") public void Mypointcutafter () {System.out.println (" after=== "); }}
AspectJ XML version
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:context= "Http://www.springframework.org/schema/context" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema /beans http://www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spring-aop.xsd Http://www.springframework.org/schema/context/HTTP Www.springframework.org/schema/context/spring-context.xsd "> <!--target type--><bean id=" service "class=" Cn.happy.Aspect02.IUserServiceImpl "></bean> <!--enhanced-<bean id=" Myaspect "class=" Cn.happy.Aspect02.UserTest ></bean> <aop:config> <!--tangent to <aop:pointcut id= "MyPoint Cut "expression=" Execution (* *). Aspect02.*.select (..)) " /> <! --Facets aspect--> <aop:aspect ref= "Myaspect" > <!--Front Enhancements--<aop:before method= "Sele CT "pointcut-ref=" mypointcut "/> <!--rear-enhanced-<aop:after-returning method=" after "pointcut- ref= "Mypointcut"/> <!--surround Enhancement-<aop:around method= "Myaround" pointcut-ref= "Mypointcut"/&G T <!--exception Enhancement--<aop:after-throwing method= "mythrowing" pointcut-ref= "Mypointcut"/> <!--< ;! – Final enhancement –> <aop:after method= "Myafter" pointcut-ref= "Mypointcut"/>--> </aop:aspect> </ Aop:config></beans>
ASPECTJ Annotated and XML versions