我有,我会考虑一个非常简单的Spring MVC设置。我的applicationContext.xml是这样的:
<mvc:annotation-driven />
<mvc:resources mapping="/css/**" location="/css/" />
<context:property-placeholder location="classpath:controller-test.properties" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
我的web.xml目前是这样的:
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
我正在尝试将此设置转换为纯基于Java的配置。我已经搜索过网络,到目前为止,我已经提出了解释(一些是什么)如何进行Java配置的内容,但没有解释如何在环境中注册Java配置,即Web上下文。
到目前为止,@Configuration的内容是:
@Configuration
@EnableWebMvc
@PropertySource("classpath:controller.properties")
@ComponentScan("com.project.web")
public class WebSpringConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
}
@Bean
public ViewResolver configureViewResolver() {
InternalResourceViewResolver viewResolve = new InternalResourceViewResolver();
viewResolve.setPrefix("/WEB-INF/views/");
viewResolve.setSuffix(".jsp");
return viewResolve;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
configurer.enable();
}
}
如何在网络容器中注册?我正在使用最新的春天(4.02)。
谢谢!
答案 0 :(得分:45)
您需要对web.xml
进行以下更改才能支持基于java的配置。这将告诉DispatcherServlet
使用基于anotation的java配置AnnotationConfigWebApplicationContext
加载配置。您只需要将javaconfig文件的位置传递给contextConfigLocation
param。如下
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/*path to your WebSpringConfig*/ </param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
更新:执行相同操作而不更改web.xml
您甚至可以在没有web.xml
的情况下使用Servlet规范3.0使web.xml
成为可选项。您只需要实现/配置WebApplicationInitializer
接口来配置ServletContext
,这将允许您以编程方式创建,配置,DispatcherServlet
注册。好的是自动检测WebApplicationInitializer
。
总结是需要实施WebApplicationInitializer
以摆脱web.xml
。
public class MyWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(WebSpringConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext =
new AnnotationConfigWebApplicationContext();
dispatcherContext.register(DispatcherConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
更新:来自评论
现在正式的Spring参考文献中也包含了一个稍微复杂的解释Spring 4 Release
参考:
答案 1 :(得分:1)
基于Java的配置,而无需向web.xml
添加任何元素。 WebApplicationInitializer非常适合与Spring的基于代码的@Configuration
类一起使用
WebApplicationInitializer«将在Servlet 3.0+ environments
中实现的接口,以便以编程方式配置ServletContext-与传统的基于web.xml的方法相反(或可能与之结合)。 Implementations of this SPI will be detected automatically by SpringServletContainerInitializer, which itself is bootstrapped automatically by any Servlet 3.0 container
。 Using Servlet Spec 3.0 of Tomcat 7
从Spring 3.2开始,列出了一些Abstract类,该类实现了WebApplicationInitializer,它将由SrevletContainer自动检测。
AbstractAnnotationConfigDispatcherServletInitializer extends
AbstractDispatcherServletInitializer extends
AbstractContextLoaderInitializer implements WebApplicationInitializer
将Spring 4.1.6.RELEASE
版本与模块core, web, webmvc, beans
一起使用。
public class WebXML_DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { MvcServletXMLConfigurer.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
基于Java的配置以使用Spring服务静态资源。 Spring Boot
@Configuration
@EnableWebMvc // <mvc:annotation-driven />
@ComponentScan(value = {"com.github.yash777.controllers"})
// <context:component-scan base-package="com.github.yash777" />
public class MvcServletXMLConfigurer extends WebMvcConfigurerAdapter implements WebMvcConfigurer {
/**
* <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
* p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
*
* @return InternalResourceViewResolver as a bean configuration.
*/
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
System.out.println("WebMvcConfigurer - addResourceHandlers() function get loaded...");
// <mvc:resources mapping="/styles/**" location="/css/" />
registry
.addResourceHandler("/styles/**")
.addResourceLocations("/css/") // webapp/css/
.setCachePeriod(3600)
.resourceChain(true) // Spring 4.1
.addResolver(new GzipResourceResolver()) // Spring 4.1
.addResolver(new PathResourceResolver()); // Spring 4.1
// <mvc:resources mapping="/static/**" location="/static/" />
registry.addResourceHandler("/static/**")
.addResourceLocations("/static/", "classpath:/static/") // src/main/resources/static/
.setCachePeriod(3600)
.resourceChain(true)
.addResolver(new PathResourceResolver());
}
}
列出了一个示例控制器:
@Controller
@RequestMapping(value = { "/controller", "/c" })
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
@RequestMapping(value = {"/message", "/m"}, method = RequestMethod.GET )
public void message(HttpServletRequest request, HttpServletResponse response ) throws IOException {
System.out.println("@Controller Get method called.");
}
@RequestMapping(value = "/getView", method = RequestMethod.GET )
public ModelAndView setViewName( Model model ) {
System.out.println("GET... /getView");
ModelAndView mav = new ModelAndView();
mav.setViewName("test");
return mav;
}
}
WEB-INF/web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>