Spring Learning (7)-enhancement class and spring Learning Enhancement class

Source: Internet
Author: User

Spring Learning (7)-enhancement class and spring Learning Enhancement class

Spring cut point

What is a cut point? Pointcut, each program class has multiple connection points, for example, a class with two methods, both methods are connection points, that is, the connection points are objective things in the program class. But how can we locate a certain number of connection points from multiple connection points? AOP locates a specific connection point through a "cut point. The concept of database query can be used to understand the relationship between the cut point and the connection point. The connection point is equivalent to the record in the database, and the cut point is equivalent to the query condition.

  • Pre-enhancement
  • Org. springframework. aop. BeforeAdvice indicates the pre-enhancement. Because spring only supports method-level enhancement, MethodBeforeAdvice is currently available and indicates that the pre-enhancement is implemented before the target method is executed.
    • Post Enhancement
    Org. springframework. aop. AfterAdvice indicates post-enhancement, indicating that the target method is enhanced after execution.
    • Surround Enhancement
    Org. springframework. aop. MethodInterceptor indicates enhanced surround, indicating enhanced implementation before and after the target method is executed.
    • Exception throw Enhancement
    Org. springframework. aop. ThrowsAdvice indicates that the exception is thrown and the enhancement is performed after the target method throws an exception.
    • Introduction Enhancement
    Org. springframework. aop. IntroductionInterceptor indicates introduction enhancement, indicating adding some new methods and attributes to the target class. Pre-enhancement and post-enhancementWaiter interface Waiter. java
     
    package 
    com.paic.zhangqi.spring.aop;  
    public 
    interface 
    Waiter { 
         
    void 
    greetTo(String name); 
         
    void 
    serveTo(String name); 
    } 
    
     
    Waiter interface implementation class NaiveWaiter. java
     
    package 
    com.paic.zhangqi.spring.aop;  
    public 
    class 
    NaiveWaiter  
    implements 
    Waiter {  
         
    @Override 
         
    public 
    void 
    greetTo(String name) { 
             
    System.out.println(greet to +name+...); 
         
    }  
         
    @Override 
         
    public 
    void 
    serveTo(String name) { 
             
    System.out.println(serving +name+...); 
         
    } 
    } 
    

     


    package com.paic.zhangqi.spring.aop;  import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice;  public class GreetingBeforeAdvice implements MethodBeforeAdvice {            @Override      public void before(Method method, Object[] args, Object obj) throws Throwable {          String clientName = (String)args[ 0 ];          System.out.println(How are you!Mr.+clientName+.);      } }

    The post-enhancement class GreetingAfterAdvice. java is executed after the target class method is called.

    package com.paic.zhangqi.spring.aop; import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice; public class GreetingAfterAdvice implements AfterReturningAdvice {     @Override    public void afterReturning(Object arg0, Method arg1, Object[] arg2,            Object arg3) throws Throwable {        System.out.println(Please enjoy yourself!);    }}

    Test class TestAdvice. java

    Package com. paic. zhangqi. spring. aop; import org. springframework. aop. afterAdvice; import org. springframework. aop. beforeAdvice; import org. springframework. aop. framework. proxyFactory; public class TestAdvice {public static void main (String [] args) {Waiter target = new NaiveWaiter (); BeforeAdvice beforeAdvice = new GreetingBeforeAdvice (); afterAdvice afterAdvice = new GreetingAfterAdvice (); // The proxy factory ProxyFactory pf provided by spring = new ProxyFactory (); // sets the proxy target pf. setTarget (target); // Add an enhanced pf for the proxy target. addAdvice (beforeAdvice); pf. addAdvice (afterAdvice); // generate the proxy instance Waiter proxy = (Waiter) pf. getProxy (); proxy. greetTo (John); proxy. serveTo (Tomcat );}}
    Output result
    How are you!Mr.John.greet to John...Please enjoy yourself!How are you!Mr.Tomcat.serving Tomcat...Please enjoy yourself!

    Use the configuration file to configure beans. xml

    <beans beans="" http:="" schema="" spring-beans-3.0.xsd="" www.springframework.org="" 
    xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans">      <bean class="com.paic.zhangqi.spring.aop.GreetingBeforeAdvice" id="greetingBefore" />      <bean class="com.paic.zhangqi.spring.aop.GreetingAfterAdvice" id="greetingAfter" />      <bean class="com.paic.zhangqi.spring.aop.NaiveWaiter" id="target" /> </beans>

    Corresponding test class SpringConfigTest. java

    package com.paic.zhangqi.spring.aop; import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringConfigTest {     public static void main(String[] args) {        String configPath = com/paic/zhangqi/spring/aop/beans.xml;        ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);        Waiter waiter = (Waiter) ctx.getBean(waiter);        waiter.greetTo(John);        waiter.serveTo(Tomcat);    }}

    Same output

    How are you!Mr.John.greet to John...Please enjoy yourself!How are you!Mr.Tomcat.serving Tomcat...Please enjoy yourself!

     

    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.