Various notifications in Spring

Source: Internet
Author: User

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"4 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"5 xsi:schemalocation= "Http://www.springframework.org/schema/beans6 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd7 HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP8 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">9 <!-- Ten 1. Introduction of AOP namespaces One xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" A HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP - http://www.springframework.org/schema/aop/spring-aop-2.5.xsd - 2. Target class the 3. Facets - 4. The interceptor is implemented within spring - 5, the configuration of AOP -       - + <BeanID= "Persondao"class= "Cn.test.aop.PersonDaoImpl"></Bean> - <BeanID= "Transaction"class= "Cn.test.aop.Transaction"></Bean> +  A <!--AOP Configuration - at <Aop:config> -     <!--pointcut Expression - expression - determine which class can generate a proxy object - ID Unique identification - -     <Aop:pointcutexpression= "Execution (* cn.test.aop.persondaoimpl.* (..))"ID= "perform"/> in     <!--Facets - -     <Aop:aspectref= "Transaction"> to         <!--front-facing notifications + * Before the target method is executed -                - the         <Aop:beforeMethod= "BeginTransaction"Pointcut-ref= "perform"/> *         <!--Post Notification $ * After the target method is executedPanax Notoginseng * The return value of the target method can be obtained according to returning - * If the target method encounters an exception, the notification does not execute the                - +         <aop:after-returningMethod= "Commit"Pointcut-ref= "perform"returning= "Val"/> A         <!--pre-and post-notifications can only add content to the target method, but cannot control the execution of the target method - the         <!--  + Final Notice - * After the target method is executed $ * Regardless of whether the target method encounters an exception, execute $ * Often do some close resources -          -          -         <Aop:afterMethod= "Finallymethod"Pointcut-ref= "perform"/>  the         <!--  - Exception NotificationWuyi The goal is to get the exception thrown by the target method. the          - -         <aop:after-throwingMethod= "Exceptionmethod"throwing= "Ex"Pointcut-ref= "perform"/> Wu          -     </Aop:aspect> About </Aop:config> $  -  - </Beans>
  Transaction.java
1 Public class Transaction {2 //joinpoint connection Point information3 Public void BeginTransaction (Joinpoint joinpoint) {4 Joinpoint.getargs ();//Gets the parameter of the method5 String methodName = Joinpoint.getsignature (). GetName ();6 System.out.println (methodName);7 System.err.println ("Begin trans Action");8 }9 Public Void Commit (Joinpoint joinpoint,object val) {Ten System.err.println ("commit"); One } A - Public void Finallymethod () { - System.err.println ("finally Method"); the } - - Public void Exceptionmethod (Throwable ex) { - System.err.println (Ex.getmessage ()); + } - + /** A * Surround Notifications at * Ability to control the execution of target methods - */ - - Public void Aroundmethod (Proceedingjoinpoint joinpoint) { - String methodname=joinpoint.getsignature (). GetName (); - if ("Addperson". Equals (MethodName)) { in - } to}

Test:

1 /**2 * Principle3 * * Load config file, start spring container4 * * Spring container creates objects for bean5 * * Parsing the configuration of AOP, parsing pointcut expressions6 * * See the class that incorporates spring management and pointcut expression matches, and if it does, creates a proxy object for that class7 * * Proxy object Method body formation is the target method + notification8 * * When the client is in Context.getbean, if the bean has a proxy object, the proxy object is returned and the original object is returned if there is no proxy object9 * Description:Ten * If the target class implements an interface, the spring container takes jdkproxy, and if the target class does not implement an interface, the Spring container takes One * Cglibproxy A      *       -      *      */ - @Test the      Public voidDosome () { -          -ApplicationContext applicationcontext=NewClasspathxmlapplicationcontext ("Cn/test/aop/applicationcontext.xml"); -Persondao persondao= (Persondao) Applicationcontext.getbean ("Persondao"); +  - Persondao.addperson (); +          A}

Various notifications 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.