【问题标题】:How do I deploy a Spring app to a production server?如何将 Spring 应用程序部署到生产服务器?
【发布时间】:2013-11-28 07:56:33
【问题描述】:

我有一个在 Eclipse 中构建的 Spring MVC 应用程序,我试图将其部署为战争(对 glassfish)。我有一个如下所示的应用程序类:

package com.jp5.rest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;

@Service
@ComponentScan
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    public static void init() {
        SpringApplication.run(Application.class);
    }

}

编辑: 我越来越近了。战争文件现在部署。但是,我无法访问任何 Web 服务端点(它们都返回 404) 现在,我有一个 web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/application-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

还有一个这样的 application-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">  

    <bean id="application" class="com.jp5.rest.Application"
        init-method="init" >
        </bean>


    <context:component-scan base-package="com.jp5.rest"/>       
</beans>

控制器看起来像这样:

package com.jp5.rest;
@ComponentScan
@EnableAutoConfiguration
@Controller
@RequestMapping("/jp5/rest/message")
public class MessageRestService
{
   @RequestMapping(method=RequestMethod.PUT, value="/test")
   public @ResponseBody testResult test()
   {
       return new testResult(true, "test");
   }
}

编辑 2 感谢您的指点。这里的解决方案:http://spring.io/guides/gs/convert-jar-to-war/ 是添加这样的类。我还没有测试过,但是,我认为这可以代替 web.xml:

package com.jp5.rest;


import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.SpringBootServletInitializer;

public class Jp5RestXML extends SpringBootServletInitializer {

    @Override
    protected void configure(SpringApplicationBuilder application) {
        application.sources(Application.class);
    }

}

谢谢

【问题讨论】:

  • war 文件应该在 J2EE Web 应用程序服务器中运行,例如雄猫
  • 你有弹簧靴,为什么要部署到玻璃鱼上?只需创建一个 jar 并执行 java -jar .jar 并查看所有启动...(至少如果您将 spring-boot-starter-web 作为依赖项)。

标签: java spring spring-mvc spring-boot


【解决方案1】:

根据包装,我假设这是一个 Web 应用程序,其中 main(...) 方法没有意义。相反,Application 应该在你的 Spring 配置中初始化。

如果您需要在其他任何地方引用它,请将其定义为 Spring 配置中的 bean:

<bean id="application" class="somepackage.Application" init-method="init"/>

否则,如果您只需要在启动时执行一些一次性初始化的东西,您可以使用 @Service 或 @Component 注释该类,并在您的 Spring 配置中包含 component-scan 指令,如下所示:

<context:component-scan base-package="somepackage"/>

Spring 只是一个控制反转 (IoC) 容器,它在 Web 应用程序的上下文中运行,不需要任何特殊的部署实践。只要适用的 Spring 库对 Web 应用程序可用(在 Tomcat 公共类加载器的路径上,或与 Web 应用程序捆绑在一起),就可以像任何其他 J2EE Web 应用程序一样部署它。

Spring Stereotype Annotation Reference

编辑:如果这是针对 REST API,您使用的库应该提供一个 Servlet 实现,您可以将其添加到您的 web.xml。

例如,如果您使用的是 Restlet 框架,则 servlet 定义如下所示:

<servlet>
    <servlet-name>RestletServlet</servlet-name>
    <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
    <init-param>
        <param-name>org.restlet.application</param-name>
        <param-value>somepackage.RESTApplication</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>RestletServlet</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

用您的org.restlet.Application 实现替换somepackage.RESTApplication。

【讨论】:

  • 这似乎让我更接近了。即将用我现在的位置编辑我的问题
  • 您的 servlet 映射是如何配置的?您是否在控制器中使用 Spring @RequestMapping(...) 注释?如果是这样,您是否使用 @Controller 批注对您的控制器进行了批注?
  • 我了解您对 RESTful 库的编辑。我从字面上开始:spring.io/guides/gs/rest-service,并适应了我的使用。所以,我认为 Spring 正在做 REST 部分。
  • SpringApplication 来自Spring-Boot,它进行了大量的自动配置并允许您简单地从命令行启动应用程序。只需创建一个 jar(或者你喜欢的战争)做 java -jar &lt;your-application-file&gt;.[jar|war] 就可以了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-31
  • 1970-01-01
  • 1970-01-01
  • 2020-09-17
相关资源
最近更新 更多