A comparison between the annotation configuration of Spring and the XML configuration

  • 2020-04-01 02:15:29
  • OfStack

Annotation configuration has many advantages over XML configuration:
It can take full advantage of Java's reflection mechanism to obtain class structure information, which can effectively reduce the configuration work. When configuring an ORM map using JPA annotations, we don't need to specify the property name, type, and so on for the PO, and if the relational table fields are the same as the PO property name and type, you don't even need to write the task property mapping information -- it's all available through the Java reflection mechanism.

Annotations and Java code are in one file, while XML configuration is in separate configuration files, and most of the configuration information is not adjusted after the program is developed. If the configuration information is put together with Java code, it helps to increase the cohesion of the program. With separate XML configuration files, programmers often need to switch between program files and configuration files when writing a function. This kind of 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 in popularity. One of the big enhancements of Spring 2.5 is the introduction of a number of annotation classes, and now you can do most of the XML configuration using annotation configuration.

Where annotation configuration and XML configuration apply
With these IOC annotations, can we get rid of the XML configuration entirely? The answer is no. There are several reasons:
Annotation configuration is not necessarily superior to XML configuration. If the Bean dependencies are fixed (such as which DAO classes are used by the Service), this configuration information will not be adjusted at deployment time, then the annotation configuration is better than the XML configuration. Conversely, if the dependency is adjusted at deployment time, the XML configuration is clearly superior to the annotation configuration, because annotations are adjustments to Java source code that you need to rewrite and recompile to make adjustments.

If the Bean is not a self-written class (such as JdbcTemplate, SessionFactoryBean, and so on), the annotation configuration cannot be implemented, and the XML configuration is the only way available.

Annotation configuration tends to be class-level, whereas XML configuration can be more flexible. For example, the Transaction configuration using the aop/tx namespace is more flexible and simple than the @transaction Transaction annotation.

So in the implementation of the application, we often need to use both annotation configuration and XML configuration, for the class level and will not change the configuration can be given priority to annotation configuration; For those third-party classes and configurations that are prone to adjustments, XML configuration should be a priority. Spring merges the meta-information of the two configurations before implementing Bean creation and Bean injection.

summary
Spring provides strong support for annotation configuration after 2.1, and annotation configuration is one of the biggest highlights of Spring 2.5. Proper use of Spring 2.5's annotation configuration can effectively reduce the amount of configuration work and improve the cohesion of the program. However, this does not mean that traditional XML configuration is going to die. XML configuration still has an irreplaceable place in the configuration of third-party class beans, as well as those of data sources, cache pools, persistence layer operation template classes, transaction management, and so on.


Related articles: