Guice bindInterceptor的使用
来源:互联网 发布:centos mrtg安装 编辑:程序博客网 时间:2024/06/16 02:03
package com.ilucky.guice.test12;import com.google.inject.Guice;import com.google.inject.Injector;import com.google.inject.name.Named;/** * v1.0:20161116 * bindInterceptor的使用 * aop:面向切面编程. */public class MainTest { public static void main(String[] args) { Injector injector = Guice.createInjector(new PersonModule()); Person student = injector.getInstance(Person.class); student.say("say......"); System.out.println("----------------------------------------"); student.say2("say2......"); System.out.println("----------------------------------------"); student.dos("do......"); System.out.println("----------------------------------------"); Test test = injector.getInstance(Test.class); test.dos("do......"); }}class Test { @Named("dos") public void dos(String dos) { System.out.println("Test do=" + dos); }}/**我是在方法之前要做的事情Student say=say......我是在方法执行之后做的事情----------------------------------------我是在方法之前要做的事情Student say2=say2......我是在方法执行之后做的事情----------------------------------------我是在方法之前要做的事情Student do=do......我是在方法执行之后做的事情----------------------------------------我是在方法之前要做的事情Test do=do......我是在方法执行之后做的事情*/
package com.ilucky.guice.test12;import com.google.inject.Binder;import com.google.inject.Module;import com.google.inject.matcher.Matchers;import com.google.inject.name.Names;/** * 这里视为某些方法绑定拦截器. */public class PersonModule implements Module { public void configure(Binder binder) { binder.bind(Person.class).to(Student.class); // 测试如果注释掉如下代码. binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(Names.named("say")), new MyMethodInterceptor()) ; binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(Names.named("dos")), new MyMethodInterceptor()) ; }}
package com.ilucky.guice.test12;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class MyMethodInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { doBefore(); Object ret = invocation.proceed(); doAfter(); return ret; } public void doBefore() { System.out.println("我是在方法之前要做的事情"); } public void doAfter() { System.out.println("我是在方法执行之后做的事情"); }}
package com.ilucky.guice.test12;public interface Person { public void say(String say); public void say2(String say); public void dos(String dos);}
package com.ilucky.guice.test12;import com.google.inject.name.Named;public class Student implements Person { @Override @Named("say") public void say(String say) { System.out.println("Student say=" + say); } @Override @Named("say") public void say2(String say) { System.out.println("Student say2=" + say); } @Override @Named("dos") public void dos(String dos) { System.out.println("Student do=" + dos); }}
阅读全文
1 0
- Guice bindInterceptor的使用
- Guice的使用例子
- guice的使用例子
- guice框架的入门使用
- Google Guice使用入门
- guice使用初体验
- Google Guice 入门使用
- google guice使用
- Guice的scope
- spring&guice 的AOP
- guice
- Guice
- Guice
- Google的IOC框架: GUICE
- spring与guice的区别
- Spring和Guice的对比
- google的guice入门详解
- Guice与Spring的区别
- Mybatis 批量插入万条数据 !
- 笔记----Ajax的get与post
- android studio项目目录详解
- 用户、角色和权限,多表查询
- 数据库中存储日期的字段类型到底应该用varchar还是datetime
- Guice bindInterceptor的使用
- Mac下使用svn命令
- 排序之冒泡排序
- 使用Saiku+Kylin构建多维分析OLAP平台
- Unable to handle kernel NULL pointer dereference at virtual address 00000000
- TCP的7种定时器
- Hololens开发手记入门学习阶段总结
- 使用JQuery中的trim()方法去掉前后空格
- QT:带文件查找的文本编辑器