---
title: "Spring 动态切入点"
author: "wangyun"
date: "2008-07-20T23:36:59Z"
tags: []
slug: "spring"
draft: false
lastmod: "2008-07-20T23:36:59Z"
id: 216757
url: "https://wangyun.iteye.com/blog/216757"
---

由于动态切入点效率十分低下，并且一般并不会使用动态切入点。因此Spring 只提供了一个动态切入点：ControlFlowPointcut 类，它指定了执行aop 的类，即只有该类调用aop 方法时，方法才会动态的织入通知，其他类调用aop 方法和普通的方法调用一样。例子如下：

     1）通知代码

```
public class Before implements MethodBeforeAdvice {

    public void before( Method method, Object[] args, Object target)throws Throwable {
        System.out.println( "");
        System.out.println( "Before.before()");
        System.out.println( "target: " + target.toString());
        System.out.println( "method name: " + method.getName());
        Type[] type = method.getGenericParameterTypes();
        for (int i = 0; i < type.length; i++) {
            System.out.println( type[i].toString() + ": " + args[i]);
        }
        System.out.println( "--end--");
        System.out.println( "");
    }  
}
```

     2）ControlFlowPointcut 的指定类

```
public class Argument implements ApplicationContextAware {

    private Implement impl;

    public void setApplicationContext(ApplicationContext context)throws BeansException  {
        impl = (Implement)context.getBean( "aop");
    }

    public void test() {
        System.out.println( "Argument.test()");
        impl.test();
    }

    public void test2(Implement i) {
        i.test();   
    }
}
```

     3）目标对象

```
public class Target implements Implement {

    public void test() {
        System.out.println( "Target.test()");
    }
}
```

     4）接口定义

```
public interface Implement {

    void test();
}
```

     5）配置文件

```
<beans>
    <bean id="arg" class="spring.Argument" />

    <bean id="advice" class="org.springframework.aop.support.DefaultPointcutAdvisor">
        <property name="advice">
            <bean class="spring.Before" />
        </property>
        <property name="pointcut">
            <bean class="org.springframework.aop.support.ControlFlowPointcut">
                <constructor-arg>
                    <value>spring.Argument</value>
                </constructor-arg>
            </bean>
        </property>
    </bean>

    <bean id="aop" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyInterfaces">
            <list>
                <value>spring.Implement</value>
            </list>
        </property>
        <property name="interceptorNames">
            <list>
                <value>advice</value>
            </list>
        </property>
        <property name="target">
            <bean class="spring.Target" />
        </property>
    </bean>
</beans>
```

     6）测试代码

```
public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext( "spring.xml");
        Argument arg = (Argument)context.getBean( "arg");
        arg.test();
        System.out.println( "----------------");
        Implement impl = (Implement)context.getBean( "aop");
        impl.test();
        System.out.println( "----------------");
        arg.test2( impl);
    }
```