spring.codeconfig vs xml configuration

烈酒焚心 提交于 2019-12-02 03:19:50

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.

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

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