Comparison between spring annotation configuration and XML configuration

Annotation configuration has many advantages over XML configuration: it can make full use of java reflection mechanism to obtain class structure information, which can effectively reduce the work of configuration. For example, when configuring ORM mapping with JPA annotation, we do not need to specify the attribute name, type and other information of Po. If the relationship table fields are consistent with the Po attribute name and type, you do not even need to write task attribute mapping information - because these information can be obtained through java reflection mechanism. Comments and Java code are located in one file, while XML configuration adopts an independent configuration file. Most configuration information will not be adjusted after program development. If the configuration information and Java code are put together, it will help to enhance the cohesion of the program. When using an independent XML configuration file, programmers often need to switch between the program file and the configuration file when writing a function. This incoherence in thinking will reduce the development efficiency. Therefore, in many cases, annotation configuration is more popular than XML configuration, and annotation configuration has a further trend of popularity. A major enhancement of spring 2.5 is the introduction of many annotation classes. Now you can use annotation configuration to complete most of the functions of XML configuration.

Applicable occasions of annotation configuration and XML configuration. With these IOC annotations, can we completely get rid of the original XML configuration? The answer is No. There are several reasons: annotation configuration is not necessarily superior to XML configuration. If bean dependencies are fixed, (for example, which Dao classes are used by the service), this configuration information will not be adjusted during deployment, so the annotation configuration is better than the XML configuration; on the contrary, if this dependency will be adjusted during deployment, the XML configuration is obviously better than the annotation configuration, because annotation is an adjustment to the Java source code, and you need to rewrite the source code and recompile to implement the adjustment 。 If the bean is not a class written by itself (such as jdbctemplate, sessionfactorybean, etc.), annotation configuration cannot be implemented. At this time, XML configuration is the only available way. Annotation configuration is often class level, while XML configuration can be more flexible. For example, compared with @ transaction transaction transaction annotation, transaction configuration using AOP / TX namespace is more flexible and simple. Therefore, in the implementation and application, I Users often need to use annotation configuration and XML configuration at the same time. For class level configuration that will not change, annotation configuration can be given priority; For those third-party classes and configurations that are prone to adjustment, XML configuration should be preferred. Spring will fuse the meta information of the two configuration methods before implementing bean creation and bean injection.

Summary spring provides strong support for annotation configuration after 2.1. Annotation configuration has become one of the biggest highlights of spring 2.5. Rational use of annotation configuration in spring 2.5 can effectively reduce the workload of configuration and improve the cohesion of the program. However, this does not mean that the traditional XML configuration will die out. XML configuration still has an irreplaceable position in the configuration of third-party beans and those contents such as data source, cache pool, persistence layer operation template class, transaction management and so on.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>