Spring_spring and AOP_ASPECTJ annotation-based AOP implementations

Source: Internet
Author: User
Tags stub

Tag: Message roc 2-2 Pen object blank Create ASE--

I. The relationship between AspectJ, spring and AOP

ASPECTJ is a tangent-oriented framework that extends the Java language. ASPECTJ defines the AOP syntax, so it has a dedicated compiler to generate class files that conform to the Java Byte encoding specification. (Baidu Encyclopedia)

Spring also introduces AspectJ's implementation of AOP into its own framework.

When using AOP development in spring, the implementation of ASPECTJ is generally used.

Ii. types of notifications for ASPECTJ

    1. Front-facing notifications
    2. Post notification
    3. Surround Notifications
    4. Exception notification
    5. Final Notice

Three, AspectJ's pointcut expression


The part of the expression that adds "" can be omitted, and parts are separated by spaces. The following symbols can be used in them:

Execution (* *). Service.*.* (..))

Specifies that all the methods in the service sub-package under all packages are pointcuts under all classes (interfaces)

Execution (* *). Isomeservice.* (.. ))

Specify all the methods in the Isomeservice interface under all packages as Pointcuts

Third, the development environment of ASPECTJ


Importing 2 jar Packages

Spring-framework-3.0.2.release-dependencies\org.aspectj\com.springsource.org.aspectj.weaver\1.6.8.release

Iv. ASPECTJ Annotation-based AOP implementation

1. Front-facing notification

1 // main Business Interface 2  Public Interface Isomeservice {3   // Target Method 4     void Dofirst (); 5     String Dosecond (); 6     void Dothird (); 7     8 }
Isomeservice
1  Public classSomeserviceimplImplementsIsomeservice {2 3 @Override4      Public voidDofirst () {5         //TODO auto-generated Method Stub6System.out.println ("Execute Dofirst () method"));7     }8 9 @OverrideTen      PublicString Dosecond () { One         //TODO auto-generated Method Stub ASystem.out.println ("Execute Dosecond () method")); -         return"ABCDE"; -     } the  - @Override -      Public voidDothird () { -         //TODO auto-generated Method Stub +System.out.println ("Execute Dothird () method")); -     } +  A}
Someserviceimpl
1 ImportOrg.aspectj.lang.JoinPoint;2 ImportOrg.aspectj.lang.annotation.Aspect;3 ImportOrg.aspectj.lang.annotation.Before;4 5@Aspect//indicates that the current class is a tangent6  Public classMyaspect {7@Before ("Execution (* *). Isomeservice.dofirst (..)) ")8     Public voidbefore () {9System.out.println ("Perform a pre-notification method");Ten    } One     A@Before ("Execution (* *). Isomeservice.dofirst (..)) ") -     Public voidbefore (Joinpoint JP) { -System.out.println ("Perform a pre-notification method jp=" +JP); the    } -      -}
Myaspect
1 Importorg.junit.Test;2 ImportOrg.springframework.context.ApplicationContext;3 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;4 5  Public classMyTest {6 7 @Test8      Public voidtest01 () {9         //creating a Container objectTenString resource = "Com/bjpowernode/annotation/applicationcontext.xml"; OneApplicationContext ac=NewClasspathxmlapplicationcontext (Resource); A         -Isomeservice service= (Isomeservice) Ac.getbean ("Someservice"); - Service.dofirst (); theSystem.out.println ("--------------------"); - Service.dosecond (); -System.out.println ("--------------------"); - Service.dothird (); +     } -  +}
MyTest
1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi:schemalocation="5http//Www.springframework.org/schema/beans6http//www.springframework.org/schema/beans/spring-beans.xsd7http//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP8http//www.springframework.org/schema/aop/spring-aop.xsd ">9<!--registering slices---Ten<bean id= "Myaspect"class= "Com.bjpowernode.annotation.MyAspect" ></bean> One       A<!--register target objects-- -<bean id= "Someservice"class= "Com.bjpowernode.annotation.SomeServiceImpl" ></bean> -        the<!--registered ASPECTJ Auto Agent--- -<aop:aspectj-autoproxy/> -</beans>
ApplicationContext

Output:

Perform a pre- notification method to perform a pre-notification method JP=execution (void  Com.bjpowernode.annotation.ISomeService.doFirst ()) Execute the Dofirst () method-------------------- Execute the Dosecond ( ) method-------------------- Execute the Dothird ( ) method
Output

2. Rear-facing notification

1@AfterReturning ("Execution (* *). Isomeservice.dosecond (..)) ")2     Public voidmyafterreturning () {3System.out.println ("Perform a post-notification method");4        5    }6    7@AfterReturning (value= "Execution (* *). Isomeservice.dosecond (..)) ", returning=" result ")8     Public voidmyafterreturning (Object result) {9System.out.println ("Perform post-notification method result=" +result);Ten         One}
Myaspect

3. Surround Notification

1@Around ("Execution (* *). Isomeservice.dosecond (..)) ")2     PublicObject Myaround (Proceedingjoinpoint PJP)throwsthrowable{3System.out.println ("Execute wrapping notification method, before target method executes");4        //Execute target Method5Object result =pjp.proceed ();6System.out.println ("Execution wrapping notification method, after target method execution");7     returnresult;8        9}
Myaspect

Output:

1 performs a wrapping notification method before the target method executes 2 Execute the Dosecond () method 3 performing a wrapping notification method after the target method executes
Output

4. Exception notification

1  @AfterThrowing ("Execution (* *). Isomeservice.dothird (..)) " )2public    void  myafterthrowing () {3       System.out.println ("Execute Exception Notification method");   4    }
Myaspect
1   @AfterThrowing (value= "Execution (* *). Isomeservice.dothird (..)) ", throwing=" ex ")2public    void  Myafterthrowing (Exception ex) {3       System.out.println ("Execute Exception Notification method ex=" +Ex.getmessage ());   4    }
Myaspect
1 How to perform exception notification ex=/by zero
Output

5. Final Notice

1  @After ("Execution (* *). Isomeservice.dothird (..)) " )2public    void  myafter () {3       System.out.println (" Implementation of the final notification method ");   4    }
View Code

Definition of Entry point

Defines a pointcut, called Dothirdpointcut ()

1  @After ("Dothirdpointcut ()")2public    void  myafter () {3       System.out.println ("Implementation of the final notice method");   4    }5    // defines a pointcut called dothirdpointcut ()6    @ Pointcut ("Execution (* *). Isomeservice.dothird (..)) " )7public    void  dothirdpointcut () {}8 }

Spring_spring and AOP_ASPECTJ annotation-based AOP implementations

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.