第一节 云原生应用(Cloud Native Applications) (课件) [问答]
package com.ikadmin.sr.springcloudlesson1.demo;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
public class SpringApplicationDemo {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DefaultApplicationListener.class);
// context.addApplicationListener(new DefaultApplicationListener());
// context.register(DefaultApplicationListener.class);
// context.refresh();
context.publishEvent(new DefaultApplicationEvent(new Double(1.0)));
context.publishEvent(new DefaultApplicationEvent("test"));
context.publishEvent(new DefaultApplicationEvent(1));
// context.publishEvent(new DefaultApplicationEvent(null));
}
@Component
private static class DefaultApplicationListener implements ApplicationListener<DefaultApplicationEvent> {
@Override
public void onApplicationEvent(DefaultApplicationEvent event) {
System.err.printf("DefaultApplicationEvent source: %s \n", event.getSource());