如何使用基于Java的纯配置配置Spring MVC? [英] How to configure Spring MVC with pure Java-based configuration?

查看:132
本文介绍了如何使用基于Java的纯配置配置Spring MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我会考虑一个非常简单的Spring MVC设置。我的applicationContext.xml是这样的:

I have, what I would consider a pretty simple Spring MVC setup. My applicationContext.xml is this:

<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目前是这样的:

My web.xml is currently this:

  <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上下文。

I am trying to convert this set up to pure Java-based config. I've searched the web and so far, I've come up with stuff that explains (some what) how to do the Java config but doesn't explain how to register that Java config with the environment, i.e., the web context.

到目前为止我对@Configuration的了解是:

What I have so far in terms of @Configuration is this:

 @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();
 }
}

如何在Web容器中注册?我正在使用最新的春天(4.02)。

How do I register this with the web container? I am using the latest spring (4.02).

谢谢!

推荐答案

您需要对 web.xml 进行以下更改,以支持基于Java的配置。这将告诉 DispatcherServlet 使用基于anotation的java configuraion AnnotationConfigWebApplicationContext 加载配置。您只需要将javaconfig文件的位置传递给 contextConfigLocation param。如下所示

You need to make following changes to web.xml in order to support java based configuration. This will tell the the DispatcherServlet to load configuration using the anotation based java configuraion AnnotationConfigWebApplicationContext. you only need to pass the location of your javaconfig file to contextConfigLocation param. as below

<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

You can even to this without web.xml as Servlet Specification 3.0 makes the web.xml optional. You only need to implement/configure WebApplicationInitializer interface to configures the ServletContext which will allow you to create, configre, registration of DispatcherServlet programmatically. The Good thing is that WebApplicationInitializer is detected automatically.

摘要是需要实现的 WebApplicationInitializer 摆脱 web.xml

The summary is the one need to implementWebApplicationInitializer to get rid of 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
参考:

Update: from comments
A slightly more convoluted explanation is also included in official Spring reference now Spring 4 Release Reference:

http://docs.spring.io /spring/docs/3.1.x/javadoc-api/org/springframework/web/WebApplicationInitializer.html

这篇关于如何使用基于Java的纯配置配置Spring MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆