@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2011 the original author or authors .
* Copyright 2002 - 2015 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -16,12 +16,18 @@
@@ -16,12 +16,18 @@
package org.springframework.context.annotation ;
import java.lang.annotation.Retention ;
import java.lang.annotation.RetentionPolicy ;
import example.scannable.FooService ;
import example.scannable.ServiceInvocationCounter ;
import org.aspectj.lang.annotation.Aspect ;
import org.aspectj.lang.annotation.Before ;
import org.junit.Test ;
import org.springframework.aop.support.AopUtils ;
import org.springframework.context.ApplicationContext ;
import org.springframework.context.ConfigurableApplicationContext ;
import static org.hamcrest.CoreMatchers.* ;
import static org.junit.Assert.* ;
@ -32,38 +38,23 @@ import static org.junit.Assert.*;
@@ -32,38 +38,23 @@ import static org.junit.Assert.*;
* /
public class EnableAspectJAutoProxyTests {
@Configuration
@ComponentScan ( "example.scannable" )
@EnableAspectJAutoProxy
static class Config_WithJDKProxy {
}
@Configuration
@ComponentScan ( "example.scannable" )
@EnableAspectJAutoProxy ( proxyTargetClass = true )
static class Config_WithCGLIBProxy {
}
@Test
public void withJDKProxy ( ) throws Exception {
ApplicationContext ctx =
new AnnotationConfigApplicationContext ( Config_WithJDKProxy . class ) ;
public void withJdkProxy ( ) {
ApplicationContext ctx = new AnnotationConfigApplicationContext ( ConfigWithJdkProxy . class ) ;
aspectIsApplied ( ctx ) ;
assertThat ( AopUtils . isJdkDynamicProxy ( ctx . getBean ( FooService . class ) ) , is ( true ) ) ;
}
@Test
public void withCGLIBProxy ( ) throws Exception {
ApplicationContext ctx =
new AnnotationConfigApplicationContext ( Config_WithCGLIBProxy . class ) ;
public void withCglibProxy ( ) {
ApplicationContext ctx = new AnnotationConfigApplicationContext ( ConfigWithCglibProxy . class ) ;
aspectIsApplied ( ctx ) ;
assertThat ( AopUtils . isCglibProxy ( ctx . getBean ( FooService . class ) ) , is ( true ) ) ;
}
private void aspectIsApplied ( ApplicationContext ctx ) throws Exception {
private void aspectIsApplied ( ApplicationContext ctx ) {
FooService fooService = ctx . getBean ( FooService . class ) ;
ServiceInvocationCounter counter = ctx . getBean ( ServiceInvocationCounter . class ) ;
@ -79,4 +70,73 @@ public class EnableAspectJAutoProxyTests {
@@ -79,4 +70,73 @@ public class EnableAspectJAutoProxyTests {
fooService . foo ( 1 ) ;
assertEquals ( 3 , counter . getCount ( ) ) ;
}
}
@Test
public void withAnnotationOnArgumentAndJdkProxy ( ) {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext (
ConfigWithJdkProxy . class , SampleService . class , LoggingAspect . class ) ;
SampleService sampleService = ctx . getBean ( SampleService . class ) ;
sampleService . execute ( new SampleDto ( ) ) ;
sampleService . execute ( new SampleInputBean ( ) ) ;
sampleService . execute ( ( SampleDto ) null ) ;
sampleService . execute ( ( SampleInputBean ) null ) ;
}
@Test
public void withAnnotationOnArgumentAndCglibProxy ( ) {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext (
ConfigWithCglibProxy . class , SampleService . class , LoggingAspect . class ) ;
SampleService sampleService = ctx . getBean ( SampleService . class ) ;
sampleService . execute ( new SampleDto ( ) ) ;
sampleService . execute ( new SampleInputBean ( ) ) ;
sampleService . execute ( ( SampleDto ) null ) ;
sampleService . execute ( ( SampleInputBean ) null ) ;
}
@Configuration
@ComponentScan ( "example.scannable" )
@EnableAspectJAutoProxy
static class ConfigWithJdkProxy {
}
@Configuration
@ComponentScan ( "example.scannable" )
@EnableAspectJAutoProxy ( proxyTargetClass = true )
static class ConfigWithCglibProxy {
}
@Retention ( RetentionPolicy . RUNTIME )
public @interface Loggable {
}
@Loggable
public static class SampleDto {
}
public static class SampleInputBean {
}
public static class SampleService {
// Not matched method on {@link LoggingAspect}.
public void execute ( SampleInputBean inputBean ) {
}
// Matched method on {@link LoggingAspect}
public void execute ( SampleDto dto ) {
}
}
@Aspect
public static class LoggingAspect {
@Before ( "@args(org.springframework.context.annotation.EnableAspectJAutoProxyTests.Loggable))" )
public void loggingBeginByAtArgs ( ) {
}
}
}