|
|
|
@ -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"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -16,12 +16,18 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.context.annotation; |
|
|
|
package org.springframework.context.annotation; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.lang.annotation.Retention; |
|
|
|
|
|
|
|
import java.lang.annotation.RetentionPolicy; |
|
|
|
|
|
|
|
|
|
|
|
import example.scannable.FooService; |
|
|
|
import example.scannable.FooService; |
|
|
|
import example.scannable.ServiceInvocationCounter; |
|
|
|
import example.scannable.ServiceInvocationCounter; |
|
|
|
|
|
|
|
import org.aspectj.lang.annotation.Aspect; |
|
|
|
|
|
|
|
import org.aspectj.lang.annotation.Before; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.aop.support.AopUtils; |
|
|
|
import org.springframework.aop.support.AopUtils; |
|
|
|
import org.springframework.context.ApplicationContext; |
|
|
|
import org.springframework.context.ApplicationContext; |
|
|
|
|
|
|
|
import org.springframework.context.ConfigurableApplicationContext; |
|
|
|
|
|
|
|
|
|
|
|
import static org.hamcrest.CoreMatchers.*; |
|
|
|
import static org.hamcrest.CoreMatchers.*; |
|
|
|
import static org.junit.Assert.*; |
|
|
|
import static org.junit.Assert.*; |
|
|
|
@ -32,38 +38,23 @@ import static org.junit.Assert.*; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class EnableAspectJAutoProxyTests { |
|
|
|
public class EnableAspectJAutoProxyTests { |
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
@ComponentScan("example.scannable") |
|
|
|
|
|
|
|
@EnableAspectJAutoProxy |
|
|
|
|
|
|
|
static class Config_WithJDKProxy { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
|
|
|
@ComponentScan("example.scannable") |
|
|
|
|
|
|
|
@EnableAspectJAutoProxy(proxyTargetClass=true) |
|
|
|
|
|
|
|
static class Config_WithCGLIBProxy { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void withJDKProxy() throws Exception { |
|
|
|
public void withJdkProxy() { |
|
|
|
ApplicationContext ctx = |
|
|
|
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithJdkProxy.class); |
|
|
|
new AnnotationConfigApplicationContext(Config_WithJDKProxy.class); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
aspectIsApplied(ctx); |
|
|
|
aspectIsApplied(ctx); |
|
|
|
assertThat(AopUtils.isJdkDynamicProxy(ctx.getBean(FooService.class)), is(true)); |
|
|
|
assertThat(AopUtils.isJdkDynamicProxy(ctx.getBean(FooService.class)), is(true)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void withCGLIBProxy() throws Exception { |
|
|
|
public void withCglibProxy() { |
|
|
|
ApplicationContext ctx = |
|
|
|
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithCglibProxy.class); |
|
|
|
new AnnotationConfigApplicationContext(Config_WithCGLIBProxy.class); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
aspectIsApplied(ctx); |
|
|
|
aspectIsApplied(ctx); |
|
|
|
assertThat(AopUtils.isCglibProxy(ctx.getBean(FooService.class)), is(true)); |
|
|
|
assertThat(AopUtils.isCglibProxy(ctx.getBean(FooService.class)), is(true)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void aspectIsApplied(ApplicationContext ctx) { |
|
|
|
private void aspectIsApplied(ApplicationContext ctx) throws Exception { |
|
|
|
|
|
|
|
FooService fooService = ctx.getBean(FooService.class); |
|
|
|
FooService fooService = ctx.getBean(FooService.class); |
|
|
|
ServiceInvocationCounter counter = ctx.getBean(ServiceInvocationCounter.class); |
|
|
|
ServiceInvocationCounter counter = ctx.getBean(ServiceInvocationCounter.class); |
|
|
|
|
|
|
|
|
|
|
|
@ -79,4 +70,73 @@ public class EnableAspectJAutoProxyTests { |
|
|
|
fooService.foo(1); |
|
|
|
fooService.foo(1); |
|
|
|
assertEquals(3, counter.getCount()); |
|
|
|
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() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |