本教程次要具体解说 Guice 的一些 AOP 形式, 通过该简略教程让咱们能够疾速应用 Guice 进行 AOP 开发, 后续咱们会更深刻解说更多 Guice 中的 AOP.
根底环境
技术 | 版本 |
---|---|
Java | 1.8+ |
Guice | 4.2.3 |
初始化我的项目
- 初始化我的项目
mvn archetype:generate -DgroupId=io.edurt.lc.guice -DartifactId=guice-aop-senior -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0.0 -DinteractiveMode=false
- 批改 pom.xml 减少 Guice 依赖
<?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-senior</artifactId>
<name>Learning Center for Guice AOP(Senior)</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>
初始化 Service
首先咱们定义服务 Service,这个服务有一个简略的办法println
.
- 在
src/main/java
目录下新建 io.edurt.lc.guice.GuiceAopSeniorService 类文件, 在文件输出以下内容
package io.edurt.lc.guice;
import com.google.inject.ImplementedBy;
import org.aopalliance.intercept.MethodInvocation;
@ImplementedBy(value = GuiceAopSeniorServiceImpl.class)
public interface GuiceAopSeniorService
{void before(MethodInvocation invocation);
}
- 在
src/main/java
目录下新建 io.edurt.lc.guice.GuiceAopSeniorServiceImpl 类文件, 在文件输出以下内容
package io.edurt.lc.guice;
import org.aopalliance.intercept.MethodInvocation;
public class GuiceAopSeniorServiceImpl
implements GuiceAopSeniorService
{
@Override
public void before(MethodInvocation invocation)
{System.out.println(String.format("Before method [%s]", invocation.getMethod().getName()));
}
}
AOP 注入依赖
Guice 容许在关联 AOP 之前将 AOP 的依赖都注入到容器中!
- 在
src/main/java
目录下新建 io.edurt.lc.guice.GuiceAopInjectionMethodInterceptor 类文件, 在文件输出以下内容
package io.edurt.lc.guice;
import com.google.inject.Inject;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class GuiceAopInjectionMethodInterceptor
implements MethodInterceptor
{
@Inject
private GuiceAopSeniorService service;
@Override
public Object invoke(MethodInvocation invocation)
throws Throwable
{service.before(invocation);
Object response;
try {response = invocation.proceed();
}
finally {System.out.println(String.format("After [%s]", invocation.getMethod().getName()));
}
return response;
}
}
- 在
src/main/java
目录下新建 io.edurt.lc.guice.PrintlnService 类文件, 在文件输出以下内容
package io.edurt.lc.guice;
import com.google.inject.ImplementedBy;
@ImplementedBy(value = PrintlnServiceImpl.class)
public interface PrintlnService
{void println(String input);
}
- 在
src/main/java
目录下新建 io.edurt.lc.guice.PrintlnServiceImpl 类文件, 在文件输出以下内容
package io.edurt.lc.guice;
import com.google.inject.name.Named;
public class PrintlnServiceImpl
implements PrintlnService
{
@Override
@Named(value = "println")
public void println(String input)
{System.out.println(input);
}
}
PrintlnService
和PrintlnServiceImpl
用于测试新的服务产出.
- 接下来在
src/test/java
目录创立io.edurt.lc.guice.TestGuiceAopSenior
类文件进行定义的服务进行测试, 增加以下代码
package io.edurt.lc.guice;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.matcher.Matchers;
import com.google.inject.name.Names;
import org.junit.Test;
public class TestGuiceAopSenior
{
@Inject
private PrintlnService printlnService;
@Test
public void test()
{
Injector injector = Guice.createInjector(binder -> {GuiceAopInjectionMethodInterceptor injectionMethodInterceptor = new GuiceAopInjectionMethodInterceptor();
binder.requestInjection(injectionMethodInterceptor);
binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(Names.named("println")), injectionMethodInterceptor);
});
TestGuiceAopSenior application = injector.getInstance(TestGuiceAopSenior.class);
application.printlnService.println("Hello Guice AOP");
}
}
咱们运行程序输入
Before method [println]
Hello Guice AOP
After [println]
须要留神的是:
binder.requestInjection(injectionMethodInterceptor);
该段代码用于注入自定义的 AOP 服务binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(Names.named("println")), injectionMethodInterceptor);
这里的第二个参数不能应用Matchers.any()
否则会呈现死循环, 因为容器会一直的进行 aop 操作
源码地址
GitHub