【问题标题】:ClassPathXmlApplicationContext or WebApplicationContext for Web application without Spring Web没有 Spring Web 的 Web 应用程序的 ClassPathXmlApplicationContext 或 WebApplicationContext
【发布时间】:2014-06-06 20:45:21
【问题描述】:

我想在我的 java web 服务应用程序中使用 spring 框架(部署为战争)。它对用户没有 UI 或“可见的面孔”,因此没有实现 ui 框架。只是服务。所以..我需要一个 WebApplicationContext 来加载我的 bean 配置吗?还是只使用 ClassPathXmlApplicationContext 并将返回的上下文存储在单例中以供后续使用?在这种情况下推荐的模式是什么?

【问题讨论】:

  • 您是否使用 DispatcherServlet 来调用您的 Web 服务处理程序?
  • 不,我不是。我正在使用 Spring 将我的 pojos 包装为 Web 服务。

标签: java spring web-services war applicationcontext


【解决方案1】:

我正在编写一个休息服务(但我也有一个带有 javascript 的 html 页面来记录该服务)。我认为这与您需要的类似,但是如果您想接受不同的标头(json 等),则需要添加更多内容。我只需要几个文件就可以完成所有工作。

这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="testing-services" version="2.5" metadata-complete="true"
     xmlns="..." xmlns:xsi="..."
     xsi:schemaLocation="...">
<display-name>Testing Services</display-name>
<description>Services for selenium</description>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:persistence-config.xml
        classpath:transactions-config.xml
        classpath:testing-service-config.xml
    </param-value>
</context-param>


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

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>


<servlet>
    <servlet-name>mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>/assets/pages/testing-service.html</welcome-file>
</welcome-file-list>

<session-config>
    <session-timeout>60</session-timeout>
</session-config>
</web-app>

我有一个几乎是空的 mvc-servlet.xml(它有一个空的 beans 标签)。

我的testing-service-config.xml,引用自web.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="..."
   xmlns:xsi="..."
   xmlns:context="..."
   xsi:schemaLocation="...">

<context:component-scan base-package="com.myorg.app"/>


</beans>

最后还有一个 spring 配置类,TestingServiceConfiguration:

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
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 static org.springframework.http.MediaType.TEXT_HTML;

@Configuration
@EnableWebMvc
@ComponentScan({"com.myorg.app"})
public class TestingServiceConfiguration extends WebMvcConfigurerAdapter {

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.ignoreAcceptHeader(true);
    configurer.favorParameter(false);
    configurer.favorPathExtension(false);
    configurer.defaultContentType(TEXT_HTML);
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/assets/**").addResourceLocations("/assets/");
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 2023-01-12
    相关资源
    最近更新 更多