乐趣区

关于java:Guice-AOPMatcher

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

根底环境


技术 版本
Java 1.8+
Guice 4.2.3

初始化我的项目


  • 初始化我的项目
mvn archetype:generate -DgroupId=io.edurt.lc.guice -DartifactId=guice-aop-matcher -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-matcher</artifactId>
    <name>Learning Center for Guice AOP(Matcher)</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.GuiceAopMatcherService 类文件, 在文件输出以下内容
package io.edurt.lc.guice;

import com.google.inject.ImplementedBy;

@ImplementedBy(value = GuiceAopMatcherServiceImpl.class)
public interface GuiceAopMatcherService
{void println(String input);
}
  • src/main/java 目录下新建 io.edurt.lc.guice.GuiceAopMatcherServiceImpl 类文件, 在文件输出以下内容
package io.edurt.lc.guice;

public class GuiceAopMatcherServiceImpl
        implements GuiceAopMatcherService
{
    @Override
    public void println(String input)
    {System.out.println("Matcher input :" + input);
    }
}

AOP 注入依赖


Guice 容许在关联 AOP 之前将 AOP 的依赖都注入到容器中!

  • src/main/java 目录下新建 io.edurt.lc.guice.GuiceAopMatcherMethodInterceptor 类文件, 在文件输出以下内容
package io.edurt.lc.guice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class GuiceAopMatcherMethodInterceptor
        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.GuiceAopJavaServiceMatcher 类文件进行定义的服务进行测试, 增加以下代码
package io.edurt.lc.guice;

import com.google.inject.matcher.Matcher;

public class GuiceAopJavaServiceMatcher
        implements Matcher<Class<?>>
{
    @Override
    public boolean matches(Class<?> aClass)
    {
//        return aClass == GuiceAopMatcherService.class;
        return aClass == GuiceAopMatcherServiceImpl.class;
    }

    @Override
    public Matcher<Class<?>> and(Matcher<? super Class<?>> matcher)
    {return null;}

    @Override
    public Matcher<Class<?>> or(Matcher<? super Class<?>> matcher)
    {return null;}
}
  • 接下来在 src/test/java 目录创立 io.edurt.lc.guice.TestGuiceAopJavaServiceMatcher 类文件进行定义的服务进行测试, 增加以下代码
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 TestGuiceAopJavaServiceMatcher
{
    @Test
    public void test()
    {Injector injector = Guice.createInjector(binder -> binder.bindInterceptor(new GuiceAopJavaServiceMatcher(),
                Matchers.any(),
                new GuiceAopMatcherMethodInterceptor()));
        GuiceAopMatcherService javaServiceMatcher = injector.getInstance(GuiceAopMatcherServiceImpl.class);
        javaServiceMatcher.println("Hello Guice!!!");
    }
}

咱们运行程序输入

Before Method[println] start in 174453945750833
Matcher input : Hello Guice!!!
After Method[println] stop 174453952765375, Elapsed(nanosecond): 7014542

须要留神的是:
return aClass == GuiceAopMatcherService.class; 这里断定的是绝对的类, 在咱们的示例中是 return aClass == GuiceAopMatcherServiceImpl.class;
如果要应用父类主动转换的话须要本人解析类的实现即可

源码地址


GitHub

退出移动版