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

查看:24
本文介绍了如何使用纯基于 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 容器中注册它?我使用的是最新的 spring (4.02).

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

谢谢!

推荐答案

您需要对 web.xml 进行以下更改以支持基于 Java 的配置.这将告诉 DispatcherServlet 使用基于注解的 java 配置 AnnotationConfigWebApplicationContext 加载配置.你只需要将你的 java 配置文件的位置传递给 contextConfigLocation 参数,如下

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 annotation based java configuration AnnotationConfigWebApplicationContext. You only need to pass the location of your java config file to the 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 do this without web.xml as Servlet Specification 3.0 makes the web.xml optional. You only need to implement/configure WebApplicationInitializer interface to configure the ServletContext which will allow you to create, configure, and perform registration of DispatcherServlet programmatically. The good thing is that WebApplicationInitializer is detected automatically.

总而言之,需要实现WebApplicationInitializer来摆脱web.xml.

In summary, one needs to implement WebApplicationInitializer 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 发布

参考:

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

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

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