当前位置: 首页 > 后端技术 > Java

GuiceAOP(进阶版)

时间:2023-04-02 10:31:20 Java

本教程主要详细讲解Guice的一些AOP方法。通过这个简单的教程,我们可以快速使用Guice进行AOP开发。稍后,我们将详细解释Guice中的AOP。基础环境技术版本Java1.8+Guice4.2.3初始化项目初始化项目mvnarchetype:generate-DgroupId=io.edurt.lc.guice-DartifactId=guice-aop-senior-DarchetypeArtifactId=maven-archetype-quickstart-Dversion=1.0。0-DinteractiveMode=false修改pom.xml添加Guice依赖lc-guiceio.edurt.lc.guice1.0.04.0.0guice-aop-seniorGuiceAOP(高级)学习中心1.81.8com.google.injectguice4.2.3junitjunit4.12test初始化服务首先,我们定义服务Service,它有一个简单的方法println。在src/main/java目录下新建io.edurt.lc.guice.GuiceAopSeniorService类文件,在文件包io.edurt.lc.guice中输入以下内容;importcom.google.inject.ImplementedBy;importorg.aopalliance.intercept.MethodInvocation;@ImplementedBy(value=GuiceAopSeniorServiceImpl.class)publicinterfaceGuiceAopSeniorService{voidbefore(MethodInvocationinvocation);}在src/main/java目录下新建io.edurt.lc.guice.GuiceAopSeniorServiceImpl类文件,在文件包io.edurt.lc.guice;导入org.aopalliance.intercept.MethodInvocation;公共类GuiceAopSeniorServiceImpl实现GuiceAopSeniorService{@Overridepublicvoidbefore(MethodInvocationinvocation){System.out.println(String.format("Beforemethod[%s]",invocation.getMethod().getName()));}}AOP注入依赖Guice允许在AOP关联之前将AOP依赖注入到容器中!在src/main/java目录下新建io.edurt.lc.guice.GuiceAopInjectionMethodInterceptor类文件,在文件中导入如下内容包io.edurt.lc.guice;importcom.google.inject.Inject;importorg.aopalliance.intercept.MethodInterceptor;导入org.aopalliance.intercept.MethodInvocation;公共类GuiceAopInjectionMethodInterceptor实现MethodInterceptor{@InjectServiceAopallianceGuice服务;@Override公共对象调用(MethodInvlocationinvocation)throwsThrowable{service.before(invocation);对象响应;尝试{response=invocation.proceed();}finally{System.out.println(String.format("After[%s]",invocation.getMethod().getName()));}返回响应;}}在src/main/java目录下新建io.edurt.lc.guice.PrintlnService类文件,在文件包io.edurt.lc.guice中写入如下内容;importcom.google.inject.ImplementedBy;@ImplementedBy(value=PrintlnServiceImpl.class)publicinterfacePrintlnService{voidprintln(Stringinput);}在src/main/中创建一个新的io.edurt.lc.guice.PrintlnServiceImpl类文件java目录,在文件包io.edurt.lc.guice中输入以下内容;importcom.google.inject.name.Named;publicclassPrintlnServiceImplimplementsPrintlnService{@Override@Named(value="println")publicvoidprintln(字符串输入){System.out.println(input);}}PrintlnService和PrintlnServiceImpl用于测试新的服务输出。接下来,在src/test/java目录下创建io.edurt.lc.guice.TestGuiceAopSenior类文件来测试定义的服务,添加如下代码包io.edurt.lc.guice;importcom.google.inject.Guice;importcom.google.inject.Inject;importcom.google。inject.Injector;导入com.google.inject.matcher.Matchers;导入com.google.inject.name.Names;导入org.junit.Test;公共类TestGuiceAopSenior{@InjectprivatePrintlnServiceprintlnService;@Testpublicvoidtest(){Injectorinjector=Guice.createInjector(binder->{GuiceAopInjectionMethodInterceptorinjectionMethodInterceptor=newGuiceAopInjectionMethodInterceptor();binder.requestInjection(injectionMethodInterceptor);binder.bindInterceptor(Matchers.any(),Matchers.annotatedWith)("printl"annotatedWith)("printl"annotatedWith)("printl",injectionMethodInterceptor);});TestGuiceAopSeniorapplication=injector.getInstance(TestGuiceAopSenior.class);application.printlnService.println("HelloGuiceAOP");}}我们运行程序输出Before方法[println]HelloGuiceAOPAfter[println]注:binder.requestInjection(injectionMethodInterceptor);此代码用于注入自定义AOP服务binder.bindInterceptor(Matchers.any(),Matchers.annotatedWith(Names.named("println")),injectionMethodInterceptor);这里的第二个参数不能使用Matchers.any(),否则会死循环,因为容器会不断的进行aop操作源码地址github

最新推荐
猜你喜欢