Spring总结-AOP

    技术2022-05-19  27

    内容主要来自林信良的《Spring2.0技术手册》

    简介:

    AOP全名:Aspect-oriented programming

    Cross-cutting concerns:在一个服务的流程中插入与业务逻辑无关的系统服务逻辑

    Aspect:将Cross-cutting concerns独立出来设计为一个对象,这样的特殊对象称之为Aspect。

    1 动态代理:

    处理类必须实现java.lang.reflect.InvocationHandler接口

    说明:

    定义所要代理的接口:

    public  interface IHello{

       public void hello(String name);

    }

    业务逻辑类HelloSpeaker实现接口

    public class HelloSpeaker implements IHello{

       public void hello(String name){

           System.out.println("Hello, "+name);

       }

    }

    测试程序,使用LogHandler的bind()方法来绑定被代理对象,如下所示:

    public class ProxyDemo{

         public static void main(String[] args){

                 LogHandler logHandler = new LogHandler();

                 IHello helloProxy = (IHello) logHandler.bind(new HelloSpeaker());

                 helloProxy.hello("Justin");

         }

    }

    使用代理对象将日志等与业务逻辑无关的动作或任务提取出来,设计成一个服务对象。像之前范例中的HelloProxy或LogHandler这样的对象称之为切面(Aspect)

    2 AOP概念术语

    Cross-cutting concern:将原先与业务逻辑无关的动作(如日志等)安插到业务的处理流程之中,这些动作被称为Cross-cutting concern

    Aspect:

    使用代理对象将日志等与业务逻辑无关的动作或任务提取出来,设计成一个独立可重用的服务对象。像之前范例中的HelloProxy或LogHandler这样的对象称之为切面(Aspect)

    Advice:

    Aspect中对Cross-cutting concern的具体实现称之为Advice,如LogHandler中的invoke()方法

    Jointpoint:

    Advice在应用程序执行时加入业务流程的点或时机,具体来说就是Advice在程序中的执行时机。

    Pointcut:

    可定义感兴趣的Jointpoint,当调用方法符合Pointcut表示式时,将Advice织入应用程序

    Target:

    一个Advice被应用的对象或目标

    Introduction:

    Weave:

    Advice被应用至对象之上的过程称之为织入。

    3 Advice的例子

    public interface IHello {  public void hello(String name);

    }

    public class HelloSpeaker implements IHello {

     public void hello(String name) {   // TODO Auto-generated method stub   System.out.println("Hello, " + name);

     }

    }

    public class LogBeforeAdvice implements MethodBeforeAdvice {  private Logger logger = Logger.getLogger(this.getClass().getName());

     public void before(Method method, Object[] args, Object target) throws Throwable {   // TODO Auto-generated method stub   logger.log(Level.INFO,"method starts" +method);

     }

    }

    配置文件:

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">    <bean id="logBeforeAdvice" class="com.yang.aopadvice.LogBeforeAdvice" />    <bean id="helloSpeaker" class="com.yang.aopadvice.HelloSpeaker" />

     <bean id="helloProxy" class="org.springframework.aop.framework.ProxyFactoryBean">   <property name="proxyInterfaces" value="com.yang.aopadvice.IHello" />

     <property name="target" ref="helloSpeaker" />

     <property name="interceptorNames">   <list>    <value>logBeforeAdvice</value>   </list>  </property>  </bean> </beans>

    测试程序:

    public class SpringAOPDemo {  public static void main(String[] args){   ApplicationContext context = new ClassPathXmlApplicationContext("beans-config.xml");   IHello helloProxy = (IHello) context.getBean("helloProxy");   helloProxy.hello("Justin");  }

    }

    4 Pointcut,Advice

    Pointcut定义了感兴趣的Jointpoint

    4.1 NameMatchMethodPointcutAdvisor

    Package org.springframework.aop下

    配置例子:

    4.2 RegExpMethodPointcutAdvisor

    个人理解作用类似于正则表达式

    4.3 ControlFlowPointcut

    判断执行期是否介入Advisor,提供的是动态Pointcut功能

    5 Introduction

    6 Autoproxing

    自动代理

    总结:

    Proxy is the king of Spring AOP

    Spring AOP 的主要原理就是代理


    最新回复(0)