春天没有初始化

时间:2018-03-26 22:21:46

标签: java spring eclipse spring-mvc

我正在将一个小应用程序从Tomcat / Java 8降级到Tomcat / Java 7.由于这个原因,我不得不将Spring从5.x版降级到4.x版。尽管我正在努力让东西再次运行,但Spring的上下文还没有开始。

配置:

  • hibernate-entitymanager 5.2.15
  • mysql-connector-java 6.0.6
  • javax.servlet-api 4.0.0
  • jstl 1.2
  • jta 1.1
  • spring-data-jpa 2.0.2
  • commons-codec 1.11
  • gson 2.8.2
  • commons-dbcp2 2.2.0
  • validation-api 2.0.1
  • spring-core 4.1.4
  • spring-context 4.1.4
  • spring-context-support 4.1.4
  • spring-web 4.1.4
  • spring-jdbc 4.1.4
  • spring-orm 4.1.4
  • spring-webmvc 4.1.4
  • javax-servlet.jsp-api 2.3.1

问题:

INFO: No Spring WebApplicationInitializer types detected on classpath


package com.mycompany.myapp.conf;

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

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

public class ApplicationInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) throws ServletException {

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

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

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

}


package com.mycompany.myapp.conf;

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

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.mycompany.myapp")
public class SpringConfiguration extends WebMvcConfigurerAdapter {

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

        return viewResolver;
    }

    //@Override
     public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry
              .addResourceHandler("/resources/**")
              .addResourceLocations("/public", "/resources/")
              .setCachePeriod(31556926); 
        }

}

1 个答案:

答案 0 :(得分:0)

关于知识建设的问题:

我发现Spring没有识别当前的目录结构(显然适用于Java / Tomcat 8和更新版本的Spring)。

然后,我使用常规的Maven目录结构创建了一个新项目,然后Spring开始查看Initializer和Configuration类。