Comparison of Spring's annotation configuration and XML configuration

The following contents are respectively transferred:
http://shuimomo.blog.51cto.com/1141396/322209
http://blog.sina.com.cn/s/blog_451f596201014z4k.html

/************ ***111111111111********************The
advantages are:
1. The XML configuration method further reduces the coupling, making the application easier to expand, even if the configuration file is further modified There is also no need to modify and recompile the project.
2. When dealing with large business volume, it should be better to use XML configuration. Because XML more clearly shows the relationship between various objects, calls between various business classes. At the same time, the relevant configuration of spring can also be seen at a glance.
Of course, some people will say that using XML configuration will make the XML file too large and difficult to view when there is a large business volume. At this point, we can use business decomposition to write multiple XML configuration files.

The disadvantages are:
it takes a certain amount of time to read and parse the configuration file, it is difficult to manage when there are too many configuration files, and the correctness of the configuration cannot be verified, which increases the difficulty of testing.



Advantages and disadvantages of annotation configuration: The

advantages are:
1. In the class file, maintenance costs can be reduced, and the configuration mechanism of annotation is obviously simple
2. No third-party parsing tools are required, and the task can be completed by using java reflection technology
3. Edit period The correctness can be verified, and the error becomes easy
4. Improve the development efficiency

The disadvantages are:
1. If you need to modify the annotation, you need to recompile the entire project
2. The relationship between business classes is not as easy to grasp as XML configuration.
3. If there are many annotations in the program, it will directly affect the quality of the code and have a certain impact on the simplicity of the code.

/********************22222222********************
Annotation configuration is not necessarily inherently better than XML configuration. If the dependency of the bean is fixed (such as which DAO classes the Service uses), and this configuration information will not be adjusted during deployment, then the annotation configuration is better than the XML configuration; otherwise, if the dependency will be deployed during deployment XML configuration is obviously better than annotation configuration, because annotations are adjustments 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.), the annotation configuration will not 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, compared to the @Transaction transaction annotation, the transaction configuration using the aop/tx namespace is more flexible and simpler.
Therefore, in implementing applications, we often need to use both annotation configuration and XML configuration. For class-level configurations that do not change, we can give priority to annotation configuration; for those third-party classes and configurations that are prone to adjustment, priority should be given to using XML configuration. Spring will combine the meta information of these two configuration methods before implementing bean creation and bean injection.

summary

Spring has provided strong support for annotation configuration after 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 workload of configuration and improve the cohesion of the program. But this does not mean that the traditional XML configuration will die out. In the configuration of third-party Beans, as well as those such as data sources, cache pools, persistence layer operation template classes, transaction management, etc., XML configuration is still irreplaceable. status.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327056738&siteId=291194637