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 static org .hamcrest .CoreMatchers .is ;
20- import static org .junit .Assert .assertEquals ;
21- import static org .junit .Assert .assertThat ;
22- import static org .junit .Assert .assertTrue ;
19+ import java .lang .annotation .Retention ;
20+ import java .lang .annotation .RetentionPolicy ;
2321
22+ import example .scannable .FooService ;
23+ import example .scannable .ServiceInvocationCounter ;
24+ import org .aspectj .lang .annotation .Aspect ;
25+ import org .aspectj .lang .annotation .Before ;
2426import org .junit .Test ;
27+
2528import org .springframework .aop .support .AopUtils ;
2629import org .springframework .context .ApplicationContext ;
30+ import org .springframework .context .ConfigurableApplicationContext ;
2731
28- import example . scannable . FooService ;
29- import example . scannable . ServiceInvocationCounter ;
32+ import static org . hamcrest . CoreMatchers .* ;
33+ import static org . junit . Assert .* ;
3034
3135/**
3236 * @author Juergen Hoeller
3337 * @author Chris Beams
3438 */
3539public class EnableAspectJAutoProxyTests {
3640
37- @ Configuration
38- @ ComponentScan ("example.scannable" )
39- @ EnableAspectJAutoProxy
40- static class Config_WithJDKProxy {
41- }
42-
43- @ Configuration
44- @ ComponentScan ("example.scannable" )
45- @ EnableAspectJAutoProxy (proxyTargetClass =true )
46- static class Config_WithCGLIBProxy {
47- }
48-
4941 @ Test
50- public void withJDKProxy () throws Exception {
51- ApplicationContext ctx =
52- new AnnotationConfigApplicationContext (Config_WithJDKProxy .class );
42+ public void withJdkProxy () {
43+ ApplicationContext ctx = new AnnotationConfigApplicationContext (ConfigWithJdkProxy .class );
5344
5445 aspectIsApplied (ctx );
5546 assertThat (AopUtils .isJdkDynamicProxy (ctx .getBean (FooService .class )), is (true ));
5647 }
5748
5849 @ Test
59- public void withCGLIBProxy () throws Exception {
60- ApplicationContext ctx =
61- new AnnotationConfigApplicationContext (Config_WithCGLIBProxy .class );
50+ public void withCglibProxy () {
51+ ApplicationContext ctx = new AnnotationConfigApplicationContext (ConfigWithCglibProxy .class );
6252
6353 aspectIsApplied (ctx );
6454 assertThat (AopUtils .isCglibProxy (ctx .getBean (FooService .class )), is (true ));
6555 }
6656
67-
68- private void aspectIsApplied (ApplicationContext ctx ) throws Exception {
57+ private void aspectIsApplied (ApplicationContext ctx ) {
6958 FooService fooService = ctx .getBean (FooService .class );
7059 ServiceInvocationCounter counter = ctx .getBean (ServiceInvocationCounter .class );
7160
@@ -81,4 +70,73 @@ private void aspectIsApplied(ApplicationContext ctx) throws Exception {
8170 fooService .foo (1 );
8271 assertEquals (3 , counter .getCount ());
8372 }
84- }
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