Skip to content

Commit 7b160f4

Browse files
committed
✨ update
1 parent e62e177 commit 7b160f4

File tree

7 files changed

+178
-1
lines changed

7 files changed

+178
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.leone.boot.spring.bootstrap;
2+
3+
import org.springframework.context.ApplicationEvent;
4+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5+
6+
/**
7+
* <p>
8+
*
9+
* @author leone
10+
* @since 2019-05-26
11+
**/
12+
public class SpringApplicationEventBootstrap {
13+
public static void main(String[] args) {
14+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
15+
16+
context.addApplicationListener(event -> {
17+
System.err.println("监听到事件" + event);
18+
});
19+
context.refresh();
20+
21+
context.publishEvent("hello world");
22+
23+
context.close();
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.leone.boot.spring.context;
2+
3+
import org.springframework.context.ApplicationContextInitializer;
4+
import org.springframework.context.ConfigurableApplicationContext;
5+
import org.springframework.core.Ordered;
6+
import org.springframework.core.annotation.Order;
7+
8+
/**
9+
* <p>
10+
*
11+
* @author leone
12+
* @since 2019-05-26
13+
**/
14+
@Order(value = Ordered.HIGHEST_PRECEDENCE)
15+
public class FirstApplicationContextInitializer<C extends ConfigurableApplicationContext>
16+
implements ApplicationContextInitializer<C> {
17+
18+
@Override
19+
public void initialize(C application) {
20+
System.out.println("first Application initialize " + application.getId());
21+
}
22+
23+
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.leone.boot.spring.context;
2+
3+
import org.springframework.context.ApplicationContextInitializer;
4+
import org.springframework.context.ConfigurableApplicationContext;
5+
import org.springframework.core.Ordered;
6+
import org.springframework.core.annotation.Order;
7+
8+
/**
9+
* <p>
10+
*
11+
* @author leone
12+
* @since 2019-05-26
13+
**/
14+
public class SecondApplicationContextInitializer implements ApplicationContextInitializer, Ordered {
15+
16+
@Override
17+
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
18+
System.out.println("second application initialize " + configurableApplicationContext.getId());
19+
}
20+
21+
@Override
22+
public int getOrder() {
23+
return Ordered.LOWEST_PRECEDENCE;
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.leone.boot.spring.listener;
2+
3+
import org.springframework.context.ApplicationListener;
4+
import org.springframework.context.event.ContextRefreshedEvent;
5+
import org.springframework.core.Ordered;
6+
import org.springframework.core.annotation.Order;
7+
8+
/**
9+
* <p>
10+
*
11+
* @author leone
12+
* @since 2019-05-26
13+
**/
14+
public class AfterHelloWorldApplicationListener implements ApplicationListener<ContextRefreshedEvent>, Ordered {
15+
@Override
16+
public void onApplicationEvent(ContextRefreshedEvent event) {
17+
System.out.println("after hello world listener " + event.getApplicationContext().getId() + " timestamp: " + event.getTimestamp());
18+
}
19+
20+
@Override
21+
public int getOrder() {
22+
return Ordered.LOWEST_PRECEDENCE;
23+
}
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.leone.boot.spring.listener;
2+
3+
import org.springframework.context.ApplicationListener;
4+
import org.springframework.context.event.ContextRefreshedEvent;
5+
import org.springframework.core.Ordered;
6+
import org.springframework.core.annotation.Order;
7+
8+
/**
9+
* <p>
10+
*
11+
* @author leone
12+
* @since 2019-05-26
13+
**/
14+
@Order(Ordered.HIGHEST_PRECEDENCE)
15+
public class HelloWorldApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
16+
@Override
17+
public void onApplicationEvent(ContextRefreshedEvent event) {
18+
System.out.println("hello world listener " + event.getApplicationContext().getId() + " timestamp: " + event.getTimestamp());
19+
}
20+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.leone.boot.spring.run;
2+
3+
import org.springframework.boot.SpringApplicationRunListener;
4+
import org.springframework.context.ConfigurableApplicationContext;
5+
import org.springframework.core.env.ConfigurableEnvironment;
6+
7+
/**
8+
* <p>
9+
*
10+
* @author leone
11+
* @since 2019-05-26
12+
**/
13+
public class HelloWorldRunListener implements SpringApplicationRunListener {
14+
@Override
15+
public void starting() {
16+
System.out.println("starting...");
17+
}
18+
19+
@Override
20+
public void environmentPrepared(ConfigurableEnvironment environment) {
21+
22+
}
23+
24+
@Override
25+
public void contextPrepared(ConfigurableApplicationContext context) {
26+
27+
}
28+
29+
@Override
30+
public void contextLoaded(ConfigurableApplicationContext context) {
31+
32+
}
33+
34+
@Override
35+
public void started(ConfigurableApplicationContext context) {
36+
37+
}
38+
39+
@Override
40+
public void running(ConfigurableApplicationContext context) {
41+
42+
}
43+
44+
@Override
45+
public void failed(ConfigurableApplicationContext context, Throwable exception) {
46+
47+
}
48+
}
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
11
org.springframework.context.ApplicationContextInitializer=\
2-
apple
2+
com.leone.boot.spring.context.SecondApplicationContextInitializer,\
3+
com.leone.boot.spring.context.FirstApplicationContextInitializer
4+
5+
org.springframework.context.ApplicationListener=\
6+
com.leone.boot.spring.listener.AfterHelloWorldApplicationListener,\
7+
com.leone.boot.spring.listener.HelloWorldApplicationListener
8+
9+
10+
11+
12+
org.springframework.context.SpringApplicationRunListener=\
13+
com.leone.boot.spring.run.HelloWorldRunListener

0 commit comments

Comments
 (0)