Spring AOP @Around @Before @After Differences

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.