Spring AOP Detailed

Source: Internet
Author: User

AOP (Aspect oriented programming): Plane-oriented programming.
Aspect-oriented programming (also called facet-oriented programming) is a hotspot in software development and an important part of the spring framework. AOP enables the isolation of parts of the business logic, which reduces the coupling between parts of the business logic, improves the reusability of the program, and improves the efficiency of development.
Main uses: Logging, performance statistics, security control, rights management, transaction processing, exception handling, resource pool management.
Individual notifications:
A pre-notification (before advice): A notification that is performed before a connection point (Joinpoint), but this notification does not prevent execution before the connection point. XML is declared using the <aop:before> element inside the <aop:aspect>, for example, the Dobefore method in Testaspect. The @before declaration is used in annotations, for example, the Dobefore method in Testannotationaspect.
B Post notification (after advice): Notification that is executed when a connection point exits (whether it is a normal return or an abnormal exit). The XML is declared using the <aop:after> element inside the <aop:aspect>. For example, the Doafter method in Testaspect, so the Doafter method is still executed when Bserviceimpl.barb throws an exception in Aoptest. The @after declaration is used in annotations.
C after return notification (after return advice): A notification that is executed after a connection point is completed normally, excluding the case where an exception was thrown. The XML is declared using the <after-returning> element inside the <aop:aspect>. Use @afterreturning declaration in annotations;
D Surround Notification (Around advice): A notification that surrounds a connection point, similar to the Dofilter method of the filter in the servlet specification in the Web. You can customize the behavior before and after the call to the method, or you can choose not to execute. The XML is declared using the <aop:around> element inside the <aop:aspect>. For example, the Doaround method in Testaspect. The @around declaration is used in annotations.
E-Notification After an exception is thrown (after throwing advice): Notification that is executed when the method throws an exception. The XML is declared using the <aop:after-throwing> element inside the <aop:aspect>. For example, the Dothrowing method in Testaspect. The @afterthrowing declaration is used in annotations.
Notification execution Order: pre-notification → surround notification connection point before → connection point execution → surround notification connection point → return notification → post notification → (if an exception occurs) exception notification → post notification


1. applicationcontext.xml file configuration, the following underlined places

<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:context=" Http://www.springframework.org/schema/context "xmlns:tx="/HTTP/ Www.springframework.org/schema/tx "xmlns:jdbc=" Http://www.springframework.org/schema/jdbc "<xmlns:aop=" http:/ /www.springframework.org/schema/aop "xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/context http://w Ww.springframework.org/schema/context/spring-context-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX http://www . springframework.org/schema/tx/spring-tx-3.0.xsd Http://www.springframework.org/schema/jdbc Http://www.springfra Mework.org/schema/jdbc/spring-jdbc-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework. Org/schema/aop/spring-aop-3.0.xsd "> <aop:aspectj-autoproxy/>........</beans> 

2. Create an AOP class

Package Cn.com.abel.springproject.aop;import Org.aspectj.lang.joinpoint;import Org.aspectj.lang.proceedingjoinpoint;import Org.aspectj.lang.annotation.after;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.springframework.stereotype.Component, @Component @Aspectpublic class TESTAOP {//Surround notification @around ("execution (* Cn.com.abel.springProject.service.TestService.sayHello (..))") public void Aroundexecute (Proceedingjoinpoint point) throws throwable{System.out.println ("================around    Start "); Parameters: Point.getargs () [0] subscript indicates the first parameter of the method, starting from 0.      hereinafter Point.proceed ();  System.out.println ("================around end");  }//Pre-notification @before ("execution (* Cn.com.abel.springProject.service.TestService.sayHello (..))") public void BeforeExecute (Joinpoint point) throws Throwable{system.out.println ("================beforE ");//Parameter: Point.getargs () [0]}//Post notification @afterreturning (" Execution (*  Cn.com.abel.springProject.service.TestService.sayHello (..)) ") public void Afterreturningexecute (Joinpoint point) throws Throwable{system.out.println ("================ Afterreturning ");}  Notifies @afterthrowing after an exception is thrown ("Execution (* Cn.com.abel.springProject.service.TestService.sayHello (..))") public void Afterthrowingexecute (Joinpoint point) throws Throwable{system.out.println ("================ Afterthrowing ");}  Final Notice @after ("Execution (* Cn.com.abel.springProject.service.TestService.sayHello (..))") public void AfterExecute (Joinpoint point) throws Throwable{system.out.println ("================after");}}


3. Target method

Package Cn.com.abel.springproject.service;import Org.springframework.stereotype.Service; @Servicepublic class Testservice {public string SayHello (Integer age, string name) {     System.out.println ("================hello word");}}


Testservice.sayhello method, output result without exception:

================around start
================before
================hello Word
================afterreturning
================around End
================after


Results when an exception occurs:

================around start
================before
================hello Word
================afterthrowing
================after


Spring AOP Detailed

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.