Spring 动态切入点

     由于动态切入点效率十分低下,并且一般并不会使用动态切入点。因此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);
    }

 

 

    

 

   

 

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值