美文网首页
5.3 AOP的Hello World示例

5.3 AOP的Hello World示例

作者: 仙境源地 | 来源:发表于2019-11-04 15:38 被阅读0次

AOP Alliance:为AOP实现定义了一组标准接口

MethodInterceptor是AOP Alliance标准接口之一
简单实现环绕通知效果

源码chapter05/aop-hello-world

package com.apress.prospring5.ch5;

public class Agent {
    public void speak() {
        System.out.print("Bond");
    }
}
package com.apress.prospring5.ch5;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class AgentDecorator implements MethodInterceptor {
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.print("James ");

        Object retVal = invocation.proceed();

        System.out.println("!");
        return retVal;
    }
}
package com.apress.prospring5.ch5;

import org.springframework.aop.framework.ProxyFactory;

public class AgentAOPDemo {
    public static void main(String... args) {
        Agent target = new Agent();

        ProxyFactory pf = new ProxyFactory();
        pf.addAdvice(new AgentDecorator());
        pf.setTarget(target);

        Agent proxy = (Agent) pf.getProxy();

        target.speak();
        System.out.println("");
        proxy.speak();
    }
}

执行结果

Bond
James Bond!

相关文章

  • 5.3 AOP的Hello World示例

    AOP Alliance:为AOP实现定义了一组标准接口 MethodInterceptor是AOP Allian...

  • react native 基础知识点

    Hello World react native 官方Hello world示例 我们从import开始来看这个程...

  • 4 替换空格

    题目 把字符串中出现的空格替换为“%20”。示例“Hello World” 变为“Hello%20World" 结...

  • 常用markdown语法

    Hello World! Hello World! Hello World! Hello World! Hello...

  • Vue:2-2 hello World

    一、 通过