java.lang.noclassdeffounderror,SpringBoot2.1.5(13)--- SpringBoot 特性下

 2023-09-26 阅读 27 评论 0

摘要:SpringBoot客制化 如果SpringApplication的默认值不符合您的口味,那么可以创建一个本地实例并定制它。例如,要关闭banner,可以编写: public static void main(String[] args) {SpringApplication app = new SpringApplication(MySpringConfig

SpringBoot客制化

如果SpringApplication的默认值不符合您的口味,那么可以创建一个本地实例并定制它。例如,要关闭banner,可以编写:

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


Fluent Builder API

如果需要构建ApplicationContext层次结构(具有父级的多个上下文/或者,如果您更喜欢使用“流利的”构建器API,则可以使用

java.lang.noclassdeffounderror。SpringApplicationBuilder。

SpringApplicationBuilder允许您将多个方法调用链接在一起,并包含父级以及允许您创建层次结构的子方法,如下面的示例所示:

new SpringApplicationBuilder().sources(Parent.class).child(Application.class).bannerMode(Banner.Mode.OFF).run(args);

应用事件及监听器

除了通常的Spring框架事件(如ContextRefreshedEvent)之外,还有SpringApplication发送一些附加的应用程序事件。

一、自定义监听器:

java.lang.throwable、1、创建:

    META-INF/spring.factories

2、添加:

org.springframework.context.ApplicationListener=com.example.project.MyListener

二、应用程序事件发送顺序如下

Application events are sent in the following order, as your application runs:1、An ApplicationStartedEvent is sent at the start of a run, but before any processing except the registration of listeners and initializers.ApplicationStartedEvent在任何处理之前,程序开始运行时被发送,初始化和自定义注册监听事件除外2、An ApplicationEnvironmentPreparedEvent is sent when the Environment to be used in the context is known, but before the context is created.ApplicationEnvironmentPreparedEvent在上下文被创建之前,应用环境被已知的上下文环境中使用时被发送3、An ApplicationPreparedEvent is sent just before the refresh is started, but after bean definitions have been loaded.ApplicationPreparedEvent在刷新开始之前,beans加载之后被发送4、An ApplicationReadyEvent is sent after the refresh and any related callbacks have been processed to indicate the application is ready to service requests.ApplicationReadyEvent在刷新后被发送,并且任何相关的回调都已经被处理,表明该应用程序已经准备好处理服务请求5、An ApplicationFailedEvent is sent if there is an exception on startup.ApplicationFailedEvent如果启动时存在异常时被发送You often won’t need to use application events, but it can be handy to know that they exist. Internally, Spring Boot uses events to handle a variety of tasks.我们不需要使用应用程序事件,但是很方便的知道他们存在,在SpringBoot内部使用各种事件来处理各种

应用程序事件通过使用Spring框架的事件发布机制发送。其中一部分机制确保在子上下文中发布给侦听器的事件也被发布

任何祖先上下文中的侦听器。因此,如果应用程序使用层次结构在SpringApplication实例中,侦听器可以接收同一类型的多个实例应用程序事件。

Spring Framework。允许侦听器区分上下文中的事件和后代上下文,它应该请求注入其应用程序上下文,然后比较与事件上下文一起注入的上下文。上下文可以通过实现applicationContextAware,如果侦听器是bean,则使用@autowired。

WEB 环境

SpringApplication试图代表您创建正确类型的ApplicationContext。

用于确定WebApplicationType的算法相当简单:

•如果存在Spring MVC,则AnnotationConfigServetWebServerApplicationContext被使用

@SpringBootApplication?•如果不存在Spring MVC且存在Spring WebFlux,则使用annotationconfigreactiveWebServerApplicationContext

•否则,将使用AnnotationConfigApplicationContext

这意味着,如果您使用的是SpringMVC和SpringWebFlux中的新WebClient默认情况下,将使用同一应用程序spring mvc。您可以通过调用setWebApplicationType(WebApplicationType)。

还可以完全控制调用setApplicationContextClass(…)。

访问应用参数

Springboot框架、如果需要访问传递给SpringApplication.run(…),可以插入org.springframework.boot.applicationarguments bean。这个

ApplicationArguments接口提供对原始字符串[]参数以及已分析的选项和非选项参数,如下例所示:

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"]}
}

应用程序退出

每个SpringApplication都向JVM注册一个关闭挂钩,以确保ApplicationContext在退出时正常关闭。所有标准的Spring生命周期回调(例如可使用DisposableBean接口或@Predstroy注释)。

例子:

@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)));}
}

 

 

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/97455.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息