释放双眼,带上耳机,听听看~!
原文地址:SpringBoot-事件监听的4种实现方式
方式1示例补充
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 1package com.cloud;
2
3import org.slf4j.Logger;
4import org.slf4j.LoggerFactory;
5import org.springframework.boot.SpringApplication;
6import org.springframework.boot.autoconfigure.SpringBootApplication;
7import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
8
9/**
10 * 该类必须放在一级目录下, 否则会报错
11 * @author Administrator
12 */
13@EnableEurekaClient // Eureka微服务注解
14@SpringBootApplication
15public class SleuthZipkinApplication {
16 private static Logger log = LoggerFactory.getLogger(SleuthZipkinApplication.class);
17
18 public static void main(String[] args) {
19 SpringApplication app = new SpringApplication(SleuthZipkinApplication.class);
20 MyListener1 asel = new MyListener1();
21 app.addListeners(asel);
22 app.run(args);
23
24 log.info("log4j2 start!");
25 }
26}
27
方式2示例补充.
创建MyListener2类,并使用@Component注解将该类装载入spring容器中
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 1package com.cloud.config;
2
3import org.apache.commons.lang.StringUtils;
4import org.jboss.logging.MDC;
5import org.springframework.beans.factory.annotation.Autowired;
6import org.springframework.boot.SpringApplication;
7import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
8import org.springframework.boot.context.event.ApplicationFailedEvent;
9import org.springframework.boot.context.event.ApplicationPreparedEvent;
10import org.springframework.boot.context.event.ApplicationStartingEvent;
11import org.springframework.context.ApplicationContext;
12import org.springframework.context.ApplicationEvent;
13import org.springframework.context.event.ContextClosedEvent;
14import org.springframework.context.event.GenericApplicationListener;
15import org.springframework.core.Ordered;
16import org.springframework.core.ResolvableType;
17import org.springframework.core.env.Environment;
18import org.springframework.stereotype.Component;
19
20/**
21 * @author langpf 2019/1/2
22 */
23@Component
24public class ApplicationStartedEventListener implements GenericApplicationListener {
25 private static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;
26
27 private static Class<?>[] EVENT_TYPES = {ApplicationStartingEvent.class,
28 ApplicationEnvironmentPreparedEvent.class, ApplicationPreparedEvent.class,
29 ContextClosedEvent.class, ApplicationFailedEvent.class};
30
31 private static Class<?>[] SOURCE_TYPES = {SpringApplication.class,
32 ApplicationContext.class};
33
34 @Autowired
35 private Environment env;
36
37 @Override
38 public void onApplicationEvent(ApplicationEvent event) {
39 String appName = env.getProperty("spring.application.name");
40 if (StringUtils.isNotBlank(appName)) {
41 MDC.put("appName", appName);
42 }
43 }
44
45 /*
46 * (non-Javadoc)
47 *
48 * @see org.springframework.core.Ordered#getOrder()
49 */
50 @Override
51 public int getOrder() {
52 // TODO Auto-generated method stub
53 return DEFAULT_ORDER;
54 }
55
56 /*
57 * (non-Javadoc)
58 *
59 * @see org.springframework.context.event.GenericApplicationListener#
60 * supportsEventType(org.springframework.core.ResolvableType)
61 */
62 @Override
63 public boolean supportsEventType(ResolvableType resolvableType) {
64 return isAssignableFrom(resolvableType.getRawClass(), EVENT_TYPES);
65 }
66
67 @Override
68 public boolean supportsSourceType(Class<?> sourceType) {
69 return isAssignableFrom(sourceType, SOURCE_TYPES);
70 }
71
72 private boolean isAssignableFrom(Class<?> type, Class<?>... supportedTypes) {
73 if (type != null) {
74 for (Class<?> supportedType : supportedTypes) {
75 if (supportedType.isAssignableFrom(type)) {
76 return true;
77 }
78 }
79 }
80 return false;
81 }
82}
83
84