Home  >  Q&A  >  body text

为什么从Spring4.x开始又推荐使用java配置方式了呢?之前不一直是推荐使用xml进行配置么?

在之前使用Spring的时候,身边的同事或者是朋友一致都推荐使用XML来配置spring,但是等Spring4.*以及SpringBoot问世后普遍都推崇使用Java来配置spring了?这是为什么呢?使用java配置Spring比用Xml配置Spring有什么好处么?

PHPzPHPz2716 days ago598

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 18:03:15

    It is more intuitive and simple to write the configuration in the code (some configurations have been done by spring boot itself), using chain programming. . .
    Using maven dependencies is more refreshing.
    But I think there are more pitfalls

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 18:03:15

    Because there are many configurations in spring that will not be changed once the configuration is completed, and there is no need to change, such as the dependencies of most beans. It makes no sense to put all these configurations that will not change in xml. It will only make the configuration larger and larger. Spring 3.x was criticized for having too many configurations, and it has become configuration-based programming, which puts the cart before the horse. , so it is actually better to put these nearly static configurations in the code.
    XML is more convenient to modify, and it can take effect without compilation, so it is better to put those configurations that need to be changed according to the environment and business in XML.
    Spring Boot absorbs the convention-based configuration of Rails, which reduces the configuration a lot. However, if you are not familiar with how its underlying configuration is configured, you may encounter many problems.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 18:03:15

    I feel a little speechless about this too.
    If you must explain, the Java configuration before the Spring era was imperative (written down all the way, depending on the execution order), which was changed to XML declarative. In the Spring Boot era, declarative Java configuration was implemented again (scattered in multiple annotated methods, does not depend on execution order).
    In addition, many configurations can be overridden with application.properties. This should have been done a long time ago!

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 18:03:15

    spring的发展,采用了约定优于配置的原则。
    1.xml中维护bean之间的依赖关系,随着业务逻辑的增长变得臃肿。
    2.在.java中的配置效率是没有xml效率高的。

    reply
    0
  • Cancelreply