Skip to content

Commit 60de42b

Browse files
committed
GH-518 - Support for conditions on @ApplicationModuleListener.
1 parent 3d68f1a commit 60de42b

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

spring-modulith-events/spring-modulith-events-api/src/main/java/org/springframework/modulith/events/ApplicationModuleListener.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,14 @@
6666
*/
6767
@AliasFor(annotation = EventListener.class, attribute = "id")
6868
String id() default "";
69+
70+
/**
71+
* Spring Expression Language (SpEL) attribute used for making the event handling conditional. The default is
72+
* {@code ""}, meaning the event is always handled.
73+
*
74+
* @since 1.2
75+
* @see EventListener#condition
76+
*/
77+
@AliasFor(annotation = EventListener.class, attribute = "condition")
78+
String condition() default "";
6979
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2024 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+
package org.springframework.modulith.events;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.lang.reflect.Method;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.springframework.context.event.ApplicationListenerMethodAdapter;
24+
import org.springframework.context.event.EventListener;
25+
import org.springframework.core.annotation.AnnotatedElementUtils;
26+
27+
/**
28+
* Unit tests for {@link ApplicationModuleListener}.
29+
*
30+
* @author Oliver Drotbohm
31+
*/
32+
class ApplicationModuleListenerUnitTests {
33+
34+
@Test // GH-518
35+
void exposesEventListenerCondition() throws Exception {
36+
37+
var method = Sample.class.getDeclaredMethod("someMethod", Object.class);
38+
var annotation = AnnotatedElementUtils.findMergedAnnotation(method, EventListener.class);
39+
40+
assertThat(annotation.condition()).isEqualTo("#{false}");
41+
}
42+
43+
@Test // GH-518
44+
void exposesConditionToAdapter() throws Exception {
45+
46+
var method = Sample.class.getDeclaredMethod("someMethod", Object.class);
47+
48+
var adapter = new CustomAdapter("someName", Sample.class, method) {};
49+
50+
assertThat(adapter.getCondition()).isEqualTo("#{false}");
51+
}
52+
53+
static class Sample {
54+
55+
@ApplicationModuleListener(condition = "#{false}")
56+
void someMethod(Object event) {}
57+
}
58+
59+
static class CustomAdapter extends ApplicationListenerMethodAdapter {
60+
61+
public CustomAdapter(String beanName, Class<?> targetClass, Method method) {
62+
super(beanName, targetClass, method);
63+
}
64+
65+
/*
66+
* (non-Javadoc)
67+
* @see org.springframework.context.event.ApplicationListenerMethodAdapter#getCondition()
68+
*/
69+
@Override
70+
public String getCondition() {
71+
return super.getCondition();
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)