Introduction to Spring AOP

Source: Internet
Author: User

First, what is AOP?

The two core ideas of spring are IOC and AOP. So spring's AOP is divine horse meaning? AOP is the abbreviation of Aspect oriented programming, meaning: aspect-oriented programming, through the pre-compilation method and run-time dynamic agent implementation in the case of non-modification of the source code to the program dynamically unified add the function of a technology. AOP is actually a continuation of the GOF design pattern, and the design pattern pursues the decoupling between the caller and the callee, improving the flexibility and extensibility of the code, and AOP can be said to be an implementation of this goal.

What is the purpose of the two?

Mainly used for logging, performance statistics, security control, transaction processing, exception handling and other code from the business logic code, through the separation of these behaviors, we want to be able to separate them into non-guided business logic methods, and then change these behavior when the code of the business logic does not affect.

A bit abstract, the following gives an example directly

Third, Demo

1, small demo overall structure

2, the required jar package

3. Service Layer Interface

1  Package Com.xpw.service; 2 3  Public Interface UserService {4      Public string Add (string name); 5 }

Service Implementation Layer Userserviceimpl code

 Package Com.xpw.service.impl; Import Com.xpw.service.UserService;  Public class Implements userservice {    @Override    public  string Add (string name) {        System.out.println ( "Do add ...");         return name;}    }

"Encapsulation" class for facets

 PackageCom.xpw.aspect;ImportOrg.aspectj.lang.ProceedingJoinPoint; Public classUseraspect { Public voidDobefore () {System.out.println ("Before do add ..."); }     Public voidDoafter () {System.out.println ("After do add ..."); }         Public voidDoaround (Proceedingjoinpoint PJP)throwsthrowable{System.out.println ("Before do add in around ()"); Object Res=pjp.proceed (); System.out.println ("res =" +res); System.out.println ("Before do add in around ()"); }}

4, the Beans.xml configuration

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"5 xsi:schemalocation= "Http://www.springframework.org/schema/beans6 http://www.springframework.org/schema/beans/spring-beans.xsd7 HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP8 http://www.springframework.org/schema/aop/spring-aop.xsd ">9     Ten     <BeanID= "Useraspect"class= "Com.xpw.aspect.UserAspect"></Bean> One      A     <BeanID= "UserService"class= "Com.xpw.service.impl.UserServiceImpl"></Bean> -      -     <Aop:config> the         <!--Defining Facets - -         <Aop:aspectID= "Myaspect"ref= "Useraspect"> -             <Aop:pointcutexpression= "Execution (* com.xpw.service.*.* (..))"ID= "Mypointcut"/> +             <Aop:beforeMethod= "Dobefore"Pointcut-ref= "Mypointcut"/> A             <Aop:afterMethod= "Doafter"Pointcut-ref= "Mypointcut"/> at             <Aop:aroundMethod= "Doaround"Pointcut-ref= "Mypointcut"/>     -         </Aop:aspect> -     </Aop:config> -      - </Beans>

5. Testing

1  PackageCom.xpw.aspect;2 3 Importorg.junit.Test;4 ImportOrg.springframework.context.ApplicationContext;5 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;6 7 ImportCom.xpw.service.UserService;8 9 Ten  Public classUseraspettest { One      A     Private StaticApplicationContext ac; -      -     Static{ theAC =NewClasspathxmlapplicationcontext ("Beans.xml"); -     } -      - @Test +      Public voidTestaspect () { -UserService US = (userservice) ac.getbean ("UserService"); +Us.add ("Wei Shao"); A     } at}

6. Results

   do does add in around () does = do Add ...

7. Conclusion

By the above results, you can know that after () < around () the priority < before (), where the around tangent method has a return value, many times we probably need this return value to do log processing and so on, Its parameters are the Proceedingjoinpoint object, through Pjp.getsignature (). GetName () can get the method name of the plunge through the Pjp.gettarget (). GetClass (). GetName () Gets the name of the cut-in target class and gets the passed-in parameter by Pjp.getargs ()

System.out.println ("method =" + pjp.getsignature (). GetName ()); System.out.println ("Class name =" + pjp.gettarget (). GetClass (). GetName ()); System.out.println ("parm =" + Pjp.getargs () [0]);

Object res = Pjp.proceed (); The code in front of this line of code is executed when the target method (add) executes, while the code behind it is executed after the execution of the target method (such as add () in this example).

Of course, before, after, around do not need to use together, choose one can

Introduction to Spring 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.