基于Spring注释的配置中的mvc:default-servlet-handler的等价物? [英] Equivalent of mvc:default-servlet-handler in Spring annotation-based configuration?

查看:273
本文介绍了基于Spring注释的配置中的mvc:default-servlet-handler的等价物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 AnnotationConfig(Web)ApplicationContext中定义等效的< mvc:default-servlet-handler /> ?现在我有:

Is it possible to have the equivalent of <mvc:default-servlet-handler/> defined in an AnnotationConfig(Web)ApplicationContext? Right now I have:

@Configuration
@ImportResource("classpath:/mvc-resources.xml")
class AppConfig {
  // Other configuration...
}

在我的 resources / mvc-resources.xml中只有以下内容

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:default-servlet-handler/>
</beans>

它按预期工作。是否可以在不导入XML文件的情况下执行此操作?这将是一个减少一些样板的好方法。

And it works as expected. Is it possible to do this without importing an XML file? It would be a nice way to cut down on some boilerplate.

推荐答案

在深入挖掘之后,我发现这是一个已知问题,将在即将发布的中通过注释功能解决春季3.1

After digging a bit deeper, I found out that this is a known problem and is addressed by annotation features in the upcoming Spring 3.1.

我用以下代码解决了我的问题:

I solved my problem with the following code:

@Configuration
@Import(FeatureConfig.class)
class AppConfig {
   ...
}

@FeatureConfiguration
class FeatureConfig {
  @Feature
  public MvcDefaultServletHandler defaultHandler() {
    return new MvcDefaultServletHandler();
  }
}

这需要使用spring的里程碑版本,尽管,但它似乎是最清洁和首选的处理方式。

This does require using the milestone version of spring, though, but it seems to be the cleanest and preferred way of handling this.

这篇关于基于Spring注释的配置中的mvc:default-servlet-handler的等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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