- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个在 Eclipse 中构建的 Spring MVC 应用程序,我正试图将其部署为 war (到 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);
}
}
编辑:我越来越近了。 war 文件现在部署。但是,我无法到达任何 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>
Controller 看起来像这样:
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/是要添加这样的类(class)。我还没有测试过,但是,我认为这可以代替 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);
}
}
谢谢
最佳答案
根据包装,我假设这是一个 Web 应用程序,其中 main(...)
方法没有意义。相反,Application
应该在您的 Spring 配置中初始化。
如果您需要在其他任何地方引用它,请在您的 Spring 配置中将其定义为一个 bean:
<bean id="application" class="somepackage.Application" init-method="init"/>
否则,如果您只需要在启动时执行一些一次性初始化操作,您可以使用 @Service
或 @Component
注释类并包含 component-scan
指令在你的 Spring 配置中如下:
<context:component-scan base-package="somepackage"/>
Spring 只是一个控制反转 (IoC) 容器,它在 Web 应用程序的上下文中运行,不需要任何特殊的部署实践。只要适用的 Spring 库可用于 Web 应用程序(在 Tomcat 公共(public)类加载器的路径上,或与 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>
将 somepackage.RESTApplication
替换为您的 org.reSTLet.Application
实现。
关于java - 如何将 Spring 应用程序部署到生产服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19992350/
我是一名优秀的程序员,十分优秀!