Skip to content

Commit

Permalink
✨ update
Browse files Browse the repository at this point in the history
  • Loading branch information
janlle committed May 26, 2019
1 parent e62e177 commit 7b160f4
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.leone.boot.spring.bootstrap;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
* <p>
*
* @author leone
* @since 2019-05-26
**/
public class SpringApplicationEventBootstrap {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

context.addApplicationListener(event -> {
System.err.println("监听到事件" + event);
});
context.refresh();

context.publishEvent("hello world");

context.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.leone.boot.spring.context;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

/**
* <p>
*
* @author leone
* @since 2019-05-26
**/
@Order(value = Ordered.HIGHEST_PRECEDENCE)
public class FirstApplicationContextInitializer<C extends ConfigurableApplicationContext>
implements ApplicationContextInitializer<C> {

@Override
public void initialize(C application) {
System.out.println("first Application initialize " + application.getId());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.leone.boot.spring.context;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

/**
* <p>
*
* @author leone
* @since 2019-05-26
**/
public class SecondApplicationContextInitializer implements ApplicationContextInitializer, Ordered {

@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
System.out.println("second application initialize " + configurableApplicationContext.getId());
}

@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.leone.boot.spring.listener;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

/**
* <p>
*
* @author leone
* @since 2019-05-26
**/
public class AfterHelloWorldApplicationListener implements ApplicationListener<ContextRefreshedEvent>, Ordered {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
System.out.println("after hello world listener " + event.getApplicationContext().getId() + " timestamp: " + event.getTimestamp());
}

@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.leone.boot.spring.listener;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

/**
* <p>
*
* @author leone
* @since 2019-05-26
**/
@Order(Ordered.HIGHEST_PRECEDENCE)
public class HelloWorldApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
System.out.println("hello world listener " + event.getApplicationContext().getId() + " timestamp: " + event.getTimestamp());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.leone.boot.spring.run;

import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

/**
* <p>
*
* @author leone
* @since 2019-05-26
**/
public class HelloWorldRunListener implements SpringApplicationRunListener {
@Override
public void starting() {
System.out.println("starting...");
}

@Override
public void environmentPrepared(ConfigurableEnvironment environment) {

}

@Override
public void contextPrepared(ConfigurableApplicationContext context) {

}

@Override
public void contextLoaded(ConfigurableApplicationContext context) {

}

@Override
public void started(ConfigurableApplicationContext context) {

}

@Override
public void running(ConfigurableApplicationContext context) {

}

@Override
public void failed(ConfigurableApplicationContext context, Throwable exception) {

}
}
13 changes: 12 additions & 1 deletion spring-boot-spring/src/main/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
org.springframework.context.ApplicationContextInitializer=\
apple
com.leone.boot.spring.context.SecondApplicationContextInitializer,\
com.leone.boot.spring.context.FirstApplicationContextInitializer

org.springframework.context.ApplicationListener=\
com.leone.boot.spring.listener.AfterHelloWorldApplicationListener,\
com.leone.boot.spring.listener.HelloWorldApplicationListener




org.springframework.context.SpringApplicationRunListener=\
com.leone.boot.spring.run.HelloWorldRunListener

0 comments on commit 7b160f4

Please sign in to comment.