无法启动组件 [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/wabSpring]]

Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/wabSpring]]

我正在尝试在 tomcat.For 上启动 spring 引导应用程序,我在 tomcat 上部署了 war 文件,但我得到 Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/wabSpring]]。我在 google 上搜索这个,但没有适合我的解决方案。请告诉我为什么会出现此错误。

我正在使用 tomcat 8.5 和 spring 版本 2.0.6

WebAppInitializer.java

public class WebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) throws ServletException 
{
        AnnotationConfigWebApplicationContext context = new 
AnnotationConfigWebApplicationContext();
        context.register(AppConfig.class);
        context.setServletContext(container);

        ServletRegistration.Dynamic servlet = 
container.addServlet("dispatcher", new 
     DispatcherServlet(context));
        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");
    }
}

AppConfig.java

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.springboot.wabit.*" })

public class AppConfig implements WebMvcConfigurer {

@Autowired
private WebFlowConfig webFlowConfig;

public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("index");
}

@Bean
public ViewResolver viewResolver() {
    InternalResourceViewResolver resolver = new 
  InternalResourceViewResolver();
    resolver.setViewClass(JstlView.class);
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}

@Bean(name = "multipartResolver")
public StandardServletMultipartResolver resolver() {
    return new StandardServletMultipartResolver();
}

@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new 
 CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(2000000);  
    return multipartResolver;
}


@Bean
public FlowHandlerAdapter flowHandlerAdapter() {
    FlowHandlerAdapter handlerAdapter = new FlowHandlerAdapter();
    handlerAdapter.setFlowExecutor(this.webFlowConfig.flowExecutor());
    handlerAdapter.setSaveOutputToFlashScopeOnRedirect(true);
    return handlerAdapter;
}

@Bean
public FlowHandlerMapping flowHandlerMapping() {
    FlowHandlerMapping handlerMapping = new FlowHandlerMapping();
    handlerMapping.setOrder(-1);
    handlerMapping.setFlowRegistry(this.webFlowConfig.flowRegistry());
    return handlerMapping;
}

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer 
configurer) {
    configurer.enable();
}


@Override
public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")
    .allowedOrigins("*")
    .allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH");
    
}

}

WebFlowConfig.java

@Configuration
@ComponentScan(basePackages = { "com.springboot.wabit.*" })
public class WebFlowConfig extends AbstractFlowConfiguration {

@Autowired
private AppConfig AppConfig;

@Bean
public FlowExecutor flowExecutor() {
    return 
getFlowExecutorBuilder(flowRegistry()).addFlowExecutionListener(new 
   SecurityFlowExecutionListener(), "*")
            .build();
}

@Bean
public FlowDefinitionRegistry flowRegistry() {
    return getFlowDefinitionRegistryBuilder().setBasePath("/WEB- 
INF/views/flows")
            .addFlowLocationPattern("/**/*-flow.xml").build();
}

@Bean
public FlowBuilderServices flowBuilderServices() {
    return   
   
getFlowBuilderServicesBuilder().
setViewFactoryCreator(mvcViewFactoryCreator())
 .setValidator(validator()).setDevelopmentMode(true).build();
}

@Bean
public MvcViewFactoryCreator mvcViewFactoryCreator() {
    MvcViewFactoryCreator factoryCreator = new MvcViewFactoryCreator();
    factoryCreator.setViewResolvers(Collections.singletonList
    (this.AppConfig.viewResolver()));
    factoryCreator.setUseSpringBeanBinding(true);
    return factoryCreator;
}

@Bean
public LocalValidatorFactoryBean validator() {
    return new LocalValidatorFactoryBean();
}

  }

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet- 
    class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>
        <param-name>throwExceptionIfNoHandlerFound</param-name>
        <param-value>true</param-value>
    </init-param>

    <multipart-config>
        <max-file-size>2097152</max-file-size>
        <max-request-size>4194304</max-request-size>
        <file-size-threshold>1048576</file-size-threshold>
    </multipart-config>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring-security.xml
    </param-value>
</context-param>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter- 
class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>  

dispatcher.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:wf="http://www.springframework.org/schema/webflow-config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="

http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow- 
config.xsd">

<context:component-scan
    base-package="com.springboot.wabit" />

<bean id="viewResolver"
     
class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="multipartResolver"
      class="org.springframework.web.multipart.
support.StandardServletMultipartResolver" />

<mvc:annotation-driven />
<mvc:resources location="/assets/"
    mapping="/resources/**" />

<wf:flow-executor id="flowExecutor"
    flow-registry="flowRegistry" />

<wf:flow-registry id="flowRegistry"
    base-path="/WEB-INF/views/flows"
    flow-builder-services="flowBuilderServices">
    <wf:flow-location-pattern
        value="/**/*-flow.xml" />
</wf:flow-registry>

<wf:flow-builder-services
    id="flowBuilderServices" view-factory-creator="viewFactoryCreator"
    validator="validator" />

<bean
    class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
    <property name="flowExecutor" ref="flowExecutor" />
</bean>

<bean
    class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
    <property name="flowRegistry" ref="flowRegistry" />
    <property name="order" value="-1" />
</bean>

<bean id="viewFactoryCreator"
    class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
    <property name="viewResolvers" ref="viewResolver" />
</bean>

    <bean id="validator"
    class="org.springframework.validation.
   beanvalidation.LocalValidatorFactoryBean" />
   </beans>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  
   <modelVersion> 4.0.0 </modelVersion>
   <groupId> com.springboot </groupId>
   <artifactId> wabit </artifactId>
   <version> 0.0.1-SNAPSHOT </version>
   <packaging> war </packaging>

<name>wabit</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF- 
8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
    </dependency>

     <dependency>
        <groupId>org.springframework.webflow</groupId>
        <artifactId>spring-webflow</artifactId>
        <version>2.5.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.13</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

    <!-- database connection pooling -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.1.1</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.1</version>
    </dependency>

    <dependency>
        <groupId>org.javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.27.0-GA</version>
    </dependency>


    <dependency>
        <groupId>commons-digester</groupId>
        <artifactId>commons-digester</artifactId>
        <version>2.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.1.1</version>
    </dependency>
</dependencies>

  <build>
    <finalName>wabSpring</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

   </project>

日志

 *Oct-2020 11:26:03.558 SEVERE [http-nio-8080-exec-52] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start: 
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/wabSpring]]
    at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:440)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:198)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:743)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:719)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:705)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:970)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1634)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:286)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(Unknown Source)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(Unknown Source)
    at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1565)
    at org.apache.catalina.manager.HTMLManagerServlet.upload(HTMLManagerServlet.java:289)
    at org.apache.catalina.manager.HTMLManagerServlet.doPost(HTMLManagerServlet.java:207)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:211)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:109)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:668)
    at org.apache.catalina.valves.RequestFilterValve.process(RequestFilterValve.java:348)
    at org.apache.catalina.valves.RemoteAddrValve.invoke(RemoteAddrValve.java:52)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:616)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:818)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1626)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at com.springboot.wabit.config.WebAppInitializer.onStartup(WebAppInitializer.java:26)
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:172)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5144)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    ... 48 more*    

我需要在 WebAppInitializer 文件中更改什么吗?如果我不包含 dispatcher-servlet.xml 文件,我的应用程序会从 tomcat 开始,但我认为那不是启动应用程序的好方法。

  1. 删除你的web.xml
  2. 删除你的WebAppInitializer
  3. 删除你的dispatcher.xml
  4. 从您的 WebFlowConfig
  5. 中删除 @CompoentScan
  6. 清理你的AppConfig(只留下 WebFlow 配置内容)
  7. 将属性放入您的 application.properties
  8. 让你的@SpringBootApplication延长SpringBootServletInitializer
  9. 构建和部署。

你的 AppConfig 做的太多了,禁用 Spring 启动 auto-config 只保留 WebFlow 配置。

@Configuration
public class AppConfig implements WebMvcConfigurer {

    private final WebFlowConfig webFlowConfig;

    public AppConfig(WebFlowConfig webFlowConfig) {
      this.webFlowConfig=webFlowConfig;
    }

    public void addViewControllers(ViewControllerRegistry registry) {
      registry.addViewController("/").setViewName("index");
    }
    
    @Bean
    public FlowHandlerAdapter flowHandlerAdapter() {
        FlowHandlerAdapter handlerAdapter = new FlowHandlerAdapter();
        handlerAdapter.setFlowExecutor(this.webFlowConfig.flowExecutor());
        handlerAdapter.setSaveOutputToFlashScopeOnRedirect(true);
        return handlerAdapter;
    }
    
    @Bean
    public FlowHandlerMapping flowHandlerMapping() {
        FlowHandlerMapping handlerMapping = new FlowHandlerMapping();
        handlerMapping.setOrder(-1);
        handlerMapping.setFlowRegistry(this.webFlowConfig.flowRegistry());
        return handlerMapping;
    }
}

你的`

@Configuration
public class WebFlowConfig extends AbstractFlowConfiguration {

    @Bean
    public FlowExecutor flowExecutor() {
        return 
    getFlowExecutorBuilder(flowRegistry()).addFlowExecutionListener(new 
       SecurityFlowExecutionListener(), "*")
                .build();
    }
    
    @Bean
    public FlowDefinitionRegistry flowRegistry() {
        return getFlowDefinitionRegistryBuilder().setBasePath("/WEB- 
    INF/views/flows")
                .addFlowLocationPattern("/**/*-flow.xml").build();
    }
    
    @Bean
    public FlowBuilderServices flowBuilderServices(MvcViewFactoryCreator viewFactoryCreator, Validator validator) {
        return   
       
    getFlowBuilderServicesBuilder().
    setViewFactoryCreator(viewFactoryCreator)
     .setValidator(validator).setDevelopmentMode(true).build();
    }
    
    @Bean
    public MvcViewFactoryCreator mvcViewFactoryCreator(List<ViewResolver> viewResolvers) {
        MvcViewFactoryCreator factoryCreator = new MvcViewFactoryCreator();
        factoryCreator.setViewResolvers(viewResolvers);
        factoryCreator.setUseSpringBeanBinding(true);
        return factoryCreator;
    }

  }

你应该已经有一个 @SpringBootApplication 注释 class,如果你不创建它并将它放在 com.springboot.wabit 中(因为我假设那是你的 top-level包)并让它扩展 SpringBootServletInitializer。如 the official documentation.

中所述
@SpringBootApplication
public class YourApplication extends SpringBootServletInitializer {

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

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder.sources(YourApplication.class);
  }
}

现在,当您从 AppConfig class 中删除一些 bean 时,您需要向 application.properties 添加一些属性以实现相同的目的。

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsps

spring.servlet.multipart.maxFileSize=2MB

最后,您的依赖关系有点乱,您不需要 servlet-apijavaassistmaven-shade 依赖关系。您不需要它们,或者它们已经包含在您导入的 spring-boot-starter-* 项目中。