Browse Source
ApplicationContextEventTests.eventForSelfInjectedProxiedListenerFiredOnlyOnce relates to and reproduces #28283.pull/31407/head
Alexander Kriegisch
1 year ago
committed by
Juergen Hoeller
6 changed files with 102 additions and 0 deletions
@ -0,0 +1,17 @@
@@ -0,0 +1,17 @@
|
||||
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"; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,15 @@
@@ -0,0 +1,15 @@
|
||||
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);
|
||||
} |
||||
} |
@ -0,0 +1,12 @@
@@ -0,0 +1,12 @@
|
||||
package org.springframework.context.event.test.self_inject; |
||||
|
||||
import org.springframework.context.ApplicationEvent; |
||||
|
||||
public class MyEvent extends ApplicationEvent { |
||||
private String message; |
||||
|
||||
public MyEvent(Object source, String message) { |
||||
super(source); |
||||
this.message = message; |
||||
} |
||||
} |
@ -0,0 +1,20 @@
@@ -0,0 +1,20 @@
|
||||
package org.springframework.context.event.test.self_inject; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.context.ApplicationListener; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
@Component |
||||
public class MyEventListener implements ApplicationListener<MyEvent> { |
||||
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
|
||||
private MyEventListener eventDemoListener; |
||||
|
||||
@Override |
||||
public void onApplicationEvent(MyEvent event) { |
||||
//System.out.println("Event: " + event);
|
||||
eventCount++; |
||||
} |
||||
} |
@ -0,0 +1,15 @@
@@ -0,0 +1,15 @@
|
||||
package org.springframework.context.event.test.self_inject; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.context.ApplicationEventPublisher; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
@Component |
||||
public class MyEventPublisher { |
||||
@Autowired |
||||
private ApplicationEventPublisher eventPublisher; |
||||
|
||||
public void publishMyEvent(String message) { |
||||
eventPublisher.publishEvent(new MyEvent(this, message)); |
||||
} |
||||
} |
Loading…
Reference in new issue