部署war spring失败 - 无法启动组件

时间:2017-06-27 16:20:15

标签: java spring spring-mvc tomcat war

我已经尝试将.WAR spring部署到tomcat 9但是有错误 - 无法启动组件。 error log 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配置。空配置。如何解决这个问题呢?当我把我已经创建的网址服务时,它找不到,我有一个问题。我在tomcat中部署war时必须放置配置吗?尽管如此,我的代码在部署之前运行良好。没问题。

感谢。 波比

2 个答案:

答案 0 :(得分:0)

您需要配置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

答案 1 :(得分:0)

即使我也使用spring boot获得Hello world的相同错误消息。但只是尝试了tomcat的最新版本(我使用的是tomcat 8.5.23)。它像魅力一样!!!