diff --git a/spring-context/src/test/java/org/springframework/context/event/ApplicationContextEventTests.java b/spring-context/src/test/java/org/springframework/context/event/ApplicationContextEventTests.java
index a7f6676a00..3849c623bd 100644
--- a/spring-context/src/test/java/org/springframework/context/event/ApplicationContextEventTests.java
+++ b/spring-context/src/test/java/org/springframework/context/event/ApplicationContextEventTests.java
@@ -41,7 +41,7 @@ import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.PayloadApplicationEvent;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
-import org.springframework.context.event.test.self_inject.MyApplication;
+import org.springframework.context.event.test.self_inject.MyAspect;
import org.springframework.context.event.test.self_inject.MyEventListener;
import org.springframework.context.event.test.self_inject.MyEventPublisher;
import org.springframework.context.support.AbstractApplicationContext;
@@ -280,17 +280,17 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen
* Regression test for issue 28283,
* where event listeners proxied due to e.g.
*
- * - {@code @Transactional} annotations in their methods or
- * - being targeted by aspects
+ * - {@code @Transactional} annotations in their methods or
+ * - being targeted by aspects
*
* were added to the list of application listener beans twice (both proxy and unwrapped target).
*/
@Test
public void eventForSelfInjectedProxiedListenerFiredOnlyOnce() {
- String basePackage = MyApplication.class.getPackageName();
- AbstractApplicationContext context = new AnnotationConfigApplicationContext(basePackage);
+ AbstractApplicationContext context = new AnnotationConfigApplicationContext(
+ MyAspect.class, MyEventListener.class, MyEventPublisher.class);
context.getBean(MyEventPublisher.class).publishMyEvent("hello");
- assertThat(MyEventListener.eventCount).isEqualTo(1);
+ assertThat(context.getBean(MyEventListener.class).eventCount).isEqualTo(1);
context.close();
}
diff --git a/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyApplication.java b/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyApplication.java
deleted file mode 100644
index 6686c1b121..0000000000
--- a/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyApplication.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package org.springframework.context.event.test.self_inject;
-
-import org.springframework.context.annotation.AnnotationConfigApplicationContext;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.context.annotation.EnableAspectJAutoProxy;
-import org.springframework.context.support.AbstractApplicationContext;
-
-@Configuration
-@EnableAspectJAutoProxy(proxyTargetClass = true)
-public class MyApplication {
- public static void main(String[] args) {
- try (AbstractApplicationContext context = new AnnotationConfigApplicationContext("org.springframework.context.event.test.self_inject")) {
- context.getBean(MyEventPublisher.class).publishMyEvent("hello");
- assert MyEventListener.eventCount == 1 : "event listener must fire exactly once";
- }
- }
-}
diff --git a/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyAspect.java b/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyAspect.java
index 9b037195a9..45c25a9a00 100644
--- a/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyAspect.java
+++ b/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyAspect.java
@@ -1,15 +1,34 @@
+/*
+ * Copyright 2002-2023 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package org.springframework.context.event.test.self_inject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
+
import org.springframework.stereotype.Component;
@Aspect
@Component
public class MyAspect {
+
@Before("within(org.springframework.context.event.test.self_inject.MyEventListener)")
public void myAdvice(JoinPoint joinPoint) {
- //System.out.println(joinPoint);
+ // System.out.println(joinPoint);
}
+
}
diff --git a/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyEvent.java b/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyEvent.java
index 2dc0f2bccf..fa426c57c1 100644
--- a/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyEvent.java
+++ b/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyEvent.java
@@ -1,12 +1,31 @@
+/*
+ * Copyright 2002-2023 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package org.springframework.context.event.test.self_inject;
import org.springframework.context.ApplicationEvent;
+@SuppressWarnings("serial")
public class MyEvent extends ApplicationEvent {
+
private String message;
public MyEvent(Object source, String message) {
super(source);
this.message = message;
}
+
}
diff --git a/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyEventListener.java b/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyEventListener.java
index 5fddae77c0..cf0063e778 100644
--- a/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyEventListener.java
+++ b/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyEventListener.java
@@ -1,3 +1,19 @@
+/*
+ * Copyright 2002-2023 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package org.springframework.context.event.test.self_inject;
import org.springframework.beans.factory.annotation.Autowired;
@@ -6,15 +22,15 @@ import org.springframework.stereotype.Component;
@Component
public class MyEventListener implements ApplicationListener {
- public static int eventCount;
- @Autowired // use '-Dspring.main.allow-circular-references=true' in Spring Boot >= 2.6.0
- //@Lazy // with '@Lazy', the problem does not occur
+ public int eventCount;
+
+ @Autowired
private MyEventListener eventDemoListener;
@Override
public void onApplicationEvent(MyEvent event) {
- //System.out.println("Event: " + event);
eventCount++;
}
+
}
diff --git a/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyEventPublisher.java b/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyEventPublisher.java
index 0959134f7c..9063b247ed 100644
--- a/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyEventPublisher.java
+++ b/spring-context/src/test/java/org/springframework/context/event/test/self_inject/MyEventPublisher.java
@@ -1,3 +1,19 @@
+/*
+ * Copyright 2002-2023 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package org.springframework.context.event.test.self_inject;
import org.springframework.beans.factory.annotation.Autowired;
@@ -6,10 +22,12 @@ import org.springframework.stereotype.Component;
@Component
public class MyEventPublisher {
+
@Autowired
private ApplicationEventPublisher eventPublisher;
public void publishMyEvent(String message) {
eventPublisher.publishEvent(new MyEvent(this, message));
}
+
}