Spring学习(七)——增强类

Spring 切点

什么是切点?切点(Pointcut),每个程序类都拥有多个连接点,如一个拥有两个方法的类,这两个方法都是连接点,即连接点是程序类中客观存在的事物。但在这为数从多的连接点中,如何定位到某个感兴趣的连接点上呢?AOP通过"切点"定位特定连接点。通过数据库查询的概念来理解切点和连接点的关系再适合不过了;连接点相当于数据库中的记录,而切点相当于查询条件.
       在Spring中,切点通过org.springframework.aop.Pointcut接口进行描述,它使用类和方法作为连接点的查询条 件,Spring AOP的规则解析引擎负责解析切点所设定的查询条件,找到对应的连接点---其实确切地说,应该是执行点而非连接点,因为连接点是方法执行前、执行后等包 括方位信息的具体程序执行点,而切点只定位到某个方法上,所以如果希望定位到具体连接点上,还需要提供方位信息。

 

 

aop术语:

 

1、切面:所有切入点的集合

 

2、切入点:一组符合某种规则的连接点

 

3、连接点:狭义上通俗的讲指的是某个方法

 

4、通知:在某个连接点上的某种操作,该操作并非连接点中的操作,而是外来的操作。

 

5、引入(Introduction):引入(在AspectJ中被称为inter-type声明)使得一个切面可以定义被通知对象实现给定的接口, 并且可以为那些对象提供具体的实现

 

spring aop 增强类型支持5种:
  • 前置增强
           org.springframework.aop.BeforeAdvice  代表前置增强,因为spring只支持方法级的增强,所以MethodBeforeAdvice 是目前可用前置增强,表示在目标方法执行前实施增强。
  • 后置增强
           org.springframework.aop.AfterAdvice 代表后增强,表示目标方法在执行后实施增强 
  • 环绕增强
            org.springframework.aop.MethodInterceptor 代表环绕增强,表示目标方法执行前后实施增强
  • 异常抛出增强
            org.springframework.aop.ThrowsAdvice 代表抛出异常增强,表示目标方法抛出异常后实施增强
  • 引介增强
            org.springframework.aop.IntroductionInterceptor 代表引介增强,表示在目标类中添加一些新的方法和属性
 
前置增强,后置增强
 
服务员接口 Waiter.java

package com.paic.zhangqi.spring.aop;
 
public interface Waiter {
    void greetTo(String name);
    void serveTo(String name);
}
 
服务员接口实现类 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+...);
    }
}

 


前置增强类 GreetingBeforeAdvice.java 在目标类方法执行前执行

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+.);
    }
}

后置增强类GreetingAfterAdvice.java 在目标类方法调用后执行

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!);
    }
}

测试类 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();
         
        // spring 提供的代理工厂
        ProxyFactory pf = new ProxyFactory();
         
        // 设置代理目标
        pf.setTarget(target);
         
        // 为代理目标添加增强
        pf.addAdvice(beforeAdvice);
        pf.addAdvice(afterAdvice);
         
        // 生成代理实例
        Waiter proxy = (Waiter)pf.getProxy();
         
        proxy.greetTo(John);
        proxy.serveTo(Tomcat);
 
    }
}
输出结果
How are you!Mr.John.
greet to John...
Please enjoy yourself!
How are you!Mr.Tomcat.
serving Tomcat...
Please enjoy yourself!

使用配置文件进行配置 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>

对应的测试类 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);
    }
}

同样得到输出

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

 

posted @ 2015-12-24 15:46  ChrisMurphy  阅读(1957)  评论(0编辑  收藏  举报