spring-boot 事件监听 ApplicationListener

介绍

SpringBoot 为 ApplicationContextEvent 提供了四种事件:

  • ApplicationStartedEvent :spring boot 启动开始时执行的事件
  • ApplicationEnvironmentPreparedEvent:spring boot 对应 Enviroment 已经准备完毕,但此时上下文 context 还没有创建
  • ApplicationPreparedEvent:spring boot 上下文 context 创建完成,但此时 spring 中的 bean 是没有完全加载完成的
  • ApplicationFailedEvent:spring boot启动异常时执行事件

ApplicationEnvironmentPreparedEvent事件监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.ConfigurableEnvironment;

/**
* 环境配置事件监听器
*
* @author Created by YL on 2018/7/5
*/
@Slf4j
public class EnvironmentPreparedEventListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {

@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment();
// 获取 spring.profiles.active 的值
String activeProfile = environment.getActiveProfiles()[0];
log.info("激活环境 ---> activeProfile: {}", activeProfile);
}
}

这里要注意注册ApplicationEnvironmentPreparedEvent监听器的方式

如果是 jar 环境部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

import java.util.Collections;

/**
* @author Created by YL on 2018/7/5
*/
@SpringBootApplication
public class App {

/**
* Common
*/
private static SpringApplicationBuilder configureSpringBuilder(SpringApplicationBuilder builder) {
builder.listeners(new EnvironmentPreparedEventListener());
return builder.sources(App.class);
}

/**
* for JAR deploy
*/
public static void main(String[] args) {
configureSpringBuilder(new SpringApplicationBuilder())
.run(args);
synchronized (App.class) {
while (true) {
try {
App.class.wait();
} catch (Exception e) {
e.printStackTrace();
break;
}
}
}
}
}

或者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

import java.util.Collections;

/**
* @author Created by YL on 2018/7/5
*/
@SpringBootApplication
public class App {

/**
* for JAR deploy
*/
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.addListeners(new EnvironmentPreparedEventListener());
app.run(args);
synchronized (App.class) {
while (true) {
try {
App.class.wait();
} catch (Exception e) {
e.printStackTrace();
break;
}
}
}
}
}

如果是 war 环境部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

import java.util.Collections;

/**
* @author Created by YL on 2018/7/2
*/
@SpringBootApplication
public class App extends SpringBootServletInitializer {

/**
* Common
*/
private static SpringApplicationBuilder configureSpringBuilder(SpringApplicationBuilder builder) {
builder.listeners(new EnvironmentPreparedEventListener());
return builder.sources(App.class);
}

/**
* for WAR deploy
*/
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return configureSpringBuilder(builder);
}

/**
* for JAR deploy
*/
public static void main(String[] args) {
configureSpringBuilder(new SpringApplicationBuilder())
.run(args);
synchronized (App.class) {
while (true) {
try {
App.class.wait();
} catch (Exception e) {
e.printStackTrace();
break;
}
}
}
}
}

Junit 测试

测试单元下,自定义事件不生效的问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = App.class)
@ContextConfiguration(loader = IAmTest.CustomLoader.class)
@ActiveProfiles("local")
// 单元测试配置数据库默认会事务会退,此时强制事务提交
// @Rollback(false)
@Slf4j
public class IAmTest {

public static class CustomLoader extends SpringBootContextLoader {
@Override
protected SpringApplication getSpringApplication() {
SpringApplication app = super.getSpringApplication();
app.addListeners(new EnvironmentPreparedEventListener());
return app;
}
}

@Resource
private TestService testService;

@Test
public void get() {
JSONObject json = testService.get("name", "200~180****2307~01~~");
System.out.println(json);
}
}
  • 本文作者: forever杨
  • 本文链接: https://blog.yl-online.top/posts/ba3065f8.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。如果文章内容对你有用,请记录到你的笔记中。本博客站点随时会停止服务,请不要收藏、转载!