some notes about spring aop
2016-12-04 01:27
288 查看
1 .
2 . 动态代理 by implement InvocationHandler (对接口)
3 . 对类:
@Aspect
ServiceTimeCountAspect:
Spring AOP 会动态选择使用 JDK 动态代理、CGLIB 来生成 AOP 代理,如果目标类实现了接口,Spring AOP 则无需 CGLIB 的支持,直接使用 JDK 提供的 Proxy 和 InvocationHandler 来生成 AOP 代理即可。
timeCountIntecetor implements handlerInterceptor { preHandle(); postHandle(); afterComplete(); }
2 . 动态代理 by implement InvocationHandler (对接口)
class MyProxy implements InvocationHandler { Object obj; public Object bind(Object obj) { this.obj = obj; return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj .getClass().getInterfaces(), this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("I'm proxy!"); Object res = method.invoke(obj, args); return res; } } public class DynamicProxy { public static void main(String[] args) { MyProxy myproxy = new MyProxy(); HoseeDynamicimpl dynamicimpl = new HoseeDynamicimpl(); HoseeDynamic proxy = (HoseeDynamic)myproxy.bind(dynamicimpl); System.out.println(proxy.sayhi()); } }
3 . 对类:
@Aspect
ServiceTimeCountAspect:
@Pointcut("execution(* me.ele.jarch.aries.service.*.*(..))") private void serviceMethod() { } // @Around("me.ele.jarch.aries.aspect.ServiceTimeCountAspect.serviceMethod()") // @Around("serviceMethod() || repositoryMethod()") @Around("serviceMethod()") public Object logServiceMethodRunningTime(ProceedingJoinPoint pjp) throws Throwable { // start stopwatch StopWatch watch = new StopWatch(); watch.start(); Object retVal = pjp.proceed(); // stop stopwatch watch.stop(); Long time = watch.getTotalTimeMillis(); String methodName = pjp.getSignature().getName(); logger.info("service method: {} time count : {}", methodName, time); return retVal; }
Spring AOP 会动态选择使用 JDK 动态代理、CGLIB 来生成 AOP 代理,如果目标类实现了接口,Spring AOP 则无需 CGLIB 的支持,直接使用 JDK 提供的 Proxy 和 InvocationHandler 来生成 AOP 代理即可。
相关文章推荐
- Some Notes About Design Pattern
- some notes about ADDM and AWR
- Some notes about the time
- Some notes about performSelector method
- Some notes about the SAP 4.6C running on HP-UX
- Some notes about CRM Roles Concept in Web UI
- Some Notes About Modules
- Some Notes About JMM and JAVA String & Constants Pool
- Some notes about English Language
- notes about crosstool,uboot,mysql and some tools in linux(ubuntu)
- SpringIOC Aop
- spring的AOP配置
- Spring AOP介绍及源码分析
- springboot【19】日志管理之使用AOP统一处理Web请求日志
- spring的AOP术语
- Java利用动态代理模拟实现Spring的AOP(面向切面编程)功能的小实践
- 什么是spring的IOC AOP?
- Spring中的IOC和AOP
- Spring aop Cannot subclass final class class com.sun.proxy
- 【Spring AOP 详解】