Spring's AOP

Source: Internet
Author: User

It is recommended to use annotations instead of XML

 PackageOrg.zln.module.test3_aop.interceptor;ImportOrg.aspectj.lang.ProceedingJoinPoint;Importorg.aspectj.lang.annotation.*;/*** Created by Coolkid on 2015/6/6 0006.*//*declares that this is a facet*/@Aspect Public classMyinterceptor {/*declares the Pointcut defines the method to intercept method the body is empty, using an expression to describe which method is the Pointcut*/@Pointcut ("Execution (* org.zln.module.test3_aop.service.impl.personservicebean.* (..))")    Private voidAnymethod () {}/*declaring a pre-notification*/@Before ("Anymethod ()")     Public voidDoaccesscheck () {System.out.println ("Pre-notification"); } @After ("Anymethod ()")     Public voidDoafter () {System.out.println ("Final Notice"); } @AfterThrowing (Pointcut= "Anymethod ()", throwing= "E")     Public voiddoafterthrowing (Exception e) {System.out.println ("Exception Notification:" +e); } @Around ("Anymethod ()")     PublicObject dobasicprofiling (Proceedingjoinpoint PJP)throwsThrowable {//if () {//determine if the user is in a permissionSystem.out.println ("Entry method")); /*executing a business bean method*/Object Result=pjp.proceed (); System.out.println ("Exit Method"); //}        returnresult; }}/*Execution (* org.zln.module.test3_aop.service). *.*(..))  Describes the first * return value type, * represents any return value type Org.zln.module.test3_aop.service package name:    The class under the sub-package also intercepts the second class, * represents the third * method of all classes, * represents all methods (..) Parameter of the method,: Represents any defined Anymethod method as the name of this pointcut execution (* org.zln.module.test3_aop.service.impl.personservicebean.* (..)) Instructions to intercept all methods of the Personservicebean class*/
<?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"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5. xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">    <!--Open the AOP annotation parser -    <Aop:aspectj-autoproxy/>    <!--handing custom interceptors to spring management -    <BeanID= "Myinterceptor"class= "Org.zln.module.test3_aop.interceptor.MyInterceptor"/>    <BeanID= "PERSONSERVICEAOP"class= "Org.zln.module.test3_aop.service.impl.PersonServiceBean"/></Beans>

Spring's AOP

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.