我想在我的java web服务应用程序中使用spring框架(部署为war)。它没有UI或对用户“可见的脸”,因此没有实现ui框架。只有服务。所以..。我需要一个WebApplicationContext来加载我的bean配置吗?或者只是使用ClassPathXmlApplicationContext并将返回的上下文存储在单例中以供后续使用?在这种情况下,推荐的模式是什么?
发布于 2014-06-07 05:04:22
我正在编写一个rest服务(但我也有一个包含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标记)。
从web.xml引用的我的testing-service-config.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/");
}
}https://stackoverflow.com/questions/24090327
复制相似问题