Skip to content

Commit 8cb9d5e

Browse files
committed
EnableAspectJAutoProxy features exposeProxy flag (analogous to XML namespace)
Issue: SPR-10454
1 parent 01f1158 commit 8cb9d5e

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

spring-aop/src/main/java/org/springframework/aop/config/AopConfigUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -97,7 +97,7 @@ public static void forceAutoProxyCreatorToUseClassProxying(BeanDefinitionRegistr
9797
}
9898
}
9999

100-
static void forceAutoProxyCreatorToExposeProxy(BeanDefinitionRegistry registry) {
100+
public static void forceAutoProxyCreatorToExposeProxy(BeanDefinitionRegistry registry) {
101101
if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
102102
BeanDefinition definition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
103103
definition.getPropertyValues().add("exposeProxy", Boolean.TRUE);

spring-context/src/main/java/org/springframework/context/annotation/AspectJAutoProxyRegistrar.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
* as appropriate based on a given @{@link EnableAspectJAutoProxy} annotation.
2828
*
2929
* @author Chris Beams
30+
* @author Juergen Hoeller
3031
* @since 3.1
3132
* @see EnableAspectJAutoProxy
3233
*/
@@ -43,11 +44,14 @@ public void registerBeanDefinitions(
4344

4445
AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);
4546

46-
AnnotationAttributes enableAJAutoProxy =
47+
AnnotationAttributes enableAspectJAutoProxy =
4748
AnnotationConfigUtils.attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class);
48-
if (enableAJAutoProxy.getBoolean("proxyTargetClass")) {
49+
if (enableAspectJAutoProxy.getBoolean("proxyTargetClass")) {
4950
AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
5051
}
52+
if (enableAspectJAutoProxy.getBoolean("exposeProxy")) {
53+
AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);
54+
}
5155
}
5256

5357
}

spring-context/src/main/java/org/springframework/context/annotation/EnableAspectJAutoProxy.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
* }</pre>
103103
*
104104
* @author Chris Beams
105+
* @author Juergen Hoeller
105106
* @since 3.1
106107
* @see org.aspectj.lang.annotation.Aspect
107108
*/
@@ -117,4 +118,12 @@
117118
*/
118119
boolean proxyTargetClass() default false;
119120

121+
/**
122+
* Indicate that the proxy should be exposed by the AOP framework as a {@code ThreadLocal}
123+
* for retrieval via the {@link org.springframework.aop.framework.AopContext} class.
124+
* Off by default, i.e. no guarantees that {@code AopContext} access will work.
125+
* @since 4.3.1
126+
*/
127+
boolean exposeProxy() default false;
128+
120129
}

spring-context/src/test/java/org/springframework/context/annotation/EnableAspectJAutoProxyTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import java.lang.annotation.RetentionPolicy;
2121

2222
import example.scannable.FooService;
23+
import example.scannable.FooServiceImpl;
2324
import example.scannable.ServiceInvocationCounter;
2425
import org.aspectj.lang.annotation.Aspect;
2526
import org.aspectj.lang.annotation.Before;
2627
import org.junit.Test;
2728

29+
import org.springframework.aop.framework.AopContext;
2830
import org.springframework.aop.support.AopUtils;
2931
import org.springframework.context.ApplicationContext;
3032
import org.springframework.context.ConfigurableApplicationContext;
@@ -54,6 +56,14 @@ public void withCglibProxy() {
5456
assertThat(AopUtils.isCglibProxy(ctx.getBean(FooService.class)), is(true));
5557
}
5658

59+
@Test
60+
public void withExposedProxy() {
61+
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithExposedProxy.class);
62+
63+
aspectIsApplied(ctx);
64+
assertThat(AopUtils.isJdkDynamicProxy(ctx.getBean(FooService.class)), is(true));
65+
}
66+
5767
private void aspectIsApplied(ApplicationContext ctx) {
5868
FooService fooService = ctx.getBean(FooService.class);
5969
ServiceInvocationCounter counter = ctx.getBean(ServiceInvocationCounter.class);
@@ -101,23 +111,44 @@ public void withAnnotationOnArgumentAndCglibProxy() {
101111
static class ConfigWithJdkProxy {
102112
}
103113

114+
104115
@ComponentScan("example.scannable")
105116
@EnableAspectJAutoProxy(proxyTargetClass = true)
106117
static class ConfigWithCglibProxy {
107118
}
108119

109120

121+
@ComponentScan("example.scannable")
122+
@EnableAspectJAutoProxy(exposeProxy = true)
123+
static class ConfigWithExposedProxy {
124+
125+
@Bean
126+
public FooService fooServiceImpl() {
127+
return new FooServiceImpl() {
128+
@Override
129+
public String foo(int id) {
130+
assertNotNull(AopContext.currentProxy());
131+
return super.foo(id);
132+
}
133+
};
134+
}
135+
}
136+
137+
110138
@Retention(RetentionPolicy.RUNTIME)
111139
public @interface Loggable {
112140
}
113141

142+
114143
@Loggable
115144
public static class SampleDto {
116145
}
117146

147+
118148
public static class SampleInputBean {
119149
}
120150

151+
121152
public static class SampleService {
122153

123154
// Not matched method on {@link LoggingAspect}.
@@ -129,6 +160,7 @@ public void execute(SampleDto dto) {
129160
}
130161
}
131162

163+
132164
@Aspect
133165
public static class LoggingAspect {
134166

0 commit comments

Comments
 (0)