Skip to content

Commit 8e6fff4

Browse files
naXa777Eugen
authored andcommitted
BAEL-1596 Spring Events (eugenp#5605)
* Annotation-driven event listener + generic events * update examples + add tests * add article link * add more tests for Spring Events
1 parent 064113f commit 8e6fff4

12 files changed

Lines changed: 214 additions & 5 deletions

‎spring-all/README.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
This project is used to replicate Spring Exceptions only.
66

7-
###The Course
7+
### The Course
8+
89
The "REST With Spring" Classes: http://bit.ly/restwithspring
9-
10-
### Relevant articles:
10+
11+
### Relevant articles:
12+
1113
- [Guide to Spring @Autowired](http://www.baeldung.com/spring-autowire)
1214
- [Properties with Spring](http://www.baeldung.com/properties-with-spring) - checkout the `org.baeldung.properties` package for all scenarios of properties injection and usage
1315
- [Spring Profiles](http://www.baeldung.com/spring-profiles)
@@ -30,3 +32,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
3032
- [Spring Web Contexts](http://www.baeldung.com/spring-web-contexts)
3133
- [Spring Cache – Creating a Custom KeyGenerator](http://www.baeldung.com/spring-cache-custom-keygenerator)
3234
- [Spring @Primary Annotation](http://www.baeldung.com/spring-primary)
35+
- [Spring Events](https://www.baeldung.com/spring-events)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.baeldung.springevents.synchronous;
2+
3+
import org.springframework.context.event.ContextStartedEvent;
4+
import org.springframework.context.event.EventListener;
5+
import org.springframework.stereotype.Component;
6+
import org.springframework.transaction.event.TransactionPhase;
7+
import org.springframework.transaction.event.TransactionalEventListener;
8+
9+
@Component
10+
public class AnnotationDrivenEventListener {
11+
12+
// for tests
13+
private boolean hitContextStartedHandler = false;
14+
private boolean hitSuccessfulEventHandler = false;
15+
private boolean hitCustomEventHandler = false;
16+
17+
@EventListener
18+
public void handleContextStart(final ContextStartedEvent cse) {
19+
System.out.println("Handling context started event.");
20+
hitContextStartedHandler = true;
21+
}
22+
23+
@EventListener(condition = "#event.success")
24+
public void handleSuccessful(final GenericSpringEvent<String> event) {
25+
System.out.println("Handling generic event (conditional): " + event.getWhat());
26+
hitSuccessfulEventHandler = true;
27+
}
28+
29+
@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
30+
public void handleCustom(final CustomSpringEvent event) {
31+
System.out.println("Handling event inside a transaction BEFORE COMMIT.");
32+
hitCustomEventHandler = true;
33+
}
34+
35+
boolean isHitContextStartedHandler() {
36+
return hitContextStartedHandler;
37+
}
38+
39+
boolean isHitSuccessfulEventHandler() {
40+
return hitSuccessfulEventHandler;
41+
}
42+
43+
boolean isHitCustomEventHandler() {
44+
return hitCustomEventHandler;
45+
}
46+
}

‎spring-all/src/main/java/org/baeldung/springevents/synchronous/ContextRefreshedListener.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@
77
@Component
88
public class ContextRefreshedListener implements ApplicationListener<ContextRefreshedEvent> {
99

10+
// for tests
11+
private boolean hitContextRefreshedHandler = false;
12+
1013
@Override
1114
public void onApplicationEvent(final ContextRefreshedEvent cse) {
1215
System.out.println("Handling context re-freshed event. ");
16+
hitContextRefreshedHandler = true;
1317
}
1418

19+
boolean isHitContextRefreshedHandler() {
20+
return hitContextRefreshedHandler;
21+
}
1522
}

‎spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventPublisher.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,16 @@ public void publishEvent(final String message) {
1616
applicationEventPublisher.publishEvent(customSpringEvent);
1717
}
1818

19+
public void publishGenericEvent(final String message, boolean success) {
20+
System.out.println("Publishing generic event.");
21+
final GenericSpringEvent<String> genericSpringEvent = new GenericStringSpringEvent(message, success);
22+
applicationEventPublisher.publishEvent(genericSpringEvent);
23+
}
24+
25+
public void publishGenericAppEvent(final String message) {
26+
System.out.println("Publishing generic event.");
27+
final GenericSpringAppEvent<String> genericSpringEvent = new GenericStringSpringAppEvent(this, message);
28+
applicationEventPublisher.publishEvent(genericSpringEvent);
29+
}
30+
1931
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.baeldung.springevents.synchronous;
2+
3+
import org.springframework.context.ApplicationEvent;
4+
5+
public class GenericSpringAppEvent<T> extends ApplicationEvent {
6+
7+
private final T what;
8+
9+
public GenericSpringAppEvent(final Object source, final T what) {
10+
super(source);
11+
this.what = what;
12+
}
13+
14+
public T getWhat() {
15+
return what;
16+
}
17+
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.baeldung.springevents.synchronous;
2+
3+
public class GenericSpringEvent<T> {
4+
5+
private final T what;
6+
protected final boolean success;
7+
8+
public GenericSpringEvent(final T what, final boolean success) {
9+
this.what = what;
10+
this.success = success;
11+
}
12+
13+
public T getWhat() {
14+
return what;
15+
}
16+
17+
public boolean isSuccess() {
18+
return success;
19+
}
20+
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.baeldung.springevents.synchronous;
2+
3+
import org.springframework.context.ApplicationListener;
4+
import org.springframework.lang.NonNull;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class GenericSpringEventListener implements ApplicationListener<GenericSpringAppEvent<String>> {
9+
10+
// for testing
11+
private boolean hitEventHandler = false;
12+
13+
@Override
14+
public void onApplicationEvent(@NonNull final GenericSpringAppEvent<String> event) {
15+
System.out.println("Received spring generic event - " + event.getWhat());
16+
hitEventHandler = true;
17+
}
18+
19+
boolean isHitEventHandler() {
20+
return hitEventHandler;
21+
}
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.baeldung.springevents.synchronous;
2+
3+
class GenericStringSpringAppEvent extends GenericSpringAppEvent<String> {
4+
5+
GenericStringSpringAppEvent(final Object source, final String what) {
6+
super(source, what);
7+
}
8+
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.baeldung.springevents.synchronous;
2+
3+
public class GenericStringSpringEvent extends GenericSpringEvent<String> {
4+
5+
GenericStringSpringEvent(final String what, final boolean success) {
6+
super(what, success);
7+
}
8+
9+
}

‎spring-all/src/test/java/org/baeldung/springevents/synchronous/ContextRefreshedListenerIntegrationTest.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33
import org.baeldung.springevents.synchronous.SynchronousSpringEventsConfig;
44
import org.junit.Test;
55
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.test.context.ContextConfiguration;
78
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
89
import org.springframework.test.context.support.AnnotationConfigContextLoader;
910

11+
import static org.springframework.util.Assert.isTrue;
12+
1013
@RunWith(SpringJUnit4ClassRunner.class)
1114
@ContextConfiguration(classes = { SynchronousSpringEventsConfig.class }, loader = AnnotationConfigContextLoader.class)
1215
public class ContextRefreshedListenerIntegrationTest {
1316

17+
@Autowired
18+
private ContextRefreshedListener listener;
19+
1420
@Test
15-
public void testContextRefreshedListener() throws InterruptedException {
21+
public void testContextRefreshedListener() {
1622
System.out.println("Test context re-freshed listener.");
23+
isTrue(listener.isHitContextRefreshedHandler(), "Refresh should be called once");
1724
}
1825
}

0 commit comments

Comments
 (0)