java - Spring JavaConfig 和 Tomcat 8

标签 java eclipse spring spring-mvc tomcat

我有一个 Spring 4 Web 应用程序 (webapp-module.war) 在 Eclipse 中使用 Java 8、tomcat 8 和 JavaConfig(无 web.xml)在本地工作和运行:

enter image description here

但是当我在远程 Ubuntu 服务器上部署到 tomcat 8(我在 eclipse 中本地使用的相同版本)时,我得到:

enter image description here

我验证了正确的主机和端口。日志中没有报错(/var/lib/tomcat8/logs/catalina.out)

Jun 21, 2016 10:32:44 PM org.apache.catalina.startup.HostConfig undeploy
INFO: Undeploying context [/webapp-module]
Jun 21, 2016 10:32:44 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /var/lib/tomcat8/webapps/webapp-module.war
Jun 21, 2016 10:32:46 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Jun 21, 2016 10:32:46 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deployment of web application archive /var/lib/tomcat8/webapps/webapp-module.war has finished in 1,870 ms
root@vmi63860:/var/lib/tomcat8/logs# 

访问日志包含:

root@vmi63860:/var/log/tomcat8# cat localhost_access_log.2016-06-22.txt 
xx.xxx.xxx.xx - - [22/Jun/2016:22:36:00 +0200] "GET /webapp-module/ HTTP/1.1" 404 1040
xx.xxx.xxx.xx - - [22/Jun/2016:22:36:00 +0200] "GET /favicon.ico HTTP/1.1" 404 1034
xx.xxx.xxx.xx - - [22/Jun/2016:22:36:50 +0200] "GET /webapp-module/hello HTTP/1.1" 404 1050

其中 xx.xxx.xxx.xx 是我尝试在浏览器中访问 Web 应用程序的本地计算机的 IP。

我看了看: Spring Java Config: Tomcat deploy without web.xml但它并没有真正提供解决方案。

下面是我的项目的详细信息:

来源

enter image description here

配置.java

@Configuration // Marks this class as configuration
// Specifies which package to scan
@ComponentScan("com.samples")
// Enables Spring's annotations
@EnableWebMvc
public class Config {

  @Bean
  public UrlBasedViewResolver setupViewResolver() {
    UrlBasedViewResolver resolver = new UrlBasedViewResolver();
    resolver.setPrefix("/WEB-INF/jsp/");
    resolver.setSuffix(".jsp");
    resolver.setViewClass(JstlView.class);
    return resolver;
  }

}

WebInitializer.java

public class WebInitializer implements WebApplicationInitializer {

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

    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(Config.class);
    ctx.setServletContext(servletContext);

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

  }

}

HelloController.java

@Controller
public class HelloController {

  @RequestMapping("/")
  public String home() {
    return "index";
  }

  @RequestMapping("/hello")
  public String showhello(ModelMap model) {
    model.addAttribute("message", "Hello Spring MVC Framework!");
    return "hello";
  }

}

最佳答案

抱歉之前太草率了

似乎根本没有加载 spring 上下文。

我猜问题出在这段代码中:

public class WebInitializer implements WebApplicationInitializer {

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

    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(Config.class);
    ctx.setServletContext(servletContext);

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

  }

}

你使用了ctx.register(Config.class);

无论如何我总是使用这种初始化:

public class AppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.scan("com.spring");
        rootContext.setConfigLocations(new String[]{"com.spring.config.WebAppContextConfig", "com.spring.config.AppConfig"});
        // Manages the lifecycle of the root application context
        servletContext.addListener(new ContextLoaderListener(rootContext));

        // Declare dispatcher servlet. Handles requests into the application
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
                new DispatcherServlet(rootContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

    }

}

如您所见,我使用了 rootContext.setConfigLocations 来指定在哪里可以找到 spring 配置类

无论如何Here你可以找到一个工作示例,我成功地将它部署在 tomcat 8.0.39 和 8.5.4 上

希望有用

安杰洛

关于java - Spring JavaConfig 和 Tomcat 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37954300/

相关文章:

java - 将架构位置添加到 JAXB 解码器

eclipse - m2e 中是否提供所有 Maven 选项?

java - 在 Eclipse 中停止 vertx verticle

python - Pydev 调试器无法使用断点

java - Spring RequestBody Mapping 将所有属性映射为 null 值

java - 无需 web.xml 注册 Olingo servlet

java - 用 do while 循环提示,似乎无法进入循环

java - 为什么@JoinColumn在父端但生成的列在子端?

java - Spring JMS 模板发送无效消息

java - 添加 AOP 建议时 Spring Boot 应用程序上下文被破坏