• Spring Boot 2.0 教程


    原文连接:https://www.codemore.top/cates/Backend/post/2018-05-20/spring-boot-SpringApplication

    可以通过SpringApplication.run() 方法轻松的启动一个Spring应用,例如

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

    运行结果

      .   ____          _            __ _ _
     /\ / ___'_ __ _ _(_)_ __  __ _    
    ( ( )\___ | '_ | '_| | '_ / _` |    
     \/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::   v2.0.1.RELEASE
    
    2013-07-31 00:08:16.117  INFO 56603 --- [           main] o.s.b.s.app.SampleApplication            : Starting SampleApplication v0.1.0 on mycomputer with PID 56603 (/apps/myapp.jar started by pwebb)
    2013-07-31 00:08:16.166  INFO 56603 --- [           main] ationConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6e5a8246: startup date [Wed Jul 31 00:08:16 PDT 2013]; root of context hierarchy
    2014-03-04 13:09:54.912  INFO 41370 --- [           main] .t.TomcatServletWebServerFactory : Server initialized with port: 8080
    2014-03-04 13:09:56.501  INFO 41370 --- [           main] o.s.b.s.app.SampleApplication            : Started SampleApplication in 2.992 seconds (JVM running for 3.658)

    默认的Log级别是INFO级别.

    定制banner

    banner是在Spring应用启动的时候打印的,如果需要定制banner,可以添加banner.txt到classpath中,或者设置spring.banner.location属性设置banner的路径。banner默认编码方式是UTF-8,如果不使用UTF-8,可以设置spring.banner.charset属性设置其编码方式。除了文本文件,也可以使用,banner.gif,banner.jpeg,banner.png等图片文件作为banner,或者设置spring.banner.image.location属性设置图片banner。图片有Spring转为ASCII输出。 banner.txt 中添加如下占位符

    • ${application.version} 在MANIFEST.MF中定义的版本号,例如Implementation-Version: 1.0则版本号为 1.0
    • ${application.formatted-version} 版本号格式化加V 例如 v1.0
    • ${spring-boot.version} 使用的Spring Boot的版本,例如 2.0.1.RELEASE
    • ${spring-boot.formatted-version} 版本号加V,例如v2.0.1.RELEASE
    • ${Ansi.NAME},${AnsiColor.NAME} ,${AnsiBackground.NAME},${AnsiStyle.NAME} ANSI escape code名
    • ${application.title} MANIFEST.MF 定义的应用名,例如Implemention-Title: MyApp取MyApp

    也可以实现org.springframework.boot.Banner接口的printBanner()方法定制Banner,使用SpringApplication.setBanner()设置banner。 配置spring.main.banner-mode设置是否在控制台显示banner

    定制SpringApplication

    可以通过创建自定义的SpringApplication示例来定制SpringApplication,例如

    public static void main(String[] args) {
    	SpringApplication app = new SpringApplication(MySpringConfiguration.class);
    	app.setBannerMode(Banner.Mode.OFF);
    	app.run(args);
    }

    使用builder定制SpringApplication

    new SpringApplicationBuilder()
    		.sources(Parent.class)
    		.child(Application.class)
    		.bannerMode(Banner.Mode.OFF)
    		.run(args);
    应用事件和监听器

    除了通常的Spring Framework的事件,例如ContextRefreshedEvent等,SpringApplication还会发送一些其他的应用时间。 由于一些事件是在ApplicationContext创建之前触发的,因此无法通过@Bean的方式注册这些事件的监听器。可以使用SpringApplication.addListeners() 犯法或者SpringApplicationBuilder.listeners()方法注册监听器。 也可以通过添加META-INF/spring.factories文件到项目中,自动注册监听器,例如org.springframework.context.ApplicationListener=come.example.project.MyListener 应用启动后,应用时间按照如下的顺序触发 1. 应用开始运行时触发ApplicationStartingEvent事件 2. 在ApplicationContext创建之前,Environment可用时触发ApplicationEnvironmentPreparedEvent事件。 3. bean定义加载后,刷新之前触发ApplicationPreparedEvent事件 4. context刷新后,command-line runner运行前,触发ApplicationStartedEvent 5. command-lie runner运行后,触发ApplicationReadyEvent表示应用可以接受请求了 6. 如果发生异常,触发ApplicationFailedEvent 应用事件时通过Spring Framework的事件发布机制发送的,这种机制保证了发送给子context的事件同样也会发送给其祖先context。因此如果ApplicationContext时有层次的,可能会收到多个相同的事件,为了区分这些事件,需要应用注入自己的ApplicationContext,当接收事件时判断是否时本层事件再做处理,可以用过继承ApplicationContextAware注入ApplicationContext,或者如果监听器时一个bean也可以用过@Autowired注入ApplicationContext。

    Web环境

    SpringApplication可以根据配置创建正确的ApplicationContext

    • 如果提供了Spring MVC,则使用AnnotationConfigServletWebServerApplicationContext
    • 如果Spring MVC未提供,而提供了WebFlux 则使用AnnotationConfigReactiveWebApplicationContext
    • 如果都没提供则使用AnnotationConfigApplicationContext

    同样也可以使用setApplication(...)完全自己设置ApplicationContext。

    访问应用参数

    如果需要访问SpringApplication.run(...)中的args参数,可以通过注入org.springframework.boot.ApplicationArguments bean访问。接口ApplicationArguments不仅提供了访问原生参数的String[] ,同样也提供了 option和non-option参数例如

    import org.springframework.boot.*
    import org.springframework.beans.factory.annotation.*
    import org.springframework.stereotype.*
    
    @Component
    public class MyBean {
    
    	@Autowired
    	public MyBean(ApplicationArguments args) {
    		boolean debug = args.containsOption("debug");
    		List<String> files = args.getNonOptionArgs();
    		// if run with "--debug logfile.txt" debug=true, files=["logfile.txt"]
    	}
    
    }
    使用ApplicationRunner和CommandLineRunner

    如果在运行完SpringApplication.run()之后需要运行其他代码,可以通过实现ApplicationRunner或者CommandLineRunner接口,这俩接口作用相同,都提供一个run()的方法,等调用完SpringApplication.run()之后调用,例如:

    import org.springframework.boot.*
    import org.springframework.stereotype.*
    
    @Component
    public class MyBean implements CommandLineRunner {
    
    	public void run(String... args) {
    		// Do something...
    	}
    
    }

    如果定义了多个CommandLineRunner或者ApplicationRunner,则可以通过接口org.springframework.core.Ordered或者org.springframework.core.annotation.Order注解提供顺序。

    应用退出

    每一个SpringApplication都会注册一个JVM退出的钩子,保证ApplicationContext可以优雅关闭。另外bean可以实现org.springfamework.boot.ExitCodeGenerator接口当调用SpringApplication.exit()时返回一个特定的返回码,这个返回码可以传递给System.exit()例如:

    @SpringBootApplication
    public class ExitCodeApplication {
    
    	@Bean
    	public ExitCodeGenerator exitCodeGenerator() {
    		return () -> 42;
    	}
    
    	public static void main(String[] args) {
    		System.exit(SpringApplication
    				.exit(SpringApplication.run(ExitCodeApplication.class, args)));
    	}
    
    }


  • 相关阅读:
    面向对象、构造函数的区别
    写一个function,清除字符串前后的空格。(兼容所有浏览器)
    两个DIV高度自适应方法(左右两个DIV高度一样)
    js数组去重
    input框处理删除小图标的功能
    查找显示高亮
    JSON.parse()和JSON.stringify()
    jquery封装
    怎么理解HTML语义化
    html5语义化标签
  • 原文地址:https://www.cnblogs.com/hitandrew/p/9062124.html
Copyright © 2020-2023  润新知