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

GuiceAOP(匹配器)

时间:2023-04-01 17:38:36 Java

本教程主要详细讲解Guice的一些AOP方法。通过这个简单的教程,我们可以快速使用Guice进行AOP开发。稍后,我们将详细解释Guice中的AOP。基础环境技术版本Java1.8+Guice4.2.3初始化项目初始化项目mvnarchetype:generate-DgroupId=io.edurt.lc.guice-DartifactId=guice-aop-matcher-DarchetypeArtifactId=maven-archetype-quickstart-Dversion=1.0。0-DinteractiveMode=false修改pom.xml添加Guice依赖lc-guiceio.edurt.lc.guice1.0.04.0.0guice-aop-matcherGuiceAOP(Matcher)学习中心1.81.8com.google.injectguice4.2.3junitjunit4.12test初始化服务首先,我们定义服务Service,它有一个简单的方法println。在src/main/java目录下新建io.edurt.lc.guice.GuiceAopMatcherService类文件,在文件包io.edurt.lc.guice中输入以下内容;importcom.google.inject.ImplementedBy;@ImplementedBy(value=GuiceAopMatcherServiceImpl.class)公共接口GuiceAopMatcherService{voidprintln(Stringinput);}在src/main/java目录下新建io.edurt.lc.guice.GuiceAopMatcherServiceImpl类文件,在文件包io.edurt.lc.guice中输入如下内容;公共类GuiceAopMatcherServiceImpl实现GuiceAopMatcherService{@Overridepublicvoidprintln(Stringinput){System.out.println("Matcherinput:"+input);}}AOP注入依赖Guice允许在AOP关联之前将AOP依赖注入到容器中!在src/下main/java目录下新建io.edurt.lc.guice.GuiceAopMatcherMethodInterceptor类文件,在文件包io.edurt.lc.guice中输入如下内容;importorg.aopalliance.intercept.MethodInterceptor;导入org.aopalliance.intercept.MethodInvocation;公共类GuiceAopMatcherMethodInterceptor实现MethodInterceptor{@OverridepublicObjectinvoke(MethodInvocationmethodInvocation)throwsThrowable{StringmethodName=methodInvocation.getMethod().getName();longstartTime=System.nanoTime();System.out.println(String.format"BeforeMethod[%s]startin%s",methodName,开始时间));对象响应;尝试{response=methodInvocation.proceed();}最后{longendTime=System.nanoTime();System.out.println(String.format("AfterMethod[%s]stop%s,Elapsed(nanosecond):%d",methodName,endTime,(endTime-startTime)));}返回响应;}}在src/test/java目录下创建io.edurt.lc.guice.GuiceAopJavaServiceMatcher类文件进行定义测试服务,添加如下代码包io.edurt.lc.guice;导入com.google.inject.matcher.Matcher;publicclassGuiceAopJavaServiceMatcherimplementsMatcher>{@Overridepublicbooleanmatches(ClassaClass){//returnaClass==GuiceAopMatcherService.class;返回aClass==GuiceAopMatcherServiceImpl.class;}@OverridepublicMatcher>and(Matcher>matcher){returnnull;}@OverridepublicMatcheror(Matcher>matcher){returnnull;}}接下来在src/test/java目录下创建io.edurt.lc.guice.TestGuiceAopJavaServiceMatcher类文件来测试定义的服务,添加如下代码包io.edurt.lc.guice;importcom.google.inject.Guice;导入com.google.inject.Injector;导入com.google.inject.matcher.Matchers;导入org.junit.Test;公共类TestGuiceAopJavaServiceMatcher{@Testpublicvoidtest(){Injectorinjector=Guice.createInjector(binder->binder.bindInterceptor(newGuiceAopJavaServiceMatcher(),Matchers.any(),newGuiceAopMatcherMethodInterceptor()));GuiceAopMatcherServicejavaMatServicematcher=injector.getAoppl(GuiceAopMatcherService.class);javaServiceMatcher.println("你好Guice!!!");我们运行程序输出BeforeMethod[println]startin174453945750833Matcherinput:HelloGuice!!!AfterMethod[println]stop174453952765375,Elapsed(nanosecond):7014542注意:返回aClass==GuiceAopMatcherService.class;相对类在这里确定。在我们的示例中,返回aClass==GuiceAopMatcherServiceImpl.class;如果要使用父类的自动转换,需要自己解析类的实现。源码地址GitHub