Skip to content

Commit 739775c

Browse files
committed
Introduce @EnableAspectJAutoProxy
Issue: SPR-8138
1 parent b08a6d3 commit 739775c

File tree

6 files changed

+267
-2
lines changed

6 files changed

+267
-2
lines changed

org.springframework.aop/src/main/resources/org/springframework/aop/config/spring-aop-3.1.xsd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@
7272
<xsd:annotation>
7373
<xsd:documentation source="java:org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"><![CDATA[
7474
Enables the use of the @AspectJ style of Spring AOP.
75+
76+
See org.springframework.context.annotation.EnableAspectJAutoProxy Javadoc
77+
for information on code-based alternatives to this XML element.
7578
]]></xsd:documentation>
7679
</xsd:annotation>
7780
<xsd:complexType>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context.annotation;
18+
19+
import java.util.Map;
20+
21+
import org.springframework.aop.config.AopConfigUtils;
22+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
23+
import org.springframework.core.type.AnnotationMetadata;
24+
25+
/**
26+
* Registers an {@link org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator
27+
* AnnotationAwareAspectJAutoProxyCreator} against the current {@link BeanDefinitionRegistry}
28+
* as appropriate based on a given @{@link EnableAspectJAutoProxy} annotation.
29+
*
30+
* @author Chris Beams
31+
* @see EnableAspectJAutoProxy
32+
* @since 3.1
33+
*/
34+
public class AspectJAutoProxyConfigurationSelector implements ImportSelector {
35+
36+
/**
37+
* Register, escalate, and configure the AspectJ auto proxy creator. Always return
38+
* an empty array, as no actual {@code @Configuration} classes are required.
39+
*/
40+
public String[] selectImports(ImportSelectorContext context) {
41+
BeanDefinitionRegistry registry = context.getBeanDefinitionRegistry();
42+
AnnotationMetadata importingClassMetadata = context.getImportingClassMetadata();
43+
44+
Map<String, Object> enableAJAutoProxy =
45+
importingClassMetadata.getAnnotationAttributes(EnableAspectJAutoProxy.class.getName());
46+
47+
AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);
48+
49+
if ((Boolean)enableAJAutoProxy.get("proxyTargetClass")) {
50+
AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
51+
}
52+
53+
return new String[] { };
54+
}
55+
56+
}

org.springframework.context/src/main/java/org/springframework/context/annotation/Configuration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,9 @@
299299
* classes using their respective "{@code @Enable}" annotations. See
300300
* {@link org.springframework.scheduling.annotation.EnableAsync @EnableAsync},
301301
* {@link org.springframework.scheduling.annotation.EnableScheduling @EnableScheduling},
302-
* {@link org.springframework.transaction.annotation.EnableTransactionManagement @EnableTransactionManagement}, and
303-
* {@link org.springframework.web.servlet.config.annotation.EnableWebMvc @EnableWebMvc}
302+
* {@link org.springframework.transaction.annotation.EnableTransactionManagement @EnableTransactionManagement},
303+
* {@link org.springframework.context.annotation.EnableAspectJAutoProxy @EnableAspectJAutoProxy},
304+
* and {@link org.springframework.web.servlet.config.annotation.EnableWebMvc @EnableWebMvc}
304305
* for details.
305306
*
306307
* <h2>Constraints when authoring {@code @Configuration} classes</h2>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context.annotation;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
/**
26+
* Enables support for handling components marked with AspectJ's {@code @Aspect} annotation,
27+
* similar to functionality found in Spring's {@code <aop:aspectj-autoproxy>} XML element.
28+
* To be used on @{@link Configuration} classes as follows:
29+
*
30+
* <pre class="code">
31+
* &#064;Configuration
32+
* &#064;EnableAspectJAutoProxy
33+
* public class AppConfig {
34+
* &#064;Bean
35+
* public FooService fooService() {
36+
* return new FooService();
37+
* }
38+
*
39+
* &#064;Bean
40+
* public MyAspect myAspect() {
41+
* return new MyAspect();
42+
* }
43+
* }</pre>
44+
*
45+
* Where {@code FooService} is a typical POJO component and {@code MyAspect} is an
46+
* {@code @Aspect}-style aspect:
47+
*
48+
* <pre class="code">
49+
* public class FooService {
50+
* // various methods
51+
* }</pre>
52+
*
53+
* <pre class="code">
54+
* &#064;Aspect
55+
* public class MyAspect {
56+
* &#064;Before("execution(* FooService+.*(..))")
57+
* public void advice() {
58+
* // advise FooService methods as appropriate
59+
* }
60+
* }</pre>
61+
*
62+
* In the scenario above, {@code @EnableAspectJAutoProxy} ensures that {@code MyAspect}
63+
* will be properly processed and that {@code FooService} will be proxied mixing in the
64+
* advice that it contributes.
65+
*
66+
* <p>Users can control the type of proxy that gets created for {@code FooService} using
67+
* the {@link #proxyTargetClass()} attribute. The following enables CGLIB-style 'subclass'
68+
* proxies as opposed to the default interface-based JDK proxy approach.
69+
* <pre class="code">
70+
* &#064;Configuration
71+
* &#064;EnableAspectJAutoProxy(proxyTargetClass=true)
72+
* public class AppConfig {
73+
* // ...
74+
* }</pre>
75+
*
76+
* <p>Note that {@code @Aspect} beans may be component-scanned like any other. Simply
77+
* mark the aspect with both {@code @Aspect} and {@code @Component}:
78+
* <pre class="code">
79+
* package com.foo;
80+
*
81+
* &#064;Component
82+
* public class FooService { ... }
83+
*
84+
* &#064;Aspect
85+
* &#064;Component
86+
* public class MyAspect { ... }</pre>
87+
*
88+
* Then use the @{@link ComponentScan} annotation to pick both up:
89+
* <pre class="code">
90+
* &#064;Configuration
91+
* &#064;ComponentScan("com.foo")
92+
* &#064;EnableAspectJAutoProxy
93+
* public class AppConfig {
94+
* // no explicit &#064Bean definitions required
95+
* }</pre>
96+
*
97+
* @author Chris Beams
98+
* @since 3.1
99+
* @see org.aspectj.lang.annotation.Aspect
100+
*/
101+
@Target(ElementType.TYPE)
102+
@Retention(RetentionPolicy.RUNTIME)
103+
@Import(AspectJAutoProxyConfigurationSelector.class)
104+
@Documented
105+
public @interface EnableAspectJAutoProxy {
106+
107+
/**
108+
* Indicate whether subclass-based (CGLIB) proxies are to be created as opposed
109+
* to standard Java interface-based proxies. The default is {@code false}.
110+
*/
111+
boolean proxyTargetClass() default false;
112+
113+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2002-2008 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
import org.springframework.aop.aspectj.annotation.EnableAspectJAutoProxy;
9+
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.springframework.context.annotation;
20+
21+
import static org.hamcrest.CoreMatchers.is;
22+
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertThat;
24+
import static org.junit.Assert.assertTrue;
25+
26+
import org.junit.Test;
27+
import org.springframework.aop.support.AopUtils;
28+
import org.springframework.context.ApplicationContext;
29+
30+
import example.scannable.FooService;
31+
import example.scannable.ServiceInvocationCounter;
32+
33+
/**
34+
* @author Juergen Hoeller
35+
* @author Chris Beams
36+
*/
37+
public class EnableAspectJAutoProxyTests {
38+
39+
@Configuration
40+
@ComponentScan("example.scannable")
41+
@EnableAspectJAutoProxy
42+
static class Config_WithJDKProxy {
43+
}
44+
45+
@Configuration
46+
@ComponentScan("example.scannable")
47+
@EnableAspectJAutoProxy(proxyTargetClass=true)
48+
static class Config_WithCGLIBProxy {
49+
}
50+
51+
@Test
52+
public void withJDKProxy() throws Exception {
53+
ApplicationContext ctx =
54+
new AnnotationConfigApplicationContext(Config_WithJDKProxy.class);
55+
56+
aspectIsApplied(ctx);
57+
assertThat(AopUtils.isJdkDynamicProxy(ctx.getBean(FooService.class)), is(true));
58+
}
59+
60+
@Test
61+
public void withCGLIBProxy() throws Exception {
62+
ApplicationContext ctx =
63+
new AnnotationConfigApplicationContext(Config_WithCGLIBProxy.class);
64+
65+
aspectIsApplied(ctx);
66+
assertThat(AopUtils.isCglibProxy(ctx.getBean(FooService.class)), is(true));
67+
}
68+
69+
70+
private void aspectIsApplied(ApplicationContext ctx) throws Exception {
71+
FooService fooService = ctx.getBean(FooService.class);
72+
ServiceInvocationCounter counter = ctx.getBean(ServiceInvocationCounter.class);
73+
74+
assertEquals(0, counter.getCount());
75+
76+
assertTrue(fooService.isInitCalled());
77+
assertEquals(1, counter.getCount());
78+
79+
String value = fooService.foo(1);
80+
assertEquals("bar", value);
81+
assertEquals(2, counter.getCount());
82+
83+
fooService.foo(1);
84+
assertEquals(3, counter.getCount());
85+
}
86+
}

spring-framework-reference/src/new-in-3.1.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@
128128
Javadoc</para>
129129
</listitem>
130130

131+
<listitem>
132+
<para>See
133+
org.springframework.context.annotation.EnableAspectJAutoProxy
134+
Javadoc</para>
135+
</listitem>
136+
131137
<listitem>
132138
<para>See org.springframework.scheduling.annotation.EnableScheduling
133139
Javadoc</para>

0 commit comments

Comments
 (0)