11/*
2- * Copyright 2002-2011 the original author or authors.
2+ * Copyright 2002-2015 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.
1616
1717package org .springframework .context .annotation ;
1818
19+ import java .lang .annotation .Retention ;
20+ import java .lang .annotation .RetentionPolicy ;
21+
1922import example .scannable .FooService ;
2023import example .scannable .ServiceInvocationCounter ;
24+ import org .aspectj .lang .annotation .Aspect ;
25+ import org .aspectj .lang .annotation .Before ;
2126import org .junit .Test ;
2227
2328import org .springframework .aop .support .AopUtils ;
2429import org .springframework .context .ApplicationContext ;
30+ import org .springframework .context .ConfigurableApplicationContext ;
2531
2632import static org .hamcrest .CoreMatchers .*;
2733import static org .junit .Assert .*;
3238 */
3339public class EnableAspectJAutoProxyTests {
3440
35- @ Configuration
36- @ ComponentScan ("example.scannable" )
37- @ EnableAspectJAutoProxy
38- static class Config_WithJDKProxy {
39- }
40-
41- @ Configuration
42- @ ComponentScan ("example.scannable" )
43- @ EnableAspectJAutoProxy (proxyTargetClass =true )
44- static class Config_WithCGLIBProxy {
45- }
46-
4741 @ Test
48- public void withJDKProxy () throws Exception {
49- ApplicationContext ctx =
50- new AnnotationConfigApplicationContext (Config_WithJDKProxy .class );
42+ public void withJdkProxy () {
43+ ApplicationContext ctx = new AnnotationConfigApplicationContext (ConfigWithJdkProxy .class );
5144
5245 aspectIsApplied (ctx );
5346 assertThat (AopUtils .isJdkDynamicProxy (ctx .getBean (FooService .class )), is (true ));
5447 }
5548
5649 @ Test
57- public void withCGLIBProxy () throws Exception {
58- ApplicationContext ctx =
59- new AnnotationConfigApplicationContext (Config_WithCGLIBProxy .class );
50+ public void withCglibProxy () {
51+ ApplicationContext ctx = new AnnotationConfigApplicationContext (ConfigWithCglibProxy .class );
6052
6153 aspectIsApplied (ctx );
6254 assertThat (AopUtils .isCglibProxy (ctx .getBean (FooService .class )), is (true ));
6355 }
6456
65-
66- private void aspectIsApplied (ApplicationContext ctx ) throws Exception {
57+ private void aspectIsApplied (ApplicationContext ctx ) {
6758 FooService fooService = ctx .getBean (FooService .class );
6859 ServiceInvocationCounter counter = ctx .getBean (ServiceInvocationCounter .class );
6960
@@ -79,4 +70,73 @@ private void aspectIsApplied(ApplicationContext ctx) throws Exception {
7970 fooService .foo (1 );
8071 assertEquals (3 , counter .getCount ());
8172 }
82- }
73+
74+ @ Test
75+ public void withAnnotationOnArgumentAndJdkProxy () {
76+ ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext (
77+ ConfigWithJdkProxy .class , SampleService .class , LoggingAspect .class );
78+
79+ SampleService sampleService = ctx .getBean (SampleService .class );
80+ sampleService .execute (new SampleDto ());
81+ sampleService .execute (new SampleInputBean ());
82+ sampleService .execute ((SampleDto ) null );
83+ sampleService .execute ((SampleInputBean ) null );
84+ }
85+
86+ @ Test
87+ public void withAnnotationOnArgumentAndCglibProxy () {
88+ ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext (
89+ ConfigWithCglibProxy .class , SampleService .class , LoggingAspect .class );
90+
91+ SampleService sampleService = ctx .getBean (SampleService .class );
92+ sampleService .execute (new SampleDto ());
93+ sampleService .execute (new SampleInputBean ());
94+ sampleService .execute ((SampleDto ) null );
95+ sampleService .execute ((SampleInputBean ) null );
96+ }
97+
98+
99+ @ Configuration
100+ @ ComponentScan ("example.scannable" )
101+ @ EnableAspectJAutoProxy
102+ static class ConfigWithJdkProxy {
103+ }
104+
105+ @ Configuration
106+ @ ComponentScan ("example.scannable" )
107+ @ EnableAspectJAutoProxy (proxyTargetClass = true )
108+ static class ConfigWithCglibProxy {
109+ }
110+
111+
112+ @ Retention (RetentionPolicy .RUNTIME )
113+ public @interface Loggable {
114+ }
115+
116+ @ Loggable
117+ public static class SampleDto {
118+ }
119+
120+ public static class SampleInputBean {
121+ }
122+
123+ public static class SampleService {
124+
125+ // Not matched method on {@link LoggingAspect}.
126+ public void execute (SampleInputBean inputBean ) {
127+ }
128+
129+ // Matched method on {@link LoggingAspect}
130+ public void execute (SampleDto dto ) {
131+ }
132+ }
133+
134+ @ Aspect
135+ public static class LoggingAspect {
136+
137+ @ Before ("@args(org.springframework.context.annotation.EnableAspectJAutoProxyTests.Loggable))" )
138+ public void loggingBeginByAtArgs () {
139+ }
140+ }
141+
142+ }
0 commit comments