Peak Spring4 Learning (6) Application examples of spring AOP

Source: Internet
Author: User
Tags throwable

I. Introduction of AOP:

Ii. Examples of AOP:

Iii. Examples of Use

Requirements: Before and after student added, print the log information;

0) Spring AOP needs to refer to the jar package:

1) Studentservice.java Interface:

 Package Com.cy.service;  Public Interface Studentservice {    publicvoid  addstudent (String name);}
View Code

2) Studentserviceimpl.java Implementation class:

 Package Com.cy.service.impl; Import Com.cy.service.StudentService;  Public class Implements Studentservice {    @Override    publicvoid  addstudent (String name) {        System.out.println ("add student" +name);}    }
View Code

3) Studentserviceaspect.java Slice class:

 PackageCom.cy.advice;ImportOrg.aspectj.lang.JoinPoint;ImportOrg.aspectj.lang.ProceedingJoinPoint; Public classStudentserviceaspect {//pre-notification, before method execution     Public voidDobefore (joinpoint JP) {System.out.println ("Class Name:" +jp.gettarget (). GetClass (). GetName ()); System.out.println ("Method Name:" +jp.getsignature (). GetName ()); System.out.println ("Start adding students:" + Jp.getargs () [0]); }        //After the post-notification method is complete     Public voidDoafter (joinpoint JP) {System.out.println ("Class Name:" +jp.gettarget (). GetClass (). GetName ()); System.out.println ("Method Name:" +jp.getsignature (). GetName ()); System.out.println ("Complete Add Student:" + Jp.getargs () [0]); }        //Surround notifications can add logic before and after business methods     PublicObject Doaround (Proceedingjoinpoint PJP)throwsthrowable{System.out.println ("Before adding students"); Object RetVal= Pjp.proceed ();//retval is the return value of the studentserviceimpl.addstudent (String name) method // because the return value is void, the console prints null System.out.println ("After adding students");        System.out.println (RetVal); returnRetVal; }        //Back to Notifications     Public voiddoafterreturning (joinpoint JP) {System.out.println ("Return Notification"); }        //Exception Notification     Public voiddoafterthrowing (joinpoint JP, Throwable ex) {System.out.println ("Exception Notification"); System.out.println ("Exception Info:" +ex.getmessage ()); }}

4) beans.xml configuration:

<?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-bean S.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/a Op/spring-aop-2.5.xsd ">          <BeanID= "Studentservice"class= "Com.cy.service.impl.StudentServiceImpl"></Bean>     <BeanID= "Studentserviceaspect"class= "Com.cy.advice.StudentServiceAspect"></Bean>          <Aop:config>         <Aop:aspectID= "Studentserviceaspect"ref= "Studentserviceaspect">             <Aop:pointcutexpression= "Execution (* com.cy.service). *.*(..))"ID= "Businessservice"/>             <Aop:beforeMethod= "Dobefore"Pointcut-ref= "Businessservice"/>             <Aop:afterMethod= "Doafter"Pointcut-ref= "Businessservice"/>             <Aop:aroundMethod= "Doaround"Pointcut-ref= "Businessservice"/>             <aop:after-returningMethod= "Doafterreturning"Pointcut-ref= "Businessservice"/>             <aop:after-throwingMethod= "Doafterthrowing"Pointcut-ref= "Businessservice"throwing= "Ex"/>         </Aop:aspect>     </Aop:config>     </Beans>

5) Test Code:

 Public class T {        publicstaticvoid  main (string[] args) {        new Classpathxmlapplicationcontext ("Beans.xml");         = (Studentservice) ac.getbean ("Studentservice");        Studentservice.addstudent ("Zhang San");    }    }
View Code

1) No exception is thrown, no exception notification; console print:

2) The Addstudent method constructs the exception, has the exception notification, the console prints:

Studentserviceimpl.java:

 Package Com.cy.service.impl; Import Com.cy.service.StudentService;  Public class Implements Studentservice {    @Override    publicvoid  addstudent (String name) {        System.out.println ("add student" +name);        System.out.println (1/0);    }}
View Code

The exception that is thrown in the Doaround method is this sentence:

--Object RetVal = Pjp.proceed ();

Peak Spring4 Learning (6) Examples of spring AOP applications

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.