Spring_aop_xml using aspect to implement dynamic proxies (common)
XML uses aspect to implement dynamic proxies This method is more commonly used, and the most important advantage of using annotations is that we do not need to define a pointcut above each method to add pointcut to the description, in the XML only needs to be defined once can be used more.
In the above spring_aop_annotation use aspect to implement dynamic agent based on the modification, remove the Logaspect method on the annotations.
XML file 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:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi:schemalocation= "Http://www.springframework.org/schema /AOP Http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://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-3.2.xsd "><!--Open the support of spring annotation-->< Context:annotation-config/><!--set Spring to which packages to find annotation--><context:component-scan base-package= " Com.spring "/><!--open Annotation-based AOP automatic proxy--><aop:aspectj-autoproxy/><aop:config><!--defining facets --><aop:aspect id= "Mylogaspect" ref= "Logaspect" ><!--where to add the corresponding aspect--><aop:pointcut id= "LogpoIntcut "expression=" Execution (* com.spring.dao.*.add* (..)) | | Execution (* com.spring.dao.*.update* (..)) | | Execution (* com.spring.dao.*.delete* (..)) "/><!--before starting to join the log--><aop:before method=" Logstart "Pointcut-ref = "Logpointcut"/><!--after the end of the join log--><aop:after method= "Logend" pointcut-ref= "Logpointcut"/><!-- Add log--><aop:around method= "Logaround" pointcut-ref= "Logpointcut"/></AOP:ASPECT></AOP during execution: Config></beans>
Logaspect class:
Package Com.spring.proxy;import Org.aspectj.lang.joinpoint;import Org.aspectj.lang.proceedingjoinpoint;import Org.aspectj.lang.annotation.after;import Org.aspectj.lang.annotation.around;import Org.aspectj.lang.annotation.aspect;import Org.aspectj.lang.annotation.before;import org.springframework.stereotype.component;//let this slice class be managed by Spring @component ("Logaspect")//Declare this is a slice class @aspectpublic Class Logaspect {public void Logstart (Joinpoint JP) {//Get executed Object System.out.println (Jp.gettarget ());// The method to be executed System.out.println (Jp.getsignature (). GetName ()); Logger.info ("Add log before method execution, from Logaspect");} public void Logend (Joinpoint JP) {//Get executed Object System.out.println (Jp.gettarget ());//method to be executed System.out.println ( Jp.getsignature (). GetName ()); Logger.info ("Add log after method execution, from Logaspect");} public void Logaround (Proceedingjoinpoint pjp) throws Throwable {Logger.info ("start adding logs to around from Logaspect");// Execution procedure pjp.proceed (); Logger.info ("End Around");}}
Test code and test results:
Spring_aop_xml using aspect to implement dynamic proxies (common)