Guice的AOP还是非常弱的。眼下只支持方法级别上的,另外灵活性也不是非常高。

看例如以下演示样例:

Guice支持AOP的条件是:

  1. 类必须是public或者package (default)
  2. 类不能是final类型的
  3. 方法必须是public,package或者protected
  4. 方法不能使final类型的
  5. 实例必须通过Guice@Inject注入或者有一个无參数的构造函数

且看演示样例代码

1、定义接口

  1. package com.guice.AOP;
  2. import com.google.inject.ImplementedBy;
  3. @ImplementedBy(ServiceImpl.class)
  4. public interface Service {
  5. public void sayHello();
  6. }

2、定义实现类

  1. package com.guice.AOP;
  2. import com.google.inject.Singleton;
  3. import com.google.inject.name.Named;
  4. @Singleton
  5. public class ServiceImpl implements Service {
  6. @Named("log")
  7. @Override
  8. public void sayHello() {
  9. System.out.println(String.format("[%s#%d] execute %s at %d", this.getClass().getSimpleName(), hashCode(), "sayHello", System.nanoTime()));
  10. }
  11. }

3、定义切面

  1. package com.guice.AOP;
  2. import org.aopalliance.intercept.MethodInterceptor;
  3. import org.aopalliance.intercept.MethodInvocation;
  4. /**
  5. * TODO :自己定义的方法拦截器。用于输出方法的运行时间
  6. *
  7. * @author E468380
  8. */
  9. public class LoggerMethodInterceptor implements MethodInterceptor {
  10. @Override
  11. public Object invoke(MethodInvocation invocation) throws Throwable {
  12. String name = invocation.getMethod().getName();
  13. long startTime = System.nanoTime();
  14. System.out.println(String.format("before method[%s] at %s", name, startTime));
  15. Object obj = null;
  16. try {
  17. obj = invocation.proceed();// 运行服务
  18. } finally {
  19. long endTime = System.nanoTime();
  20. System.out.println(String.format("after method[%s] at %s, cost(ns):%d", name, endTime, (endTime - startTime)));
  21. }
  22. return obj;
  23. }
  24. }

4、測试类

  1. package com.guice.AOP;
  2. import com.google.inject.Binder;
  3. import com.google.inject.Guice;
  4. import com.google.inject.Inject;
  5. import com.google.inject.Injector;
  6. import com.google.inject.Module;
  7. import com.google.inject.matcher.Matchers;
  8. import com.google.inject.name.Names;
  9. public class AopTest {
  10. @Inject
  11. private Service service;
  12. public static void main(String[] args) {
  13. Injector injector = Guice.createInjector(new Module() {
  14. @Override
  15. public void configure(Binder binder) {
  16. binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(Names.named("log")), new LoggerMethodInterceptor());
  17. }
  18. });
  19. injector.getInstance(AopTest.class).service.sayHello();
  20. injector.getInstance(AopTest.class).service.sayHello();
  21. injector.getInstance(AopTest.class).service.sayHello();
  22. }
  23. }

输出结果:

  1. before method[sayHello] at 18832801981960
  2. [ServiceImpl$$EnhancerByGuice$$d4244950#1109685565] execute sayHello at 18832817170768
  3. after method[sayHello] at 18832817378285, cost(ns):15396325
  4. before method[sayHello] at 18832817542181
  5. [ServiceImpl$$EnhancerByGuice$$d4244950#1109685565] execute sayHello at 18832817640327
  6. after method[sayHello] at 18832817781772, cost(ns):239591
  7. before method[sayHello] at 18832817920651
  8. [ServiceImpl$$EnhancerByGuice$$d4244950#1109685565] execute sayHello at 18832818013023
  9. after method[sayHello] at 18832818132657, cost(ns):212006

关于此结果有几点说明:

(1)因为使用了AOP我们的服务得到的不再是我们写的服务实现类了,而是一个继承的子类,这个子类应该是在内存中完毕的。

(2)除了第一次调用比較耗时外(可能guice内部做了比較多的处理)。其他调用事件为0毫秒(我们的服务本身也没做什么事)。

(3)确实完毕了我们期待的AOP功能。

5、切面注入依赖

假设一个切面(拦截器)也须要注入一些依赖怎么办?

在这里我们声明一个前置服务。输出全部调用的方法名称。

①定义接口

  1. package com.guice.AOP;
  2. import org.aopalliance.intercept.MethodInvocation;
  3. import com.google.inject.ImplementedBy;
  4. @ImplementedBy(BeforeServiceImpl.class)
  5. public interface BeforeService {
  6. void before(MethodInvocation invocation);
  7. }

②定义实现类

  1. package com.guice.AOP;
  2. import org.aopalliance.intercept.MethodInvocation;
  3. public class BeforeServiceImpl implements BeforeService {
  4. @Override
  5. public void before(MethodInvocation invocation) {
  6. System.out.println("Before--->" + invocation.getClass().getName());
  7. }
  8. }

③定义切面

  1. package com.guice.AOP;
  2. import org.aopalliance.intercept.MethodInterceptor;
  3. import org.aopalliance.intercept.MethodInvocation;
  4. import com.google.inject.Inject;
  5. //这个切面依赖前置服务
  6. public class AfterMethodIntercepter implements MethodInterceptor {
  7. @Inject
  8. private BeforeService beforeService;
  9. @Override
  10. public Object invoke(MethodInvocation invocation) throws Throwable {
  11. beforeService.before(invocation);
  12. Object obj = null;
  13. try {
  14. obj = invocation.proceed();
  15. } finally {
  16. System.out.println("after--->" + invocation.getClass().getName());
  17. }
  18. return obj;
  19. }
  20. }

④编写測试类

  1. package com.guice.AOP;
  2. import com.google.inject.Binder;
  3. import com.google.inject.Guice;
  4. import com.google.inject.Inject;
  5. import com.google.inject.Injector;
  6. import com.google.inject.Module;
  7. import com.google.inject.matcher.Matchers;
  8. import com.google.inject.name.Names;
  9. public class AopTest2 {
  10. @Inject
  11. private Service service;
  12. public static void main(String[] args) {
  13. Injector injector = Guice.createInjector(new Module() {
  14. @Override
  15. public void configure(Binder binder) {
  16. AfterMethodIntercepter after = new AfterMethodIntercepter();
  17. binder.requestInjection(after);
  18. binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(Names.named("log")), after);
  19. }
  20. });
  21. injector.getInstance(AopTest2.class).service.sayHello();
  22. }
  23. }

输出结果:

  1. Before--->com.google.inject.internal.InterceptorStackCallback$InterceptedMethodInvocation
  2. [ServiceImpl$$EnhancerByGuice$$618294e9#506575947] execute sayHello at 20140444543338
  3. after--->com.google.inject.internal.InterceptorStackCallback$InterceptedMethodInvocation

说明

Binder绑定一个切面的API是:

  1. com.google.inject.Binder.bindInterceptor(Matcher<? super Class<?
  2. >>, Matcher<? super Method>, MethodInterceptor...)

第一个參数是匹配类,第二个參数是匹配方法,第三个数组參数是方法拦截器。也就是说眼下为止Guice只能拦截到方法,然后才做一些切面工作。

注意

虽然切面同意注入其依赖,可是这里须要注意的是。假设切面依赖仍然走切面的话那么程序就陷入了死循环,非常久就会堆溢出。

Guice的AOP还是非常弱的,眼下只支持方法级别上的。另外灵活性也不是非常高。

Guice 学习(八)AOP (面向切面的编程)的更多相关文章

  1. 学习笔记: AOP面向切面编程和C#多种实现

    AOP:面向切面编程   编程思想 OOP:一切皆对象,对象交互组成功能,功能叠加组成模块,模块叠加组成系统      类--砖头     系统--房子      类--细胞     系统--人    ...

  2. AOP 面向切面的编程

    一.面向切面的编程需求的产生 代码混乱:越来越多的非业务需求(日志和验证等)加入后,原有的业务方法急剧膨胀.每个方法在处理核心逻辑的同时还必须兼顾其他多个关注点. 代码分散: 以日志需求为例,只是为了 ...

  3. spring框架学习(三)——AOP( 面向切面编程)

    AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...

  4. 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之十 || AOP面向切面编程浅解析:简单日志记录 + 服务切面缓存

    代码已上传Github+Gitee,文末有地址 上回<从壹开始前后端分离[ .NET Core2.0 Api + Vue 2.0 + AOP + 分布式]框架之九 || 依赖注入IoC学习 + ...

  5. java aop面向切面编程

    最近一直在学java的spring boot,一直没有弄明白aop面向切面编程是什么意思.看到一篇文章写得很清楚,终于弄明白了,原来跟python的装饰器一样的效果.http://www.cnblog ...

  6. 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~

    简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...

  7. Z从壹开始前后端分离【 .NET Core2.0/3.0 +Vue2.0 】框架之十 || AOP面向切面编程浅解析:简单日志记录 + 服务切面缓存

    本文梯子 本文3.0版本文章 代码已上传Github+Gitee,文末有地址 大神反馈: 零.今天完成的深红色部分 一.AOP 之 实现日志记录(服务层) 1.定义服务接口与实现类 2.在API层中添 ...

  8. aop面向切面编程的实现

    aop主要用于日志记录,跟踪,优化和监控 下面是来自慕课网学习的一些案例,复制黏贴就完事了,注意类和方法的位置 pom添加依赖: <dependency> <groupId>o ...

  9. AOP 面向切面编程, Attribute在项目中的应用

    一.AOP(面向切面编程)简介 在我们平时的开发中,我们一般都是面对对象编程,面向对象的特点是继承.多态和封装,我们的业务逻辑代码主要是写在这一个个的类中,但我们在实现业务的同时,难免也到多个重复的操 ...

随机推荐

  1. c++类流操作运算符的重定义

    对于流操作运算符我们需要注意的是函数的返回类型应该是流输入类型的引用或者流输出类型的引用,因为如果代码是 cout<<a<<b; 我们对a执行完cout函数之后,我们应该再次将 ...

  2. vue 指令---气泡提示(手撸实战)

    菜鸟学习之路//L6zt github 自己在造组件轮子,也就是瞎搞.自己写了个slider组件,想加个气泡提示.为了复用和省事特此写了个指令来解决.预览地址项目地址 github 我叫给它胡博 cs ...

  3. spring-mvc jackson配置json为空不输出

    使用的spring-mvc版本是4.1.6,jackson版本是2.1.4 在spring-mvc配置文件中添加以下代码就行 <mvc:annotation-driven> <mvc ...

  4. idea Error:(65, 27) java: 未结束的字符串文字

    今天在使用IDEA的时候,出现了这个错误,原因项目文件编码不一致导致的,解决方法是: 将项目的文件编码全改成一致(UTF-8),如下图所示:

  5. 高可用技术之keepalived原理简单了解

    Keepalived 工作原理 keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议. 虚拟路由冗余协 ...

  6. 【BZOJ 3555】 [Ctsc2014]企鹅QQ(哈希)

    Description PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即时通讯.相册.集市等丰富强大 ...

  7. POJ 3621 Sightseeing Cows (最优比率环 01分数划分)

    题意: 给定L个点, P条边的有向图, 每个点有一个价值, 但只在第一经过获得, 每条边有一个花费, 每次经过都要付出这个花费, 在图中找出一个环, 使得价值之和/花费之和 最大 分析: 这道题其实并 ...

  8. 一份快速实用的 tcpdump 命令参考手册

    对于 tcpdump 的使用,大部分管理员会分成两类.有一类管理员,他们熟知  tcpdump 和其中的所有标记:另一类管理员,他们仅了解基本的使用方法,剩下事情都要借助参考手册才能完成.出现这种情况 ...

  9. 儿子写日记 : " 夜深了,妈妈在打麻将,爸爸在上网……"

    儿子写日记 : " 夜深了,妈妈在打麻将,爸爸在上网……"              爸爸检查时,很不满意地说 : " 日记源于生活,但要高于生活 !"    ...

  10. zoj 2201 No Brainer

    No Brainer Time Limit: 2 Seconds      Memory Limit: 65536 KB Zombies love to eat brains. Yum. Input ...