簡體   English   中英

部署戰爭彈簧失敗-無法啟動組件

[英]failed deploying war spring - failed to start component

我已經嘗試將.WAR spring部署到tomcat 9,但出現錯誤-無法啟動組件。 錯誤日志tomcat

AppInitializer:

public class AppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext container) throws ServletException {

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(AppConfig.class);
        ctx.setServletContext(container);

        ServletRegistration.Dynamic servlet = container.addServlet(
                "dispatcher", new DispatcherServlet(ctx));

        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");
    }
}

SpringBootWarDeploymentApplication:

@SpringBootApplication
public class SpringBootWarDeploymentApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootWarDeploymentApplication.class);
    }

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

AppConfig的:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.project.maven")
public class AppConfig extends WebMvcConfigurerAdapter {

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

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
        registry.addResourceHandler("**/**")
        .addResourceLocations("classpath:/META-INF/resources/"); // harus ada folder resources di webapp/WEB-INF/
    }       

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }   
}

在web.xml中,沒有servlet配置。 空配置。 如何解決這個問題呢? 當我放置已經創建的URL服務時,找不到它,我有一個問題。 在tomcat中部署war時必須放置配置嗎? 即使,我的代碼在部署之前也能很好地運行。 沒問題。

謝謝。 鮑比

您需要配置web.xml 使用波紋管參考代碼:-

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>sample.traditional.config</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

<!-- Disables Servlet Container welcome file handling. Needed for compatibility 
    with Servlet 3.0 and Tomcat 7.0 -->
<welcome-file-list>
    <welcome-file></welcome-file>
</welcome-file-list>

</web-app>

要進行更多配置,您可以點擊下面的鏈接:-

http://callistaenterprise.se/blogg/teknik/2014/04/15/a-first-look-at-spring-boot/

https://dzone.com/articles/webapp-makeover-spring-4-and

甚至我也使用Spring Boot收到了Hello world的錯誤消息。 但是只是嘗試了最新版本的tomcat(我使用了tomcat 8.5.23)。 它像魅力一樣工作!!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM