Notification types defined by the ASPECTJ framework in spring

Source: Internet
Author: User
Tags aop throwable

Target Object interface
1publicinterface iuserservice {2public void Add (); 3 Public void update (); 4 Public void del (); 5 Public void search (); 6 }

1. Create a target object

1  Public classUserserviceimplImplementsIuserservice {2 @Override3      Public voidAdd () {4System.out.println ("Add ...."));5     }6 @Override7      Public voidUpdate () {8SYSTEM.OUT.PRINTLN ("Update ...."));9     }Ten @Override One      Public voiddel () { ASystem.out.println ("Del ..."); -     } - @Override the      Public voidsearch () { -System.out.println ("Search ....")); -     } -}

2. Create notifications (enhanced advice)

1 //Adcice Enhanced Class2  Public classUserservicehelper {3     //front-facing notifications4      Public voidbefore (Joinpoint JP) {5SYSTEM.OUT.PRINTLN ("Intercept target class" +jp.getsignature (). Getdeclaringtypename ());6SYSTEM.OUT.PRINTLN ("Intercept target method name" +jp.getsignature (). GetName ());7System.out.println ("Pre-notification");8         9     }Ten      Public voidafterreturning (Joinpoint jp,object val) { OneSYSTEM.OUT.PRINTLN ("Target return value" +val); ASYSTEM.OUT.PRINTLN ("Post notification"); -     } -      PublicObject Around (proceedingjoinpoint PJP)throwsthrowable{ theSYSTEM.OUT.PRINTLN ("Notification before wrapping"); -Object value =pjp.proceed (); -SYSTEM.OUT.PRINTLN ("Surround Notification"); -         returnvalue; +     } -      Public voidafterthrowing (Joinpoint jp,throwable ex) { +     //output If there is an exception ASYSTEM.OUT.PRINTLN ("Exception throw notification" +ex); at     } -      Public voidAfter (Joinpoint JP) { -SYSTEM.OUT.PRINTLN ("Intercept target method name" +jp.getsignature (). GetName ()); -System.out.println ("Final Notice"); -     } -}

3.1 Applicationcontext.xml configuration in spring:

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" xmlns:context= "Http://www.springframework.org/schema/context"4xsi:schemalocation="5http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd6http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">7<ImportResource= "./aop4.xml"/>8</beans>

3.2 XMP configuration in spring Apo1.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:context= "Http://www.springframework.org/schema/context"5xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"6xsi:schemalocation="7http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd8http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd9http//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop.xsd">Ten<!--target Object-- One<bean id= "UserService"class= "Cn.aspectjs.UserServiceImpl"/> A<!--advice Notifications- -<bean id= "Userserviceadvice"class= "Cn.aspectjs.UserServiceHelper"/> -<!--config is used to declare that the content means that the ASPECTJ framework implements Aop,aspect to configure facets, regardless of the interface. the<aop:config proxy-target-class= "true" > -<aop:aspect ref= "Userserviceadvice" > -<aop:pointcut expression= "Execution (* *.add (..))" id= "Mypointcut"/> -<!--method is enhanced class-- +<aop:before method= ""/> -<aop:before method= "Before" pointcut-ref= "Mypointcut"/> +<aop:after-returning method= "afterreturning" pointcut-ref= "Mypointcut" returning= "Val"/> A<aop:around method= "Around" pointcut-ref= "Mypointcut"/> at<aop:after-throwing method= "afterthrowing" pointcut-ref= "Mypointcut" throwing= "ex"/> -<aop:after method= "after" pointcut-ref= "Mypointcut"/> -</aop:aspect> -</aop:config> -</beans>

4. Test class

//spring Integrated JUnit Testing
@RunWith (Springjunit4classrunner.class)
parameter defines the spring configuration file to be loaded2@ContextConfiguration (locations= "Classpath:applicationContext.xml")3 Public classaspectjtest{4 @Autowired5 PrivateIuserservice UserService;6 @Test7 Public voidtest1 () {8 //The notification that corresponds to the aspectj definition in aop1.xml.9 Userservice.add ();Ten } One}

Notification types defined by the ASPECTJ framework in spring

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.