Skip to content

Commit 24e033a

Browse files
author
Grzegorz Piwowarek
committed
Add ApplicationListener example
1 parent 28f1fe2 commit 24e033a

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎spring-all/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@
130130
<scope>test</scope>
131131
</dependency>
132132

133+
<dependency>
134+
<groupId>org.assertj</groupId>
135+
<artifactId>assertj-core</artifactId>
136+
<version>3.5.1</version>
137+
<scope>test</scope>
138+
</dependency>
139+
133140
<dependency>
134141
<groupId>org.hamcrest</groupId>
135142
<artifactId>hamcrest-core</artifactId>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.baeldung.startup;
2+
3+
import org.springframework.context.ApplicationListener;
4+
import org.springframework.context.event.ContextRefreshedEvent;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class StartupApplicationListenerExample implements ApplicationListener<ContextRefreshedEvent> {
9+
10+
public static int counter;
11+
12+
@Override
13+
public void onApplicationEvent(ContextRefreshedEvent event) {
14+
counter++;
15+
}
16+
}

‎spring-all/src/test/java/org/baeldung/startup/SpringStartupTest.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.baeldung.startup;
22

3+
import org.assertj.core.api.Assertions;
34
import org.junit.Test;
45
import org.junit.runner.RunWith;
56
import org.springframework.beans.factory.BeanCreationException;
@@ -35,4 +36,9 @@ public void whenConstructorInjection_shouldLogEnv() throws Exception {
3536
public void whenInitializingBean_shouldLogEnv() throws Exception {
3637
ctx.getBean(InitializingBeanExampleBean.class);
3738
}
39+
40+
@Test
41+
public void whenApplicationListener_shouldRunOnce() throws Exception {
42+
Assertions.assertThat(StartupApplicationListenerExample.counter).isEqualTo(1);
43+
}
3844
}

0 commit comments

Comments
 (0)