Method 1:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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/beans http://www.springframework.org/schema/beans/spring-beans-2.5. XSD Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring -context-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/s Pring-aop-2.5.xsd "Default-autowire= "ByName" > <Context:annotation-config/> <Context:component-scanBase-package= "Com.daoan"/> <BeanID= "Logintercetor"class= "Com.daoan.aop.LogIntercetor"></Bean> <Aop:config> <!--Pointcut, which methods are added to the plane logic - <Aop:pointcutexpression= "Execution (public * com.daoan.service). *.add (..)) "ID= "Servicepointcut"/> <!--The tangent object referenced by the slice object that joins the declaration is Loginterceptor - <Aop:aspectID= "Logaspect"ref= "Loginterceptor" > <!--before the Add method executes, the before () method under Logintercetor is executed first - <Aop:beforeMethod= "Before"Pointcut-ref= "Servicepointcut" /> </Aop:aspect> </Aop:config></Beans>
package com.daoan.aop;import org.aspectj.lang.proceedingjoinpoint;import Org.aspectj.lang.annotation.afterreturning;import Org.aspectj.lang.annotation.afterthrowing;import Org.aspectj.lang.annotation.around;import Org.aspectj.lang.annotation.aspect;import Org.aspectj.lang.annotation.before;import Org.aspectj.lang.annotation.pointcut;import org.springframework.stereotype.component;//@Aspect//@Componentpublic class Logintercetor {//(execute the Before () method before the method executes) , if the logic needs to be woven into a method of a class, that object must be spring-managed)//@Before ("Execution (public * com.daoan.service). *.add (..)) ") public void before () {System.out.println ("method before"); }//@Around ("Execution (public * com.daoan.service) after the normal operation of the method is complete. *.add (..)) ") public void Aroundmethod (Proceedingjoinpoint pjp) throws Throwable {System.out.println ("method around Start"); Pjp.proceed (); System.out.println ("Method around End"); }}
Method 2:
<Context:annotation-config/> <Context:component-scanBase-package= "Com.daoan"/> <BeanID= "Logintercetor"class= "Com.daoan.aop.LogIntercetor"></Bean> <Aop:config> <!--The tangent object referenced by the slice object that joins the declaration is Loginterceptor - <Aop:aspectID= "Logaspect"ref= "Loginterceptor" > <!--before the Add method executes, the before () method under Logintercetor is executed first - <Aop:beforeMethod= "Before"pointcut= "Execution (public * com.daoan.service). *.add (..)) " /> </Aop:aspect> </Aop:config>
Spring (AOP with XML method)