Annotation configuration has many advantages over XML configuration:
It can make full use of Java's reflection mechanism to obtain class structure information, which can effectively reduce configuration work. For example, when using JPA annotations to configure ORM mapping, we do not need to specify PO attribute names, types and other information. If the relationship table fields and PO attribute names and types are consistent, you do not even need to write task attribute mapping information - because this information is Can be obtained through Java reflection mechanism.
Comments and Java code are located in one file, while XML configuration uses an independent configuration file. Most configuration information will not be adjusted after the program is developed. If the configuration information and Java code are placed together, it helps to enhance the cohesion of the program. sex. With independent XML configuration files, programmers often need to constantly switch between program files and configuration files when writing a function. This incoherence in thinking will reduce 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 XML configuration functions.
Where annotation configuration and XML configuration are applicable
With these IOC annotations, can we completely abandon the original XML configuration method? The answer is no. There are several reasons:
Annotation configuration is not necessarily inherently superior to XML configuration. If the Bean's dependency is fixed (such as which DAO classes are used by the Service), and this configuration information will not be adjusted during deployment, then annotation configuration is better than XML configuration; conversely, if this dependency will not be adjusted during deployment When adjustments occur, XML configuration is obviously better than annotation configuration, because annotations are adjustments to Java source code, and you need to rewrite the source code and recompile before you can implement the adjustment.
If the Bean is not a class written by yourself (such as JdbcTemplate, SessionFactoryBean, etc.), annotation configuration will not be implemented, and XML configuration is the only available way.
Annotation configuration tends to be at the class level, while XML configuration can be more flexible. For example, compared to the @Transaction transaction annotation, transaction configuration using the aop/tx namespace is more flexible and simpler.
Therefore, when implementing applications, we often need to use annotation configuration and XML configuration at the same time. For configurations at the class level that will not change, annotation configuration can be given priority; for third-party classes and configurations that are easy to adjust, priority should be given to using them. XML configuration. Spring will fuse the meta-information of these two configuration methods together before implementing bean creation and bean injection.
summary
Spring has provided strong support for annotation configuration since 2.1, and the annotation configuration function has become one of the biggest highlights of Spring 2.5. Reasonable use of Spring 2.5 annotation configuration can effectively reduce the configuration workload and improve the cohesion of the program. But this does not mean that traditional XML configuration will die out. XML configuration still has irreplaceable functions in the configuration of third-party bean classes, as well as the configuration of content such as data sources, cache pools, persistence layer operation template classes, transaction management, etc. status.