Skip to content

Commit a6c2765

Browse files
committed
Revise test for ApplicationListener self injection
See gh-28322
1 parent 20c688e commit a6c2765

File tree

6 files changed

+83
-28
lines changed

6 files changed

+83
-28
lines changed

spring-context/src/test/java/org/springframework/context/event/ApplicationContextEventTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import org.springframework.context.ApplicationListener;
4242
import org.springframework.context.PayloadApplicationEvent;
4343
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
44-
import org.springframework.context.event.test.self_inject.MyApplication;
44+
import org.springframework.context.event.test.self_inject.MyAspect;
4545
import org.springframework.context.event.test.self_inject.MyEventListener;
4646
import org.springframework.context.event.test.self_inject.MyEventPublisher;
4747
import org.springframework.context.support.AbstractApplicationContext;
@@ -280,17 +280,17 @@ public void proxiedListenersMixedWithTargetListeners() {
280280
* Regression test for <a href="https://github.com/spring-projects/spring-framework/issues/28283">issue 28283</a>,
281281
* where event listeners proxied due to e.g.
282282
* <ul>
283-
* <li>{@code @Transactional} annotations in their methods or</li>
284-
* <li>being targeted by aspects</li>
283+
* <li>{@code @Transactional} annotations in their methods or</li>
284+
* <li>being targeted by aspects</li>
285285
* </ul>
286286
* were added to the list of application listener beans twice (both proxy and unwrapped target).
287287
*/
288288
@Test
289289
public void eventForSelfInjectedProxiedListenerFiredOnlyOnce() {
290-
String basePackage = MyApplication.class.getPackageName();
291-
AbstractApplicationContext context = new AnnotationConfigApplicationContext(basePackage);
290+
AbstractApplicationContext context = new AnnotationConfigApplicationContext(
291+
MyAspect.class, MyEventListener.class, MyEventPublisher.class);
292292
context.getBean(MyEventPublisher.class).publishMyEvent("hello");
293-
assertThat(MyEventListener.eventCount).isEqualTo(1);
293+
assertThat(context.getBean(MyEventListener.class).eventCount).isEqualTo(1);
294294
context.close();
295295
}
296296

spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyApplication.java

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
1+
/*
2+
* Copyright 2002-2023 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+
* https://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+
117
package org.springframework.context.event.test.self_inject;
218

319
import org.aspectj.lang.JoinPoint;
420
import org.aspectj.lang.annotation.Aspect;
521
import org.aspectj.lang.annotation.Before;
22+
623
import org.springframework.stereotype.Component;
724

825
@Aspect
926
@Component
1027
public class MyAspect {
28+
1129
@Before("within(org.springframework.context.event.test.self_inject.MyEventListener)")
1230
public void myAdvice(JoinPoint joinPoint) {
13-
//System.out.println(joinPoint);
31+
// System.out.println(joinPoint);
1432
}
33+
1534
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
1+
/*
2+
* Copyright 2002-2023 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+
* https://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+
117
package org.springframework.context.event.test.self_inject;
218

319
import org.springframework.context.ApplicationEvent;
420

21+
@SuppressWarnings("serial")
522
public class MyEvent extends ApplicationEvent {
23+
624
private String message;
725

826
public MyEvent(Object source, String message) {
927
super(source);
1028
this.message = message;
1129
}
30+
1231
}
Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2002-2023 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+
* https://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+
117
package org.springframework.context.event.test.self_inject;
218

319
import org.springframework.beans.factory.annotation.Autowired;
@@ -6,15 +22,15 @@
622

723
@Component
824
public class MyEventListener implements ApplicationListener<MyEvent> {
9-
public static int eventCount;
1025

11-
@Autowired // use '-Dspring.main.allow-circular-references=true' in Spring Boot >= 2.6.0
12-
//@Lazy // with '@Lazy', the problem does not occur
26+
public int eventCount;
27+
28+
@Autowired
1329
private MyEventListener eventDemoListener;
1430

1531
@Override
1632
public void onApplicationEvent(MyEvent event) {
17-
//System.out.println("Event: " + event);
1833
eventCount++;
1934
}
35+
2036
}

spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyEventPublisher.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2002-2023 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+
* https://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+
117
package org.springframework.context.event.test.self_inject;
218

319
import org.springframework.beans.factory.annotation.Autowired;
@@ -6,10 +22,12 @@
622

723
@Component
824
public class MyEventPublisher {
25+
926
@Autowired
1027
private ApplicationEventPublisher eventPublisher;
1128

1229
public void publishMyEvent(String message) {
1330
eventPublisher.publishEvent(new MyEvent(this, message));
1431
}
32+
1533
}

0 commit comments

Comments
 (0)