SpringCloud系列(一)云原生应用

 一、Bootstrap上下文

SpringCloud系列(一)云原生应用_第1张图片

 

代码说明


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


/**
 * Spring 事件/监听器 Demo
 *
 * @创建人 steffens
 * @创建时间 2019/6/24
 * @描述 文件创建
 */
public class SpringEventListenerDemo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        //增加监听器
        context.addApplicationListener( new MyApplicationListener() );

        //上下文启动
        context.refresh();

        //发布事件
        context.publishEvent( new MyApplicationEvent( "Hello World1~~"));
        context.publishEvent( new MyApplicationEvent( "Hello World2~~"));
        context.publishEvent( new MyApplicationEvent( "Hello World3~~"));
    }

    private static class MyApplicationListener implements ApplicationListener{
        @Override
        public void onApplicationEvent(MyApplicationEvent event) {
            System.out.printf("MyApplicationListener receive, event : %s \n", event.getSource());
        }
    }
    private static class MyApplicationEvent extends ApplicationEvent {

        /**
         * Create a new ApplicationEvent.
         *
         * @param source the object on which the event initially occurred (never {@code null})
         */
        public MyApplicationEvent(Object source) {
            super(source);
        }
    }
}

二、 应用启动SpringApplication.run

例如args和程序参数

SpringCloud系列(一)云原生应用_第2张图片

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringCloudLesson1Application {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudLesson1Application.class, args);
	}

}

 

 

 三、修改成非web程序,注意启动注解 ( AnnotationConfigApplicationContext )

SpringCloud系列(一)云原生应用_第3张图片

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringCloudLesson1Application {

	public static void main(String[] args) {
		//SpringApplication.run(SpringCloudLesson1Application.class, args);
		//方式二
		SpringApplication springApplication = new SpringApplication(SpringCloudLesson1Application.class);
		//修改成非web程序
		springApplication.setWebEnvironment(false);
		springApplication.run(args);
	}

}

 

SpringCloud系列(一)云原生应用_第4张图片

 

 

 

你可能感兴趣的:(SpringCloud系列(一)云原生应用)