Spring Boot WEB-INF未加载

时间:2014-11-21 12:31:14

标签: java xml spring servlets spring-boot

我有一个春季启动应用程序。打包为war文件,内容如下

static
org - springframework - boot -loader - SpringClasses
META-INF
    - MANIFEST.MF
    - maven -my.group.id - my-artifact-id - pom.xml & pom.properties
WEB-INF
    - lib (contains all jars)
    - classes (my main application's classes)
    - some other stuff
    - web.xml
    - main-servlet.xml

其中web.xml和main-servlet.xml是配置xml'

我试过,从springBoot应用程序执行如下操作:

@EnableWebMvc
@EnableAutoConfiguration
@Configuration
@ImportResource({ "classpath:main-servlet.xml"})
public class FakeAppBooter {

    public static void main(String args[]) {
        SpringApplication.run(FakeAppBooter.class, args);
        System.out.println("Test");
    }

    public DispatcherServlet mvcDispatcherServlet() {
            XmlWebApplicationContext ctx = new XmlWebApplicationContext();
            ctx.setConfigLocation("classpath:main-servlet.xml");
            DispatcherServlet dispatcherServlet = new DispatcherServlet(ctx);
            return dispatcherServlet;
        }

        @Bean
        public ServletRegistrationBean mvcServletRegistrationBean() {
            ServletRegistrationBean bean = new ServletRegistrationBean();
            bean.setServlet(mvcDispatcherServlet());
            ArrayList<String> list = new ArrayList<>();
            list.add("/");
            bean.setUrlMappings(list);
            return bean;
        }
}

然而,在启动时,我得到:

Caused by: java.io.FileNotFoundException: class path resource [main-servlet.xml] cannot be opened because it does not exist
        at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
        ... 25 more

我需要这些来为我的应用程序定义servlet。

我该怎么办?

1 个答案:

答案 0 :(得分:0)

您的问题的解决方案:

  1. 当您使用SpringBoot时从不关心您的web.xml,因为SpringBoot本身将通过EmbeddedServletContainer初始化您的Web容器,最好是TomcatEmbeddedServletContainer。因此,忽略您的web.xml配置并解决您的第一个问题
  2. 您还没有将main-servlet.xml放在适当的类路径中。复制main-servlet.xml并将其粘贴到项目的 src文件夹中。

  3. 相应地更改您的代码

    import java.util.ArrayList;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    //Place your Imports Appropriately
    
    /**
     * @author Praveen
     *
     */
      @EnableWebMvc
      @EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)
      @ImportResource({ "classpath:main-servlet.xml"})
      @ComponentScan
      public class FakeAppBooter {
      private ApplicationContext ctx;
      public static void main(String[] args) {
      SpringApplication.run(FakeAppBooter.class, args);
      System.out.println("Test>>>>");
     }
      public DispatcherServlet mvcDispatcherServlet() {
         ctx = new ClassPathXmlApplicationContext();
         ((AbstractRefreshableConfigApplicationContext)                       ctx).setConfigLocation("classpath:main-servlet.xml");
         DispatcherServlet dispatcherServlet = new DispatcherServlet();
          return dispatcherServlet;
       }
    
      @Bean
        public ServletRegistrationBean mvcServletRegistrationBean() {
        ServletRegistrationBean bean = new ServletRegistrationBean();
        bean.setServlet(mvcDispatcherServlet());
        ArrayList<String> list = new ArrayList<>();
        list.add("/");
        bean.setUrlMappings(list); 
        return bean;
        }
      }
    
  4. main-servlet.xml
    我以非常简单的方式保存了main-servlet.xml。

    main-servlet.xml file

  5. 成功登录
    Successful Log