Spring.AOP下的 hello world !
2017-10-17 17:46
513 查看
此方法只做演示,项目中不使用
项目中用annotation
此代码输出 "World"
如何使用AOP,在松耦合情况下输出 hello world !
以下代码实现:
打印出:
项目中用annotation
此代码输出 "World"
package com.test532; public class MessageWriter { public void writeMessage(){ System.out.print("World"); } }
如何使用AOP,在松耦合情况下输出 hello world !
以下代码实现:
package com.test532; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; //此类实现包围通知 MethodInterceptor public class MethodDecorator implements MethodInterceptor{ @Override public Object invoke(MethodInvocation invocation) throws Throwable { System.out.print("Hello "); Object retVal = invocation.proceed(); System.out.print(" !"); return retVal; } }
public class HelloWorldWriter { public static void main(String[] args) { MessageWriter writer = new MessageWriter(); //使用ProxyFactory是完全编程式的做法,一般我们不需要这么做, //可以依靠ProxyFactoryBean类来声明式地创建代理 ProxyFactory factory = new ProxyFactory();//创建代理工厂 factory.addAdvice(new MethodDecorator());//织入通知 factory.setTarget(writer);//设定织入的目标对象 MessageWriter proxy = (MessageWriter)factory.getProxy(); //获得代理 proxy.writeMessage(); } }
打印出:
Hello World !
相关文章推荐
- spring & aop- hello world (一)
- Spring AOP之Hello World
- Spring AOP之Hello World
- Spring AOP之Hello World
- Spring AOP 的 Hello World
- Spring AOP之Hello World
- [教程]spring AOP详解
- Spring AOP之 java 动态代理(Proxy 与 InvocationHandler)理解
- SpringBoot中使用AOP打印接口日志的方法
- Spring-AOP-通知-异常通知
- Spring-Aop详细教程
- 【第六章】 AOP 之 6.5 AspectJ切入点语法详解 ——跟我学spring3
- Spring AOP Example – Advice
- spring-boot使用AOP统一处理日志
- spring-AOP原理与应用
- Spring AOP编程-aspectJ注解开发
- 跟开振学习Spring AOP第三篇:为什么要用AOP
- Spring入门笔记(二)AOP及AspectJ
- Spring-AOP
- 一个简单的spring AOP例子