Several notifications for spring (front, back, surround, exception, final)

Source: Internet
Author: User
Tags aop throwable

1. No abnormal

2, there is an abnormal

1, the proxy class interface Person.java
1  PackageCom.xiaostudy;2 3 /**4 * @desc proxy class Interface5  * 6  * @authorXiaostudy7  *8  */9  Public InterfacePerson {Ten  One      Public voidAdd (); A      Public voidupdate (); -      Public voidDelete (); -}
2, by the agent class Personimple.java
1  PackageCom.xiaostudy;2 3 /**4 * @desc by proxy class5  * 6  * @authorXiaostudy7  *8  */9  Public classPersonimpleImplementsPerson {Ten  One     /** A * @desc Implement Interface method -      */ -      Public voidAdd () { theSystem.out.println ("Add () >>>>>>>>"); -     } -  - @Override +      Public voidUpdate () { -System.out.println ("Update () >>>>>>>>"); + //int i = 1/0; A     } at  - @Override -      Public voidDelete () { -System.out.println ("Delete () >>>>>>>>"); -     } -      in}
3, Myaspectj.java
1  PackageCom.xiaostudy;2 3 ImportOrg.aspectj.lang.JoinPoint;4 ImportOrg.aspectj.lang.ProceedingJoinPoint;5 6 /**7 * @desc notification class8  * 9  * @authorXiaostudyTen  * One  */ A  Public classMYASPECTJ { -      -      Public voidMybefort (Joinpoint joinpoint) { theSystem.out.println ("Pre-Notification >>>>>>>>>joinpoint:" +joinpoint.getsignature (). GetName ()); -     } -      -      Public voidmyafterreturning (Joinpoint joinpoint, Object ret) { +System.out.println ("Post-notification >>>>>>>>>joinpoint:" +joinpoint.getsignature (). GetName () -+ ", ret:" +ret); +     } A      at      PublicObject Myaround (Proceedingjoinpoint joinpoint)throwsThrowable { -System.out.println ("Surround notice = >>>>>>>>>>>"); -Object obj =joinpoint.proceed (); -System.out.println ("Surround notice = <<<<<<<<<<<"); -         returnobj; -     } in      -      Public voidMythrowint (Joinpoint joinpoint, Throwable e) { toSYSTEM.OUT.PRINTLN ("Exception notification >>>>>>>>>joinpoint:" +joinpoint.getsignature (). GetName () ++ ", E:" +e.getmessage ()); -System.exit (0); the     } *      $      Public voidMyafter (Joinpoint joinpoint) {Panax NotoginsengSYSTEM.OUT.PRINTLN ("Final notification >>>>>>>>>joinpoint:" +joinpoint.getsignature (). GetName ()); -     } the}
4. Spring configuration file Applicationcontext.xml
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"5Xsi:schemalocation= "Http://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<!--created by proxy class--Ten<bean id= "Person"class= "Com.xiaostudy.PersonImple" ></bean> One<!--to create a slice class-- A<bean id= "Advice"class= "Com.xiaostudy.MyAspectJ" ></bean> -<!--SPRINGAOP Programming-- -<aop:config> the<!--to declare "facets" for the slice class to get a notification (method)-- -<aop:aspect ref= "Advice" > -<!--declare a pointcut, all notifications can be used-- -<aop:pointcut expression= "Execution (* com.xiaostudy.personimple.* (..))" id= "Mypointcut"/> +<!--pre-notification: Method name, pointcut-ref means: All notifications are shared, (pointcut means: Only the current notification is available, others are unavailable)- -<aop:before method= "Mybefort" pointcut-ref= "Mypointcut"/> +<!--Post notification: Returning: The second parameter name for the post notification, the content is the return value of the method-- A<aop:after-returning method= "myafterreturning" returning= "ret" pointcut-ref= "Mypointcut"/> at<!--surround Notifications- -<aop:around method= "Myaround" pointcut-ref= "Mypointcut"/> -<!--exception Notification: Throwing: The second parameter of the exception notification, the content is exception information-- -<aop:after-throwing method= "Mythrowint" throwing= "E" pointcut-ref= "Mypointcut"/> -<!--Final Notice-- -<aop:after method= "Myafter" pointcut-ref= "Mypointcut"/> in</aop:aspect> -</aop:config> to</beans>
5. Test class Test.java
1  PackageCom.xiaostudy;2 3 ImportOrg.springframework.context.ApplicationContext;4 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;5 6 /**7 * @desc Test class8  * 9  * @authorXiaostudyTen  * One  */ A  Public classTest { -  -      Public Static voidMain (string[] args) { theApplicationContext AC =NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); -Person person = ac.getbean (' person ', person.class); - Person.add (); - person.update (); + Person.delete (); -     } +  A}

Several notifications for spring (front, back, surround, exception, final)

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.