12 tips for XML configuration in Spring (1)

Source: Internet
Author: User

Spring uses dependency injection to obtain simple and effective testing capabilities. Spring beans, dependencies, and beans required by services are described in the configuration file. The configuration file is generally in XML format. However, XML configuration files are lengthy and not easy to use. In a large project that uses a large number of beans, it will become hard to read and control.

In this article, I will show you 12 best tips on Spring XML configuration files. Pay attention to other factors, such as the design of the domain model, which may affect the XML configuration. However, this article focuses more on the readability and controllability of XML configuration.

1. Avoid using automatic assembly

Spring can automatically assemble dependencies through bean class introspection, so that you do not have to explicitly describe bean attributes or constructor parameters. The bean property can be automatically assembled Based on the live matching type of the property name. Constructor can be automatically assembled according to the matching type. You can even set automatic assembly for automatic detection, so that Spring will select an appropriate mechanism for you. See the following example:

Spring can automatically assemble dependencies through bean class introspection, so that you do not have to explicitly describe bean attributes or constructor parameters. The bean property can be automatically assembled Based on the live matching type of the property name. Constructor can be automatically assembled according to the matching type. You can even set automatic assembly for automatic detection, so that Spring will select an appropriate mechanism for you. See the following example:

     
      class="com.lizjason.spring.OrderService"
      
autowire="byName"/>

The attribute name of the OrderService class is used to match a bean instance in the container. Automatic Assembly will silently save some type information and reduce confusion. However, because it sacrifices the intuition and maintainability of this configuration, you will not use it in actual projects. Many guides and statement materials boast it as a very cool feature of Spring without mentioning its shortcomings. In my opinion, like Spring's object pool, it has more commercial taste. It seems that the XML configuration file can be simplified, but it actually increases its complexity, especially when many beans have been defined in your large-scale project. Spring allows you to mix automatic and manual assembly, but this conflict makes XML configuration even more confusing.

2. Use naming rules

Like Java coding, it is useful for developers to understand XML configurations by using clear, descriptive, and consistent naming rules in projects. Taking bean ID as an example, you can follow the naming rules of attributes in Java classes. For example, the bean ID of OrderServiceDAO should be orderServiceDAO. For large projects, add the package name before the bean ID as the prefix.

3. simplified format

Simplified format helps reduce redundancy because it uses attribute values and references as attributes rather than child elements. See the following example:

     
      class="com.lizjason.spring.OrderService">
      

lizjason




The preceding procedures can be rewritten in simplified format:

     
      class="com.lizjason.spring.OrderService">
      
value="lizjason"/>

The simplified format is available in version 1.2, but note that it does not exist. This simplified format not only reduces the input of your code, but also makes the XML configuration clearer. When your configuration file contains a large number of bean definitions, it can significantly improve readability.

4. Try to use type instead of index to solve the problem of constructor parameter matching.

When the constructor has multiple parameters of the same type, Spring only allows you to use the index or value tag starting from 0 to solve this problem. See the following example:

     
      class="com.lizjason.spring.BillingService">
      


It is best to replace the above practice with the type property:

 
     
      class="com.lizjason.spring.BillingService">
      
value="lizjason"/>

Using index can slightly reduce redundancy, but it is more error-prone and less readable than the type attribute. You should only use index when there is a parameter conflict in the constructor.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.