zoukankan      html  css  js  c++  java
  • AOP

    原理

    cglib

    JDK Proxy实现。

    Spring-AOP 简介

    @see: https://www.baeldung.com/spring-aop

    AspectJ 简介

    @see: https://www.baeldung.com/aspectj

    maven 依赖

        implementation 'org.springframework:spring-aop:5.2.0.RELEASE'         // <----- 似乎只支持XML
    
        // AOP
        // https://mvnrepository.com/artifact/org.aspectj/aspectjrt
        implementation group: 'org.aspectj', name: 'aspectjrt', version: '1.9.6'
        // https://mvnrepository.com/artifact/org.aspectj/aspectjweaver
        implementation group: 'org.aspectj', name: 'aspectjweaver', version: '1.9.6'
    

    测试

    package aop
    
    import org.aspectj.lang.annotation.Aspect
    import org.aspectj.lang.annotation.Before
    import org.junit.jupiter.api.Assertions.*
    import org.junit.jupiter.api.Test
    import org.junit.jupiter.api.extension.ExtendWith
    import org.springframework.beans.factory.annotation.Autowired
    import org.springframework.context.ApplicationContext
    import org.springframework.context.annotation.ComponentScan
    import org.springframework.context.annotation.EnableAspectJAutoProxy
    import org.springframework.stereotype.Component
    import org.springframework.test.context.junit.jupiter.SpringExtension
    import org.springframework.test.context.junit.jupiter.SpringJUnitConfig
    
    var isHandle = false
    
    interface Foo {
        fun bar(): String = "Foo#bar()"
    }
    
    @Component
    class MyFoo : Foo {
        override fun bar(): String {
            println("calling")
            return super.bar()
        }
    }
    
    @Component
    @Aspect
    @EnableAspectJAutoProxy
    class MyAspect {
        @Before("execution(String bar())")
        fun handle() {
            println("handle called")
            isHandle = true
        }
    }
    
    @ComponentScan
    class HeadfirstAOPJavaConfig
    
    @ExtendWith(SpringExtension::class)
    @SpringJUnitConfig(HeadfirstAOPJavaConfig::class)
    class HeadfirstAOP {
        @Autowired lateinit var ctx: ApplicationContext
    
        @Autowired lateinit var foo: Foo
    
        @Test fun `handle() should be call`() {
            assertFalse(isHandle)
            val str = foo.bar()
            assertTrue(isHandle)
            println("str=$str")
        }
    }
    

    END

  • 相关阅读:
    swift2.0学习之拓展
    CSDN处理问题神速,顶你,为你点32个赞!
    quick-cocos2d-x教程7:程序框架内framework文件夹分析
    Android自己定义Toast
    leetcode: Add Digits
    Effective Java:对于全部对象都通用的方法
    动态规划法-01背包问题
    loosejar原理简要分析
    eclipse中Client/Server程序生成exe
    Spring 新手教程(三) 注入和自己主动装配
  • 原文地址:https://www.cnblogs.com/develon/p/14503368.html
Copyright © 2011-2022 走看看