1, 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, Agent class Personimple.java
1 PackageCom.xiaostudy;2 3 Importorg.springframework.stereotype.Component;4 5 /**6 * @desc by proxy class7 * 8 * @authorXiaostudy9 *Ten */ One@Component ("person")//class Annotations A Public classPersonimpleImplementsPerson { - - /** the * @desc Implement Interface method - */ - Public voidAdd () { -System.out.println ("Add () ..."); + } - + @Override A Public voidUpdate () { atSystem.out.println ("Update () ..."); - //int i = 1/0; - } - - @Override - Public voidDelete () { inSystem.out.println ("Delete () ..."); - } to +}3, notice class Myaspectj.java
1 PackageCom.xiaostudy;2 3 ImportOrg.aspectj.lang.JoinPoint;4 ImportOrg.aspectj.lang.ProceedingJoinPoint;5 ImportOrg.aspectj.lang.annotation.After;6 Importorg.aspectj.lang.annotation.AfterReturning;7 Importorg.aspectj.lang.annotation.AfterThrowing;8 ImportOrg.aspectj.lang.annotation.Around;9 ImportOrg.aspectj.lang.annotation.Aspect;Ten ImportOrg.aspectj.lang.annotation.Before; One ImportOrg.aspectj.lang.annotation.Pointcut; A Importorg.springframework.stereotype.Component; - - /** the * @desc notification class - * - * @authorXiaostudy - * + */ -@Component//class Annotations +@Aspect//ASPECTJ Annotations A Public classMYASPECTJ { at - //declaring a public entry point -@Pointcut ("Execution (* com.xiaostudy.personimple.* (..))") - Public voidmypointcut () { - - } in - //Pre-notification annotations, when there is only one parameter, value can be omitted without writing to@Before ("Execution (* com.xiaostudy.personimple.* (..))") + Public voidMybefort (Joinpoint joinpoint) { -System.out.println ("Pre-Notification >>>>>>>>>joinpoint:" +joinpoint.getsignature (). GetName ()); the } * $ //post-notification annotations, value must be written when the parameter is greater than 1 o'clockPanax Notoginseng@AfterReturning (value= "mypointcut ()", returning= "ret") - Public voidmyafterreturning (Joinpoint joinpoint, Object ret) { theSystem.out.println ("Post-notification >>>>>>>>>joinpoint:" +joinpoint.getsignature (). GetName () ++ ", ret:" +ret); A } the + //Surround Notification Annotations -@Around ("Mypointcut ()") $ PublicObject Myaround (Proceedingjoinpoint joinpoint)throwsThrowable { $System.out.println ("Surround notice = >>>>>>>>>>>"); -Object obj =joinpoint.proceed (); -System.out.println ("Surround notice = <<<<<<<<<<<"); the returnobj; - }Wuyi the //Exception Notification Annotations -@AfterThrowing (value= "mypointcut ()", throwing= "E") Wu Public voidMythrowint (Joinpoint joinpoint, Throwable e) { -SYSTEM.OUT.PRINTLN ("Exception notification >>>>>>>>>joinpoint:" +joinpoint.getsignature (). GetName () About+ ", E:" +e.getmessage ()); $System.exit (0); - } - - //Final notification Annotations A@After ("Mypointcut ()") + Public voidMyafter (Joinpoint joinpoint) { theSYSTEM.OUT.PRINTLN ("Final notification >>>>>>>>>joinpoint:" +joinpoint.getsignature (). GetName ()); - } $}4. Spring configuration file Applicationcontext.xml
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 Xmlns:context= "Http://www.springframework.org/schema/context"6 xsi:schemalocation= "Http://www.springframework.org/schema/beans7 http://www.springframework.org/schema/beans/spring-beans.xsd8 HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP9 http://www.springframework.org/schema/aop/spring-aop.xsdTen Http://www.springframework.org/schema/context One http://www.springframework.org/schema/context/spring-context.xsd "> A <!--Scan Annotation Class - - <Context:component-scanBase-package= "Com.xiaostudy"></Context:component-scan> - <!--determining the validity of an AOP annotation - the <Aop:aspectj-autoproxy></Aop:aspectj-autoproxy> - </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}
Annotations for spring Notifications