【问题标题】:Why Spring MVC configuration support for XML configuration is deprecated?为什么不推荐使用 Spring MVC 对 XML 配置的配置支持?
【发布时间】:2013-10-17 15:24:01
【问题描述】:

Spring MVC 主要使用注解来配置它的控制器,据我所知,在 Spring 中配置控制器的唯一方法是没有注解(只有 XML)是扩展 AbstracController(或类似的控制器类),目前所有这些类都是Spring 3 已弃用。

虽然我认为放弃对此类的支持是一个好主意,主要是因为扩展此类会创建几乎不依赖 Spring 作为依赖项的控制器,但我不明白为什么 Spring 不提供像 Struts Actions 这样的配置(Struts 2 中的动作没有扩展任何奇怪的类,因此它们没有任何 Struts 依赖)

为什么 Spring MVC 不提供像 Struts 2 Actions via XML 这样干净的 POJO 风格的配置?

为什么在 MVC 上使用丑陋的注解放弃对 XML 配置的支持?为什么不把它放到所有 Spring Proyects 中呢?

【问题讨论】:

标签: java spring spring-mvc struts2


【解决方案1】:

XML/POJO 方法的主要问题是无法通过查看代码来判断正在发生的特殊魔法。

而不是看到

@SomeAnnotation   <<-- Oh, golly there is something special happening here.
java code... 

你看

Java code    <<-- special magic hidden in XML file (or not, no way to tell)
             <<-- are these linked? no idea..
             <<-- is something going on? let me go and search....

如果源代码发生更改,XML 可能(也可能不会)不同步。

使用注解可以同时更新java代码和spring注解。
是的,它很混乱,但至少很容易同步两者。

当您看到注释时,它们很难让人摸不着头脑。如果它们甚至不可见,那么对于我们这些不生气的开发者来说,精神负担真的太大了。

为什么不在所有 Spring 项目中删除它?

那不是很甜蜜吗……

使用丑陋的注解?

显然问题已经被问到了:Is there a way to hide annotations in Eclipse?
答案是:https://stackoverflow.com/a/2569646/650492
有点……这有帮助吗?

【讨论】:

  • +1 除此之外,还有一个事实是,不必extends 框架类使您的代码在其自己的类层次结构中更加灵活。
  • 所以可以肯定地说 Spring 社区正在从 XML-configuration-to-rule-them-all 转变为 annotations-configuration-to-rule-them-all?
猜你喜欢
  • 2015-01-12
  • 2011-07-01
  • 2014-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多