首页天道酬勤thisapplicationfailed,webapplicationinitializer

thisapplicationfailed,webapplicationinitializer

张世龙 05-12 08:09 13次浏览

自动spring应用程序

spring应用程序是springboot驱动spring APP应用程序上下文的引导类

@ springbootconfiguration @ enableautoconfiguration @ components can (exclude filters={ @ filter (type=filter type.custom ) @filter(type=filtertype.custom,classes=autoconfigurationexcludefilter.classes是在哪个版本中引入的? spring框架3.1

@EnableAutoConfiguration :以激励自动组装@Enable-@Enable开头

(1) @EnableWebMvc

(2) @启用转换管理

(3) @EnableAspectJAutoProxy

)4)启用同步

@SpringBootConfiguration:等效于@ configuration-configuration class注释

@Component的“派生性

@Component

@Component - @ComponentScan

处理系统- ConfigurationClassParser

扫描类-

ClassPathBeanDefinitionScanner

classpathscanningcandidatecomponentprovider

protectedvoidregisterdefaultfilters ((this.include filters.add ) newannotationtypefilter ) component.class ); dubbo @ service-2.5.7-newannotationtypefilter (service.class );

Spring注释编程模型

@Component

@Service

@ component public @接口服务{ . } @ repository

@ component public @接口存储库{ . } @ controller

@ component public @接口控制器{ . } @ configuration

@ component public @ interface configuration { . } spring模式注释: Stereotype Annotations

Spring注释驱动示例

注释驱动上下文从annotationconfigapplicationcontext,Spring Framework 3.0引入

@ configurationpublicclassspringannotationdemo {

publicstaticvoidmain (字符串[ ] args )/XML配置文件驱动classpathxmlapplicationcontext//annotation驱动BeanDefinition //查找@ bean @ configurationannotationconfigapplicationcontextcontext=newannotationconfigapplicationcontext; 注册//configuration class=springannotationdemocontext.register (springannotationdemo.class ); //上下文启动context.refresh (; system.out.println (context.get bean (springannotationdemo.class ) ); } }@SpringBootApplication标注的当前功能

@SpringBootApplication

@SpringBootConfiguration

@Configuration

@Component

 

 

SpringApplication Spring Boot 应用的引导

基本写法

SpringApplication springApplication = new SpringApplication(MicroservicesProjectApplication.class); Map<String,Object> properties = new LinkedHashMap<>(); properties.put("server.port",0); springApplication.setDefaultProperties(properties); springApplication.run(args);

SpringApplicationBuilder

  new SpringApplicationBuilder(MicroservicesProjectApplication.class) // Fluent API           // 单元测试是 PORT = RANDOM           .properties("server.port=0") // 随机向 OS 要可用端口           .run(args);

Spring Boot 引导示例

@SpringBootApplicationpublic class MicroservicesProjectApplication {

public static void main(String[] args) {   SpringApplication springApplication = new SpringApplication(MicroservicesProjectApplication.class);   Map<String,Object> properties = new LinkedHashMap<>();   properties.put("server.port",0);   springApplication.setDefaultProperties(properties);   ConfigurableApplicationContext context = springApplication.run(args);   // 有异常?   System.out.println(context.getBean(MicroservicesProjectApplication.class));}

}

调整示例为 非 Web 程序

@SpringBootApplicationpublic class MicroservicesProjectApplication {

public static void main(String[] args) {   SpringApplication springApplication = new SpringApplication(MicroservicesProjectApplication.class);   Map<String, Object> properties = new LinkedHashMap<>();   properties.put("server.port", 0);   springApplication.setDefaultProperties(properties);   // 设置为 非 web 应用   springApplication.setWebApplicationType(WebApplicationType.NONE);   ConfigurableApplicationContext context = springApplication.run(args);   // 有异常?   System.out.println(context.getBean(MicroservicesProjectApplication.class));   // 输出当前 Spring Boot 应用的 ApplicationContext 的类名   System.out.println("当前 Spring 应用上下文的类:" + context.getClass().getName());}

}输出结果:

当前 Spring 应用上下文的类:org.springframework.context.annotation.AnnotationConfigApplicationContext

配置 Spring Boot 源

SpringAppliation 类型推断

当不加以设置 Web 类型,那么它采用推断:

-> SpringAppliation() -> deduceWebApplicationType() 第一次推断为 WebApplicationType.SERVLET

 

WebApplicationType.NONE : 非 Web 类型

 

Servlet 不存在Spring Web 应用上下文 ConfigurableWebApplicationContext 不存在

spring-boot-starter-web 不存在

spring-boot-starter-webflux 不存在

 

WebApplicationType.REACTIVE : Spring WebFlux

DispatcherHandler

spring-boot-starter-webflux 存在

Servlet 不存在

spring-boot-starter-web 不存在

 

WebApplicationType.SERVLET : Spring MVC

 

spring-boot-starter-web 存在

 

 

人工干预 Web 类型

设置 webApplicationType 属性 为 WebApplicationType.NONE

Spring Boot 事件

Spring 事件

Spring 内部发送事件

 

ContextRefreshedEvent

ApplicationContextEventApplicationEvent

 

refresh() -> finishRefresh() -> publishEvent(new ContextRefreshedEvent(this));

 

ContextClosedEvent

ApplicationContextEventApplicationEvent

 

close() -> doClose() -> publishEvent(new ContextClosedEvent(this));

自定义事件

PayloadApplicationEvent

Spring 事件 都是 ApplicationEvent 类型

发送 Spring 事件通过 ApplicationEventMulticaster#multicastEvent(ApplicationEvent, ResolvableType)

Spring 事件的类型 ApplicationEvent

Spring 事件监听器 ApplicationListener

Spring 事件广播器 ApplicationEventMulticaster

实现类:SimpleApplicationEventMulticaster

 

Spring 事件理解为消息

ApplicationEvent 相当于消息内容

ApplicationListener 相当于消息消费者、订阅者

ApplicationEventMulticaster 相当于消息生产者、发布者

Spring Boot 事件监听示例

@EnableAutoConfigurationpublic class SpringBootEventDemo {

public static void main(String[] args) {   new SpringApplicationBuilder(SpringBootEventDemo.class)           .listeners(event -> { // 增加监听器               System.err.println("监听到事件 : " + event.getClass().getSimpleName());           })           .run(args)           .close();   ; // 运行}

}

ApplicationStartingEvent(1)ApplicationEnvironmentPreparedEvent(2)ApplicationPreparedEvent(3)ContextRefreshedEventServletWebServerInitializedEventApplicationStartedEvent(4)ApplicationReadyEvent(5)ContextClosedEventApplicationFailedEvent (特殊情况)(6)

Spring Boot 事件监听器

org.springframework.context.ApplicationListener=\org.springframework.boot.ClearCachesApplicationListener,\org.springframework.boot.builder.ParentContextCloserApplicationListener,\org.springframework.boot.context.FileEncodingApplicationListener,\org.springframework.boot.context.config.AnsiOutputApplicationListener,\org.springframework.boot.context.config.ConfigFileApplicationListener,\org.springframework.boot.context.config.DelegatingApplicationListener,\org.springframework.boot.context.logging.ClasspathLoggingApplicationListener,\org.springframework.boot.context.logging.LoggingApplicationListener,\org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListenerConfigFileApplicationListener 监听 ApplicationEnvironmentPreparedEvent 事件

从而加载 application.properties 或者 application.yml 文件

Spring Boot 很多组件依赖于 Spring Boot 事件监听器实现,本质是 Spring Framework 事件/监听机制

SpringApplication 利用

Spring 应用上下文(ApplicationContext)生命周期控制 注解驱动 Bean Spring 事件/监听(ApplicationEventMulticaster)机制加载或者初始化组件

springboot类加载顺序,webapplicationinitializer