本教程次要具体解说Guice的一些AOP形式,通过该简略教程让咱们能够疾速应用Guice进行AOP开发,后续咱们会更深刻解说更多Guice中的AOP.

根底环境


技术版本
Java1.8+
Guice4.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);    }}
PrintlnServicePrintlnServiceImpl用于测试新的服务产出.
  • 接下来在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 AOPAfter [println]
须要留神的是:
binder.requestInjection(injectionMethodInterceptor); 该段代码用于注入自定义的AOP服务
binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(Names.named("println")), injectionMethodInterceptor);这里的第二个参数不能应用Matchers.any()否则会呈现死循环,因为容器会一直的进行aop操作

源码地址


GitHub