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

  • 相关阅读:
    树莓派设置音频输出音量
    linux下如何查询jdk的安装路径
    树莓派更新源集合
    树莓派wifi环境下初始化及环境配置
    JVM内存区域划分总结
    使用Mybatis Generator自动生成代码
    Clob类型转换为String
    树莓派3b+下一些常用的命令(Debian下)
    Leetcode 002-Search Insert Position
    Leetcode 001-twosum
  • 原文地址:https://www.cnblogs.com/develon/p/14503368.html
Copyright © 2011-2022 走看看