Java 不带Spring Web的Web应用程序的ClassPathXmlApplicationContext或WebApplicationContext

Java 不带Spring Web的Web应用程序的ClassPathXmlApplicationContext或WebApplicationContext,java,spring,web-services,war,applicationcontext,Java,Spring,Web Services,War,Applicationcontext,我想在部署为war的java web服务应用程序中使用spring框架。它没有UI或用户可见的界面,因此没有实现UI框架。只是服务。所以我是否需要WebApplicationContext来加载我的beans配置?或者只使用ClassPathXmlApplicationContext并将返回的上下文存储在一个单例中以供后续使用?在这个场景中,推荐的模式是什么?我正在编写一个rest服务,但我还有一个包含javascript的html页面来记录该服务。我认为,这与您需要的类似,但是如果您想要接受不

我想在部署为war的java web服务应用程序中使用spring框架。它没有UI或用户可见的界面,因此没有实现UI框架。只是服务。所以我是否需要WebApplicationContext来加载我的beans配置?或者只使用ClassPathXmlApplicationContext并将返回的上下文存储在一个单例中以供后续使用?在这个场景中,推荐的模式是什么?

我正在编写一个rest服务,但我还有一个包含javascript的html页面来记录该服务。我认为,这与您需要的类似,但是如果您想要接受不同的头json等,则需要添加更多的头。我只需要少量文件就可以完成所有工作

这是我的web.xml

最后是一个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/");
}

}

您正在使用DispatcherServlet调用web服务处理程序吗?不,我没有。我正在使用Spring将POJO包装为web服务。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="..."
   xmlns:xsi="..."
   xmlns:context="..."
   xsi:schemaLocation="...">

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


</beans>
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/");
}

}