如何使用基于Java的纯配置配置Spring MVC?

use*_*183 34 java spring spring-mvc

我有,我会考虑一个非常简单的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" />
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

我试图将此设置转换为纯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();
 }
}
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

San*_*shi 46

您需要进行以下更改web.xml才能支持基于Java的配置.这将告诉DispatcherServlet使用基于anotation的java配置加载配置AnnotationConfigWebApplicationContext.你只需要将你的javaconfig文件的位置传递给contextConfigLocationparam.如下

<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>
Run Code Online (Sandbox Code Playgroud)

更新:执行相同操作而不更改web.xml

你甚至可以在没有web.xmlServlet规范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("/");
  }
}
Run Code Online (Sandbox Code Playgroud)

更新:来自评论
一个稍微复杂的解释也包含在官方的Spring参考中,现在是Spring 4 Release Reference:

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

  • 现在官方的Spring参考中也包含了一个稍微复杂的解释:http://docs.spring.io/spring/docs/4.0.5.RELEASE/spring-framework-reference/htmlsingle/#beans-java-instantiating-container织网 (3认同)