Basic AOP knowledge and annotation method and xml configuration method for AOP aspect Programming

Source: Internet
Author: User

<Span style = "font-family:; font-size: 10.5pt; letter-Spacing: 0pt;"> AOP concept </span>

Aspect (aspect): it is similar to a class, but the focus of the two is different. The class is the abstraction of Object Features, while the aspect is the abstraction of cross-cutting concerns.

Joinpoint: the so-called connection point is the intercepted point. In spring, these points are methods, because spring only supports connection points of the method type, in fact, joinpoint can also be a field or class constructor.

Pointcut: defines the points to intercept.

Advice (notification): After the intercepted joinpoint, the task is to notify. Notifications are divided into pre-notification, post-notification, exception notification, final notification, and surround notification. Notifications are what to do.

Target: the target object of the proxy.

Weave: it refers to the process of applying the aspect to the target object and causing the proxy object to be created.

Introduction (introduced); without modifying the class code, introduction can dynamically add some methods or fields at runtime.

Jar packages used by AOP

Spring. Jar

Commons-logging.jar

Aspectjweaver. Jar

Aspectjrt. Jar

Cglib-nodep-2.1.3.jar

If the sr250 annotation is used, such as @ resource/@ postconstruct/@ predestroy, The common-annotation.jar is also introduced.


Configuration of the AOP namespace

Use Spring framework AOP to introduce the following AOP namespace (red part)

<? XML version = "1.0" encoding = "UTF-8"?>

<Beans xmlns = "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>

<AOP: aspectj-autoproxy/>

</Beans>


The following is the main code of the sample of AOP aspect programming using the annotation method.

Package COM. gdhdcy. service; 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. aspectj. lang. annotation. pointcut; @ aspectpublic class myinterceptor {@ pointcut ("execution (* COM. gdhdcy. service. impl. personservicebean. *(..)) ") Private void anymethod () {}; // life start point @ before (" anymethod () & ARGs (name) ") // enter the name of the Start method, remember to add () Public void doaccesscheck (string name) {system. out. println ("pre-notification" + name);} // @ afterreturning ("anymethod ()") @ afterreturning (pointcut = "anymethod ()", returning = "result ") // obtain the return value public void doafterreturning (string result) {system. out. println ("post notification" + result) ;}@ after ("anymethod ()") Public void doafter () {system. out. println ("final notification");} // @ afterthrowing ("anymethod ()") @ afterthrowing (pointcut = "anymethod ()", throwing = "e ") public void doafterthrowing (exception e) {system. out. println ("exception notification" + E) ;}@ around ("anymethod ()") Public object doaround (proceedingjoinpoint pjp) throws throwable {system. out. println ("surround notification entry"); object result = pjp. proceed (); system. out. println ("surround notification exit"); return result ;}}

Annotation method beans. xml configuration document

<? XML version = "1.0" encoding = "UTF-8"?> <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: 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/conte XT http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <AOP: aspectj-autoproxy/> <! -- Based on Annotation --> <bean id = "myinterceptor" class = "com. gdhdcy. service. myinterceptor "/> <bean id =" personservice "class =" com. gdhdcy. service. impl. personservicebean "/> </beans>

Source code link:

Http://pan.baidu.com/s/1jGmQMjG


Main Code of the XML method AOP aspect programming example

Package CN. itcast. service; import Org. aspectj. lang. proceedingjoinpoint;/*** aspect **/public class myinterceptor {public void doaccesscheck () {system. out. println ("pre-notification");} public void doafterreturning () {system. out. println ("post notification");} public void doafter () {system. out. println ("final notification");} public void doafterthrowing () {system. out. println ("exception notification");} public object dobasicprofiling (proceedingjoinpoint pjp) throws throwable {system. out. println ("access method"); object result = pjp. proceed (); system. out. println ("Exit method"); return result ;}}

Xml configuration document

<?xml version="1.0" encoding="UTF-8"?><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: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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">        <aop:aspectj-autoproxy/>         <bean id="personService" class="com.gdhdcy.service.impl.PersonServiceBean"></bean>        <bean id="aspetbean" class="com.gdhdcy.service.MyInterceptor"/>        <aop:config>        <aop:aspect id="asp" ref="aspetbean">        <aop:pointcut id="mycut" expression="execution(* com.gdhdcy.service..*.*(..))"/>        <aop:before pointcut-ref="mycut" method="doAccessCheck"/>        <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>  <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>  <aop:after pointcut-ref="mycut" method="doAfter"/>  <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>        </aop:aspect>        </aop:config></beans>


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.