接上一篇 Java AOP (1) compile time weaving 【Java 切面编程 (1) 编译期织入】

Dynamic proxy   动态代理

Befor talking about runtime weaving, let's take a look at Java dynamic proxy.

在说运行时织入之间,我们先看看java动态代理

public class DynamicProxyTest {
    public interface Vehicle
    {
        void whistle();
    }

    public static class Boat implements Vehicle
    {
        @Override
        public void whistle()
        {
            System.out.println( "Boat whistle!" );
        }
    }

    public static class VehicleHandler implements InvocationHandler
    {
        private Object proxied;

        public VehicleHandler(Object proxied )
        {
            this.proxied = proxied;
        }

        public Object invoke(Object proxy, Method method, Object[] args ) throws Throwable
        {
            checkVehicle();
            return method.invoke( proxied, args);
        }

        private void checkVehicle() {
            System.out.println("Check vehicle status ...");
        }
    }

    public static void main(String[] args) throws Exception {
        Boat boat = new Boat();
        boat.whistle();
        System.out.println("--------------------------");
        Vehicle proxyBoat = (Vehicle) Proxy.newProxyInstance(DynamicProxyTest.class.getClassLoader(), new Class[]{Vehicle.class}, new VehicleHandler(boat));
        proxyBoat.whistle();
    }
}

Output:

Boat whistle!
--------------------------
Check vehicle status ...
Boat whistle!

Note the marked codes, we add an InvocationHandler class and then create a new proxy instance based on that. After the proxy object is generated, every method in vehicle will be invoked with an addition checkVehicle() call.

注意标红的代码,我们增加了一个请求处理类,然后基于它创建了一个代理实例。代理对象被创建之后,针对它的每一个方法调用都会伴随着一次额外的checkVehicle函数调用。

What's the magic behind dynamic proxy? Let's trace the JDK source code.

动态代理的底层魔法是什么呢? 来看看JDK源码一探究竟。

    public static Object newProxyInstance(ClassLoader loader,
                                          Class<?>[] interfaces,
                                          InvocationHandler h)
        throws IllegalArgumentException
    {
       .......
        /*
         * Look up or generate the designated proxy class.
         */
        Class<?> cl = getProxyClass0(loader, intfs);

        /*
         * Invoke its constructor with the designated invocation handler.
         */
       ........
        return cons.newInstance(new Object[]{h});
    }

The getProxyClass0 method will generate a proxy class using ProxyClassFactory

newProxyInstance 会首先用getProxyClass0方法,然后继续调用 ProxyClassFactory 工厂类来生成一个代理类,继而产生代理实例。

    /**
     * A factory function that generates, defines and returns the proxy class given
     * the ClassLoader and array of interfaces.
     */
    private static final class ProxyClassFactory
        implements BiFunction<ClassLoader, Class<?>[], Class<?>>
    {
     ..........
    }

After the proxy class is finally generated, every interface method will looks like

当实例类生成之后,每一个方法的实现大致如下

   public final int customizedMethod()
    {
        try
        {
            return ((Integer)super.h.invoke(this, m0, null)).intValue();
        }
        catch(Exception e) {
            .......
        }
    }

The marked part tells why the implementation of invocation handler becomes so useful, it becomes the entrance of every interface method.

标红部分会调用之前 InvocationHandler 方法中的自定义的 invoke 方法,它成为了每一个接口方法的入口函数。

Spring AOP

Official document https://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html

11.1.3 AOP Proxies

Spring AOP defaults to using standard JDK dynamic proxies for AOP proxies. This enables any interface (or set of interfaces) to be proxied.

Spring AOP can also use CGLIB proxies. This is necessary to proxy classes rather than interfaces. CGLIB is used by default if a business object does not implement an interface. As it is good practice to program to interfaces rather than classes; business classes normally will implement one or more business interfaces. It is possible toforce the use of CGLIB, in those (hopefully rare) cases where you need to advise a method that is not declared on an interface, or where you need to pass a proxied object to a method as a concrete type.

It is important to grasp the fact that Spring AOP is proxy-based. See Section 11.6.1, “Understanding AOP proxies” for a thorough examination of exactly what this implementation detail actually means.

JDK proxy  --> proxy interface, CFLIB proxy --> proxy any class

根据Spring的官方文档,Spring AOP有两种实现。 对于接口,默认使用JDK动态代理,而对于无接口实现的普通类,默认使用CGLIB进行代理。

Next, a demo will show how to use Spring AOP based on maven.

接下来将会演示如何使用Spring AOP

1) Add dependency

增加maven依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>

2) Create a simple spring boot app

创建一个简单的spring boot应用

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(AppConfig.class, args);
    }
}

@Configuration
@ComponentScan
public class AppConfig extends WebMvcConfigurerAdapter {
    @Autowired
    HandlerInterceptor apiInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(apiInterceptor);
    }
}

@RestController
public class SimpleController {

    @RequestMapping("/greeting")
    public String greeting(@RequestParam(value="name", defaultValue="World") String name) {
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression("'root.methodName'");
        String message = exp.getValue(String.class);

        return message;
    }

    @RequestMapping("/hello")
    public ResponseEntity hello(@RequestParam(value="name", defaultValue = "SYSTEM") String name) {
        return ResponseEntity.ok().body("Hello from " + name);
    }
}

3) Define an aspect class

定义一个切面类

@Aspect
@Component
public class ExecutionLogger {

    private Logger logger;

    public ExecutionLogger() {
        logger = LoggerFactory.getLogger(getClass());
        logger.info("Execution time logger created.");
    }

    @Pointcut("execution(* spring.SimpleController.*(*))")
    public void methodPointcut() {
    }

    @Around("methodPointcut()")
    public Object proceed(ProceedingJoinPoint pjp) throws Throwable {
        String name = pjp.getSignature().getName();
        logger.info("This is aop join point before [{}]", name);
        try {
            return pjp.proceed();
        } finally {
        }
    }
}

@Aspect, @Pointcut and @Around annotations makes it very convinient to use Spring AOP.

We can add point cut at any method around which cutomized process like log, time evaluation, etc can be added.

Aspect, Pointcut, Around注解使得使用Spring AOP变得很简单

我们可以在想要切入的方法上加入切点(通过Pointcut注解完成),然后在通过Around注解实现具体的切面逻辑。

4) Open browser, visit greeting controller

打开浏览器,访问刚刚定义的greeting

Below is console output

下面是控制台输出,可以看到每次greeting页面被打开时会有一条调用记录

2017-05-09 14:04:38.814  INFO 7580 --- [  XNIO-2 task-1] spring.ExecutionLogger                   : This is aop join point before [greeting]

Java AOP (2) runtime weaving 【Java 切面编程 (2) 运行时织入】的更多相关文章

  1. Java AOP (1) compile time weaving 【Java 切面编程 (1) 编译期织入】

    According to wikipedia  aspect-oriented programming (AOP) is a programming paradigm that aims to inc ...

  2. Spring AOP 之编译期织入、装载期织入、运行时织入(转)

    https://blog.csdn.net/wenbingoon/article/details/22888619 一   前言 AOP 实现的关键就在于 AOP 框架自动创建的 AOP 代理,AOP ...

  3. Java AOP nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice || Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0' 两个异常解决办法

    贴出applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...

  4. 深入理解JAVA虚拟机之JVM性能篇---基础知识点(运行时数据区域)

    一. 运行数据区域划分 各个数据区域功能如下: 1. 程序计数器: 较小的一块内存空间,可以看做是当前线程所执行的字节码的行号指示器,每条线程都有一个独立的程序计数器,各条线程之间计数器互不影响,独立 ...

  5. 深入理解Java虚拟机 -- 读书笔记(1):JVM运行时数据区域

    深入理解Java虚拟机 -- 读书笔记:JVM运行时数据区域 本文转载:http://blog.csdn.net/jubincn/article/details/8607790 本系列为<深入理 ...

  6. java中exception和error有什么区别,运行时异常和一般异常有什么区别

    1.exception和error都是继承了throwable类,在java中只有throwable类型的实例才可以被抛出(throw)或者捕获(catch),它是异常处理机制的基本组成类型 2.ex ...

  7. Java虚拟机所管理的内存,包含的运行时数据区域?

    运行时数据区域 线程私有(随用户线程的启动和结束而建立和销毁)或所有线程共享(随虚拟机进程的启动而存在) 抛出的异常 备注 程序计数器(Program Counter Register) 线程私有 唯 ...

  8. java动态编程库,利用动态编程打印运行时调用全景(函数调用关系链)

    如果是一般java程序,不追求性能极致,想使用方便,推荐使用 Javassist 库. 如果是android程序,或者一般java程序欲追求性能极限,推荐使用 asm for java 及 asmde ...

  9. 04—AOP 实现项目中的切面编程

随机推荐

  1. EF批量插入(转)

    原作者地址http://blog.csdn.net/zlts000/article/details/46385773 之前做项目的时候,做出来的系统的性能不太好,在框架中使用了EntityFramew ...

  2. iwebshop上传类的使用

    $upload = new IUpload('10240000',array('jpg','gif','png')); $dir = "upload/dian"; $upload- ...

  3. Office 365开发环境概览

    本文于2017年3月26日首发于LinkedIn,原文链接请参考这里 本系列文章已经按照既定计划在每周更新,此前的几篇文章如下 Office 365 开发概览系列文章和教程 Office 365开发概 ...

  4. 关于SQL调优(Distinct 和 Exits)

    今天写了一段查询人员Id和人员编号的,由于需要从其他的表中取条件,因为人员表和另外的表对应的是一对多的关系,所以我使用了Distinct关键字对用户编号进行去重复,然后发现那个效率简直没法看,然后旁边 ...

  5. Idea Maven 建本地仓库-导入本地JAR包

    需求 IDEA 很方便集成了Maven,但是也有相应的问题,比如使用Maven仓没有包的时候不太方便,这时我们需要建立自已的本地仓库来实现 实现 找到Idea的安装目录下面的Maven,我的在 C:\ ...

  6. 初识Object-C

    Object-C是苹果推出用来开发苹果软件的一门编程语言.大学学了3年的JAVA,到了大四毅然决然的放弃JAVA,是因为第一次接触Object-C就被它的简单语法吸引了.其实不仅仅是语法简单,相对于A ...

  7. 老李分享:性能测试你不应该只知道loadrunner(1)

    老李分享:性能测试你不应该只知道loadrunner(1)   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.poptest测试 ...

  8. C语言求最小公倍数和最大公约数三种算法(经典)

    把以前写的一些经验总结汇个总,方便给未来的学弟学妹们做个参考! --------------------------永远爱你们的:Sakura 最小公倍数:数论中的一种概念,两个整数公有的倍数成为他们 ...

  9. .Net MVC4笔记之Razor视图引擎的基础语法

    Razor视图引擎的基础语法: 1.“_”开头的cshtml文档将不能在服务器上访问,和asp.net中的config文档差不多. 2.Razor语法以@开头,以@{}进行包裹. 3.语法使用: 注释 ...

  10. struts2之拦截器

    1. 为什么需要拦截器 早期MVC框架将一些通用操作写死在核心控制器中,致使框架灵活性不足.可扩展性降低, Struts 2将核心功能放到多个拦截器中实现,拦截器可自由选择和组合,增强了灵活性,有利于 ...