qq_19430115
qq_19430115
2017-05-23 23:10

spring boot无法加载web.xml配置

20
  • spring

刚写了一个spring-bootMVC 的dome, web.xml的配置写的是注解,书上说 只要继承了 WebApplicationInitializer 接口,启动是就会被自动获取到
web.xml ==>WebInitializer.java

 package com.sesame.config.web;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import com.sesame.config.Config;


public class WebInitializer implements WebApplicationInitializer {

    /**
     * 启动容器
     */
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        System.out.println("启动容器啦");
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(Config.class);
        ctx.setServletContext(servletContext);//将注册配置类和当前servletContext关联

        // 注册springMVC的DispatcherServlet
        Dynamic servlet =  servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);

    }
}

config.java

 package com.sesame.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan("com.sesame")
public class Config {

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

        return viewResolver;
    }
}

但是我在启动后
System.out.println("启动容器啦");
这行代码没有被执行,也就是说 WebInitializer.java 没有被容器识别到,
谁知道这个问题呀?求帮助

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答