Guice AOP(进阶版)
本教程主要详细讲解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依赖
lc-guice
io.edurt.lc.guice
1.0.0
4.0.0 guice-aop-senior
Learning Center for Guice AOP(Senior) 1.8
1.8
com.google.inject
guice
4.2.3
junit
junit
4.12
test
初始化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 = https://www.it610.com/article/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 = https://www.it610.com/article/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 = "https://www.it610.com/article/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]
需要注意的是:源码地址 【Guice AOP(进阶版)】GitHub
binder.requestInjection(injectionMethodInterceptor);
该段代码用于注入自定义的AOP服务
binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(Names.named("println")), injectionMethodInterceptor);
这里的第二个参数不能使用Matchers.any()
否则会出现死循环,因为容器会不断的进行aop操作
推荐阅读
- 由浅入深理解AOP
- 推荐系统论文进阶|CTR预估 论文精读(十一)--Deep Interest Evolution Network(DIEN)
- 普通人进阶的三个过程-很多人都知道,但却本末倒置
- 易效能进阶课程笔记29
- Spring|Spring 框架之 AOP 原理剖析已经出炉!!!预定的童鞋可以识别下发二维码去看了
- C语言进阶栈帧示例详解教程
- 14--Python|14--Python 面向对象进阶
- 参与直播阿福童财商进阶课――第二课广告大探秘
- C语言学习(bit)|16.C语言进阶——深度剖析数据在内存中的存储
- Python机器学习基础与进阶|Python机器学习--集成学习算法--XGBoost算法