This small code demonstrates the difference between the @around @Before @After three annotations in spring AOP @Before the execution of a logic before the intercepted method executes. @After is to execute a piece of logic after the intercepted method executes. @Around is a logic that can be executed both before and after the intercepted method.
Packagecom.itsoft.action;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportOrg.springframework.stereotype.Controller;/** * * @authorZXF * Demo AOP Test class*/@Controller Public classuseraction { Public voidqueryusers () {System.out.println ("Query all Users" the "All Users List" "); } Public Static voidMain (string[] args) {Classpathxmlapplicationcontext ctx=NewClasspathxmlapplicationcontext ("Application-aop.xml"); Useraction useraction= (useraction) ctx.getbean ("Useraction"); Useraction.queryusers (); Ctx.destroy (); }}
PackageCom.itsoft;ImportOrg.aspectj.lang.ProceedingJoinPoint;ImportOrg.aspectj.lang.annotation.After;ImportOrg.aspectj.lang.annotation.Around;ImportOrg.aspectj.lang.annotation.Aspect;ImportOrg.aspectj.lang.annotation.Before;ImportOrg.aspectj.lang.annotation.Pointcut;Importorg.springframework.stereotype.Component;/** * * @authorAdministrator * Perform specific actions after an AOP intercept*/@Aspect @component Public classlogintercept {@Pointcut ("Execution (Public * com.itsoft.action). *.*(..))") Public voidRecordlog () {} @Before ("Recordlog ()") Public voidbefore () { This. Printlog ("Before the operation log @before method execution has been recorded"); } @Around ("Recordlog ()") Public voidAround (Proceedingjoinpoint PJP)throwsthrowable{ This. Printlog ("Before the operation log @around method execution has been recorded"); Pjp.proceed (); This. Printlog ("After the operation log @around method execution has been recorded"); } @After ("Recordlog ()") Public voidAfter () { This. Printlog ("After the operation log @after method execution has been recorded"); } Private voidPrintlog (String str) {System.out.println (str); }}
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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/context Http://www.springframework.org/schema/context/spring-cont Ext-3.0.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-be Ans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3. 0.xsd "> <Context:annotation-config/> <Context:component-scanBase-package= "Com.itsoft"/> <Aop:aspectj-autoproxy/></Beans>
Code Demo SVN SVN://GITEE.COM/RAINYN/SSM
Reference transfer from Http://outofmemory.cn/code-snippet/3025/spring-AOP-Around-Before-After-differentiate
Spring AOP @Around @Before @After Differences