spring.codeconfig vs xml configuration

主宰稳场 提交于 2019-12-02 07:32:21

问题


I've been using spring.net with xml configuration for some times, and I just saw that spring team released CodeConfig a month ago.

What I like about the xml config is that if I have a problem on the live server I can easily change the xml configuration to enable some specific debugging settings, or disable a specific component simply changing the xml configuration.

What is the advantage of using a code configuration instead of an xml configuration other than compile time check?


回答1:


With code config, possible benefits you could get are:

  • Better refactoring support; e.g. renaming an injected property
  • More compact configuration, compared to xml
  • Developers can use code, in which they are often more at home than in xml

Benefit of the last point is also that developers new to the framework will have a significantly less steep learning curve than with the xml config.

From the docs:

While there are several positive aspects to expressing configuration metadata in XML files, there are also many problems with this approach including the verbosity of XML and its heavy dependence on string-literals which are both prone to typing errors and unusually resistant to most modern refactoring tools in use today. The CodeConfig approach removes these problems by providing a type safe, code-based, approach to dependency injection. It keeps the configuration metadatda external to your class so your class can be a POCO, free of any DI related annotations.




回答2:


Just to highlight one thing, you can mix and match configuration styles. From within a CodeConfig class you can refere to XML config files using the [ImportResource] attribute (see here), and in the XML you can use the namespace (see here).

Cheers, Mark



来源:https://stackoverflow.com/questions/6064759/spring-codeconfig-vs-xml-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!