Guice AOP(Matcher)

本教程主要详细讲解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依赖



    
        lc-guice
        io.edurt.lc.guice
        1.0.0
    

    4.0.0

    guice-aop-matcher
    Learning Center for Guice AOP(Matcher)

    
        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.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>
{
    @Override
    public boolean matches(Class aClass)
    {
//        return aClass == GuiceAopMatcherService.class;
        return aClass == GuiceAopMatcherServiceImpl.class;
    }

    @Override
    public Matcher> and(Matcher> matcher)
    {
        return null;
    }

    @Override
    public Matcher> or(Matcher> 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

你可能感兴趣的:(Guice AOP(Matcher))