• 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

  • 相关阅读:
    [算法初步]希尔排序
    逆波兰表达式1(简介)
    [数据结构]之链表
    [数据结构]之栈
    [算法初步]之归并排序
    [算法初步]之快速排序
    [算法初步]之冒泡排序
    逆波兰表达式2中缀转后缀表达式)
    [算法初步]之简单选择排序
    [数据结构]之顺序表
  • 原文地址:https://www.cnblogs.com/develon/p/14503368.html
Copyright © 2020-2023  润新知