--Je veux tester si les paramètres AOP fonctionnent
Préparez la classe Service et MethodInterceptor suivants comme prérequis.
Service cible AOP
@Service
class SampleService {
    fun execute() {
        println("SampleService#execute")
    }
}
Interceptor
class SampleInterceptor(
        private val name: String
) : MethodInterceptor {
    override fun invoke(invocation: MethodInvocation?): Any? {
        println("intercept by $name")
        return invocation?.proceed()
    }
}
class SampleServicePointCut : StaticMethodMatcherPointcut() {
    override fun matches(method: Method, @Nullable targetClass: Class<*>?): Boolean {
        return targetClass?.let { SampleService::class.java.isAssignableFrom(it) } ?: false
    }
}
config
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
class AopConfig {
    @Bean
    fun interceptorA(): Advisor {
        return DefaultPointcutAdvisor(SampleServicePointCut(), SampleInterceptor("configured interceptor"))
    }
}
Code de test
@SpringBootApplication
class SpringTestApplication
@RunWith(SpringRunner::class)
@SpringBootTest(classes = [SpringTestApplication::class])
internal class SampleServiceTest {
    @Autowired
    private lateinit var service: SampleService
    @Test
    fun test() {
        service.execute()
    }
}
Résultat d'exécution
intercept by configured interceptor
SampleService#execute
Code de test à l'aide de ProxyFactory
@Test
fun testByProxy() {
    val factory = ProxyFactory(SampleService())
    factory.addAdvisor(DefaultPointcutAdvisor(SampleServicePointCut(), SampleInterceptor("Proxy")))
    val proxy = factory.proxy as SampleService
    proxy.execute()
}
Résultat d'exécution
intercept by Proxy
SampleService#execute
Il est également bon de créer une fonction util en utilisant Extension.
Exemple d'utilisation du code de test d'extension kotlin à l'aide de ProxyFactory
@Test
fun testByProxy() {
    val proxy = SampleService().proxy {
        addAdvisor(DefaultPointcutAdvisor(SampleServicePointCut(), SampleInterceptor("Proxy")))
    }
    proxy.execute()
}
@Suppress("UNCHECKED_CAST")
fun <T : Any> T.proxy(settings: ProxyFactory.() -> Unit): T {
    return ProxyFactory(this).also { settings(it) }.proxy as T
}
Service et configuration
@Aspect
@Component
class SampleAspect(
        private val name: String = ""
) {
    @Before("execution(* SampleAspectService.*(..))")
    fun advice() {
        println("advice by $name")
    }
}
@Service
class SampleAspectService {
    fun execute() {
        println("SampleAspectService#execute")
    }
}
Code de test
@Test
fun testAspectProxy() {
    val factory = AspectJProxyFactory(SampleAspectService())
    factory.addAspect(SampleAspect("proxy"))
    val proxy = factory.getProxy() as SampleAspectService
    proxy.execute()
}
Résultat d'exécution
advice by proxy
SampleAspectService#execute