parent
a7a88371e7
commit
c4ec6aea68
|
@ -295,9 +295,11 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
|
||||||
@Nullable
|
@Nullable
|
||||||
protected Object doInvoke(Object... args) {
|
protected Object doInvoke(Object... args) {
|
||||||
Object bean = getTargetBean();
|
Object bean = getTargetBean();
|
||||||
|
// Detect package-protected NullBean instance through equals(null) check
|
||||||
if (bean.equals(null)) {
|
if (bean.equals(null)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
ReflectionUtils.makeAccessible(this.method);
|
ReflectionUtils.makeAccessible(this.method);
|
||||||
try {
|
try {
|
||||||
return this.method.invoke(bean, args);
|
return this.method.invoke(bean, args);
|
||||||
|
|
|
@ -16,8 +16,6 @@
|
||||||
|
|
||||||
package org.springframework.context.event;
|
package org.springframework.context.event;
|
||||||
|
|
||||||
import java.io.Closeable;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
@ -627,12 +625,13 @@ public class AnnotationDrivenEventListenerTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void missingBeanDoesntCrash() {
|
public void missingListenerBeanIgnored() {
|
||||||
load(MissingEventListener.class);
|
load(MissingEventListener.class);
|
||||||
context.getBean(UseMissingEventListener.class);
|
context.getBean(UseMissingEventListener.class);
|
||||||
context.getBean(ApplicationEventMulticaster.class).multicastEvent(new TestEvent(this));
|
context.getBean(ApplicationEventMulticaster.class).multicastEvent(new TestEvent(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void load(Class<?>... classes) {
|
private void load(Class<?>... classes) {
|
||||||
List<Class<?>> allClasses = new ArrayList<>();
|
List<Class<?>> allClasses = new ArrayList<>();
|
||||||
allClasses.add(BasicConfiguration.class);
|
allClasses.add(BasicConfiguration.class);
|
||||||
|
@ -1087,31 +1086,32 @@ public class AnnotationDrivenEventListenerTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@Import({UseMissingEventListener.class})
|
@Import(UseMissingEventListener.class)
|
||||||
public static class MissingEventListener {
|
public static class MissingEventListener {
|
||||||
@Bean
|
|
||||||
public MyEventListener missing() {
|
@Bean
|
||||||
return null;
|
public MyEventListener missing() {
|
||||||
}
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public static class MyEventListener implements Closeable {
|
public static class MyEventListener {
|
||||||
@EventListener
|
|
||||||
public void hear(TestEvent e) {
|
|
||||||
throw new AssertionError();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@EventListener
|
||||||
public void close() throws IOException {}
|
public void hear(TestEvent e) {
|
||||||
|
throw new AssertionError();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class UseMissingEventListener {
|
public static class UseMissingEventListener {
|
||||||
@Inject
|
|
||||||
public UseMissingEventListener(Optional<MyEventListener> notHere) {
|
@Inject
|
||||||
if (notHere.isPresent()) {
|
public UseMissingEventListener(Optional<MyEventListener> notHere) {
|
||||||
throw new AssertionError();
|
if (notHere.isPresent()) {
|
||||||
}
|
throw new AssertionError();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue