First, AOP
AOP (Aspect Oriented Programming): aspect-oriented programming, in OOP (Object oriented programming), the key Unit module degree is the class, whereas in AOP the element module degree is the facet.
Application scenarios: Interceptors, declarative things, logs, and so on.
Second, XML-based
Example
Packagecom.my;/*** @Author Jyy * @Description {} * @Date 2018/7/16 11:29*/ Public classPerson {PrivateString ID; PrivateString name; Privateaddress address; PublicPerson () {} PublicPerson (address address) { This. Address =address; } PublicString getId () {returnID; } Public voidsetId (String id) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicAddress getaddress () {returnaddress; } Public voidsetaddress (address address) { This. Address =address; } Public voidGetaddressdetail () {System.out.println ("Query user Detailed address"); }}
Package com.my; /** */Publicclass aoptest {publicvoid BEFOREAOP () { System.out.println ("======before aop======"); } Public void Afteraop () { System.out.println ("======after aop======");} }
Packagecom.my;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;/*** @Author Jyy * @Description {} * @Date 2018/7/13 10:06*/ Public classMainapp { Public Static voidMain (string[] args) {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("Beans.xml"); Person Person= Ctx.getbean (person.class); Person.getaddressdetail (); }}
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop-3.0.xsd "><aop:config> <aop:aspect id= "AOP" ref= "Aoptest" > <aop:pointcut id= "SelectAll"expression= "Execution (* com.my.*.* (..))" /> <aop:before pointcut-ref= "SelectAll" method= "Beforeaop"/> <aop:after pointcut-ref= "se Lectall "method=" Afteraop "/> </aop:aspect> </aop:config> <bean id=" Person "class= "Com.my.Person" > <property name= "id" value= "1001"/> <property name= "name" value= "Zhang San"/> <property name= "Address" ref= "Address"/> </bean> <bean id= "Address"class= "Com.my.Address" > <property name= "Country" value= "China"/> <property name= "province" value= "Jiangsu province"/& Gt <property name= "City" value= "Nanjing"/> </bean> <bean id= "Aoptest"class= "Com.my.AopTest"/></beans>
Output Result:
======before aop====== Query user detailed address ======after aop======
The above example is a simple demonstration of the facets, and we mainly analyze the following configuration files:
<aop:aspect id= "AOP" ref= "aoptest" > Define facets AOP, point to class Aoptest
expression= "Execution (* com.my.*.* (..))" /> Defines the tangent selectall of a slice, specifying the scope of the slice
<aop:before pointcut-ref= "SelectAll" method= "Beforeaop"/> Define methods for pre-execution operations, including before, after, Afterreturning, Afterthrowing, around
<aop:after pointcut-ref= "SelectAll" method= "Afteraop"/> Define methods for post-execution operations, including before, after, Afterreturning, Afterthrowing, around
The above is only part of the configuration, other configuration self-lookup
Third, based on @aspectj
Turn on @aspectj support
<aop:aspectj-autoproxy/>
Example
Packagecom.my;/*** @Author Jyy * @Description {} * @Date 2018/7/16 11:29*/ Public classPerson {PrivateString ID; PrivateString name; Privateaddress address; PublicPerson () {} PublicPerson (address address) { This. Address =address; } PublicString getId () {returnID; } Public voidsetId (String id) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicAddress getaddress () {returnaddress; } Public voidsetaddress (address address) { This. Address =address; } Public voidGetaddressdetail () {System.out.println ("Query user Detailed address"); }}
Packagecom.my;ImportOrg.aspectj.lang.annotation.After;ImportOrg.aspectj.lang.annotation.Aspect;ImportOrg.aspectj.lang.annotation.Before;ImportOrg.aspectj.lang.annotation.Pointcut;/*** @Author Jyy * @Description {} * @Date 2018/8/6 11:28*/@Aspect Public classaoptest {@Pointcut ("Execution (* com.my.*.* (..))") Private voidaoptest () {} @Before ("Aoptest ()") Public voidBeforeaop () {System.out.println ("======before aop======"); } @After ("Aoptest ()") Public voidAfteraop () {System.out.println ("======after aop======"); }}
Packagecom.my;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;/*** @Author Jyy * @Description {} * @Date 2018/7/13 10:06*/ Public classMainapp { Public Static voidMain (string[] args) {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("Beans.xml"); Person Person= Ctx.getbean (person.class); Person.getaddressdetail (); }}
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop-3.0.xsd "><aop:aspectj-autoproxy/> <bean id= "Person"class= "Com.my.Person" > <property name= "id" value= "1001"/> <property name= "name" value= "Zhang San"/> <property name= "Address" ref= "Address"/> </bean> <bean id= "Address"class= "Com.my.Address" > <property name= "Country" value= "China"/> <property name= "province" value= "Jiangsu province"/& Gt <property name= "City" value= "Nanjing"/> </bean> <bean id= "Aoptest"class= "Com.my.AopTest"/></beans>
Output Result:
======before aop====== Query user detailed address ======after aop======
When all methods under the com.my. Package path are called, the BEFOREAOP () method is called before the method is called, where we can perform permission validation to implement the Interceptor.
Spring| Aop