AOP (Aspect Orient programming), which we generally call aspect-oriented programming, complements the object-oriented approach to the crosscutting concerns that are distributed across modules in the system, such as transaction management, logging, caching, and so on. The key to the AOP implementation is the AOP proxy, which is automatically created by the AOP framework, which is mainly divided into static agent and dynamic agent, static proxy is ASPECTJ, and dynamic agent is represented by spring AOP.
Example of an AOP configuration:
Data Access Layer:
Package Com.hanqi.dao;public class Appuserdao {public int deleteuser (int IDs) {System.out.println (IDs + "deleted"); Yichang () ;//Throws an exception to return 1;} public void Yichang () {throw new RuntimeException ("Error occurred!) ");}}
Slice agent layer:
Package Com.hanqi.util;public class Logginproxy {public void Beforemethod () {System.out.println ("method) was called before! ");} public void Aftermethod () {System.out.println ("method is called after!) ");} public void Returnmethod () {System.out.println ("called when the result is returned!") ");} public void Throwmethod () {System.out.println ("called when an exception is thrown!") ");} /*public void Aroundmethod () {System.out.println ("method called");} */}
Spring.xml Configuring AOP
? 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 "xmlns:context=" Http://www.springframework.org/schema/context "xsi:schemalocation=" Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http ://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/aop http ://www.springframework.org/schema/aop/spring-aop-4.3.xsd "><bean id=" Logginproxy "class=" Com.hanqi.util.LogginProxy "></bean><bean id=" Appuserdao "class=" Com.hanqi.dao.AppuserDao "></ bean><aop:config><!--Configuring Pointcuts--><aop:pointcutexpression= "Execution (* com.hanqi.dao.*.* (..))" Id= " Aoppointcut "/><!--execution: execution, which means that the slice class is executed when all classes under Com.hanqi.dao are executed--><!--indicate the tangent class--><aop:aspect ref= "Logginproxy" ><aop:before method= "Beforemethod" pointcut-ref= "aoppointcut"/><aop:after method= "AfterMethod" pointcut-ref= "Aoppointcut"/><aop:after-returning method= "Returnmethod" pointcut-ref= "Aoppointcut"/>< Aop:after-throwing method= "Throwmethod" pointcut-ref= "Aoppointcut"/></aop:aspect></aop:config> </beans>
JUnit Test tests:
Package Com.hanqi.util;import static Org.junit.jupiter.api.assertions.*;import Org.junit.jupiter.api.AfterEach; Import Org.junit.jupiter.api.beforeeach;import Org.junit.jupiter.api.test;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.hanqi.dao.appuserdao;class junittest {private Classpathxmlapplicationcontext c;private Appuserdao Appuserdao; @BeforeEachvoid setUp () throws Exception {c = new Classpathxmlapplicationcontext ("Spring.xml"); Appuserdao = C.getbean (Appuserdao.class);} @AfterEachvoid TearDown () throws Exception {C.close ();} @Testvoid Test () {Appuserdao.deleteuser (55);}}
Printing results:
Configuring AOP with Annotations
Spring.xml configuration:
<?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" xmlns: context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/ Context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/ SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-4.3.xsd "><context:component-scan Base-package= "Com.hanqi" ></context:component-scan><!--configuration Scanner--></beans>
Data Access Layer:
Package Com.hanqi.dao;import org.springframework.stereotype.Repository; @Repository//Add to Spring container, The ID name defaults to the class name first letter lowercase public class Appuserdao {public int deleteuser (int IDs) {System.out.println (IDs + "deleted"); Yichang (); return 1 ;} public void Yichang () {throw new RuntimeException ("Error occurred!) ");} }
Appuserservice class:
Package Com.hanqi.service;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.beans.factory.annotation.qualifier;import Org.springframework.stereotype.repository;import Com.hanqi.dao.AppuserDao; @Repositorypublic class Appuserservice {@Autowired//The class is automatically placed into a member variable in the current class, when the default assembly type bytype//@ Qualifier ("DAO") This annotation can assemble a class of id= "DAO" in the bean into the member variable to distinguish between the same type and the private Appuserdao appuserdao;public int deleteuser (int IDS) {return appuserdao.deleteuser (IDS);}}
proxy class:
Package Com.hanqi.util;import Org.aspectj.lang.annotation.after;import org.aspectj.lang.annotation.AfterReturning ; Import Org.aspectj.lang.annotation.afterthrowing;import Org.aspectj.lang.annotation.aspect;import Org.aspectj.lang.annotation.before;import Org.springframework.context.annotation.enableaspectjautoproxy;import org.springframework.stereotype.Repository; @Repository//Added to the spring container, the ID name is the first letter of the class name lowercase @aspect// Declares that the current class is a slice class @enableaspectjautoproxy//auto proxy public class Logginproxy {@Before ("Execution (* com.hanqi.dao.*.* (..))") Declares a tangent point public void Beforemethod () {System.out.println ("method was called before!) ");} @After ("Execution (* com.hanqi.dao.*.* (..))") public void Aftermethod () {System.out.println ("method is called after!) ");} @AfterReturning ("Execution (* com.hanqi.dao.*.* (..))") public void Returnmethod () {System.out.println ("called when the result is returned!") ");} @AfterThrowing ("Execution (* com.hanqi.dao.*.* (..))") public void Throwmethod () {System.out.println ("called when an exception is thrown!") ");} /*public void Aroundmethod () {System.out.println ("method called");} */}
Test class:
Package Com.hanqi.util;import Org.junit.jupiter.api.aftereach;import Org.junit.jupiter.api.beforeeach;import Org.junit.jupiter.api.test;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.hanqi.dao.appuserdao;import Com.hanqi.service.appuserservice;class junittest {private Classpathxmlapplicationcontext c;private Appuserdao appuserdao;private appuserservice appuserService;@ Beforeeachvoid setUp () throws Exception {c = new Classpathxmlapplicationcontext ("Spring.xml");//appuserdao = C.getbean ( Appuserdao.class); appuserservice = C.getbean (Appuserservice.class);} @AfterEachvoid TearDown () throws Exception {C.close ();} @Testvoid Test () {//appuserdao.deleteuser; Appuserservice.deleteuser (66);}}
JAVA Framework-SPRING-AOP Face Tangent