本教程主要详细讲解Guice的一些AOP方式,通过该简单教程让我们可以快速使用Guice进行AOP开发,后续我们会更深入讲解更多Guice中的AOP.
技术 | 版本 |
---|---|
Java | 1.8+ |
Guice | 4.2.3 |
mvn archetype:generate -DgroupId=io.edurt.lc.guice -DartifactId=guice-aop-basic -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0.0 -DinteractiveMode=false
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>lc-guice</artifactId>
<groupId>io.edurt.lc.guice</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>guice-aop-basic</artifactId>
<name>Learning Center for Guice AOP(Basic)</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
guice
: guice就是我们核心要使用的依赖
首先我们定义服务Service,这个服务有一个简单的方法println
.
src/main/java
目录下新建io.edurt.lc.guice.GuiceAopService类文件,在文件输入以下内容package io.edurt.lc.guice;
public interface GuiceAopService
{
void println(String input);
}
src/main/java
目录下新建io.edurt.lc.guice.GuiceAopServiceImpl类文件,在文件输入以下内容package io.edurt.lc.guice;
public class GuiceAopServiceImpl
implements GuiceAopService
{
@Override
public void println(String input)
{
System.out.println("Guice AOP Service : " + input);
}
}
接下来定义一个AOP的实现。在Aopalliance中(大家都认可的AOP联盟)实现我们的拦截器。GuiceAopLoggerMethodInterceptor
拦截器也没有做什么特别的事情,只是记录些执行的时间,由于执行时间比较短我们用纳秒来表示。
src/main/java
目录下新建io.edurt.lc.guice.GuiceAopLoggerMethodInterceptor类文件,在文件输入以下内容package io.edurt.lc.guice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class GuiceAopLoggerMethodInterceptor
implements MethodInterceptor
{
@Override
public Object invoke(MethodInvocation methodInvocation)
throws Throwable
{
String methodName = methodInvocation.getMethod().getName();
long startTime = System.nanoTime();
System.out.println(String.format("Before Method[%s] start in %s", methodName, startTime));
Object response;
try {
response = methodInvocation.proceed();
}
finally {
long endTime = System.nanoTime();
System.out.println(String.format("After Method[%s] stop %s, Elapsed(nanosecond): %d", methodName, endTime, (endTime - startTime)));
}
return response;
}
}
src/test/java
目录创建io.edurt.lc.guice.TestGuiceAOP类文件进行定义的服务进行测试,添加以下代码package io.edurt.lc.guice;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.matcher.Matchers;
import org.junit.Test;
public class TestGuiceAOP
{
@Test
public void test()
{
Injector injector = Guice.createInjector(binder -> binder.bindInterceptor(Matchers.any(), Matchers.any(), new GuiceAopLoggerMethodInterceptor()));
GuiceAopService aopService = injector.getInstance(GuiceAopService.class);
aopService.println("AOP Demo");
}
}
我们运行程序输出
Before Method[println] start in 206944653651583
Guice AOP Service : AOP Demo
After Method[println] stop 206944657624333, Elapsed(nanosecond): 3972750
需要注意的是:
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。