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 articleArticleI 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 |
AboveProgramYou can re-write in simplified format as follows:
Class = "com. lizjason. spring. orderservice "> value =" lizjason "/> the simplified format is available in version 1.2, but note that this simplified format does not exist.CodeAnd make 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 there are multiple parameters of the same type in the constructor, spring only allows you to use the index or value label 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 attribute: 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.
5. Reuse bean definitions whenever possible
Spring provides a mechanism similar to inheritance to reduce duplication of configuration information and simplify xml configuration. A child bean can inherit configuration information from its parent bean. In essence, this parent bean is like a template of its child bean. This is a feature that must be used in large projects. All you need to do is set the abstract attribute of the parent bean to true and reference it in the Child bean. For example:
Class = "com. lizjason. Spring. abstractservice"> value = "lizjason"/> parent = "abstractservice" class = "com. lizjason. Spring. shippingservice"> |
Shippingservice bean inherits the lizjason value of companyName, the property of abstractservice bean. Note: If you name a class or factory method for bean, this bean will be abstract by default.
6. use applicationcontext to assemble beans as much as possible, instead of using import as imports in the ant script. Spring's import element is very useful for modular bean assembly, for example: class = "com. lizjason. spring. orderservice "/>
However, compared with the use of imports in XML to pre-assemble these beans, using applicationcontext to configure them will be more flexible, and xml configuration can be easier to manage. You can pass a bean definition array to the constructor of applicationcontext as follows:
String [] serviceresources = {"orderservices. xml", "billingservices. xml", "shippingservices. xml "}; Applicationcontext orderservicecontext = newclasspathxmlapplicationcontext (serviceresources ); |
7. Use Id to identify the bean. You can use ID or name to identify the bean. The ID is less readable, but it can affect the XML analyzer to make bean reference valid. If the ID cannot be used due to the XML idref constraint, you can use name as the bean identifier. The XML idref constraint indicates that the ID must start with a letter (or a punctuation mark known in XML), followed by letters, numbers, hyphens, and underscores, colon or full stops (I don't know how to translate it ). XML idref constraints are rarely encountered in practical applications.
8. in the development phase, you can set a value for the bean dependency-check attribute to replace the default none, such as simple, objects, or all, in this case, the container will check the dependency validity for you. It is useful when all attributes of a bean (or some attribute Directories) are explicitly set or used for automatic assembly. Class = "com. lizjason. spring. orderservice "dependency-check =" objects "> value =" lizjason "/> in this example, the container will ensure that these attributes are not privitives or that the collections is set for the orderservice bean. It is possible to set the default dependency check for all beans, but this feature is rarely used because some bean attributes do not need to be set.
9. Add a description comment for each configuration file
It is best to use descriptive IDs and names in xml configuration files, rather than stacked comments. In addition, adding a file description header will be very useful. This description can summarize the bean defined in the file. Another option is to add the description information to the description element. For example:
This file defines billing servicerelated beans and it depends onbaseservices. XML, which providesservice bean templates ......
One advantage of using the description element is that the tool can easily extract the description information from this element.
10. Communicate with team members about changes
After you modify the Java source code, make sure that the corresponding part of the configuration file is changed and inform your team members of this situation. Xml configuration files are also code and they are an important part of the program, but they are hard to read and maintain. Most of the time, you need to read the xml configuration file and Java code at the same time to know what is going on.
11. setter injection and constructor injection are preferred.
Spring provides three injection methods: constructor injection, setter injection, and method injection. We generally use the first two methods.
Class = "com. lizjason. Spring. orderservice"> class = "com. lizjason. Spring. billingservice"> ref = "billingdao"> |
in this example, orderservice bean uses constructor injection, while billingservice bean uses setter injection. Constructor injection ensures that beans are built correctly, but setter injection is more flexible and easy to control, especially when classes have multiple attributes and some of them are optional. 12. Do not misuse injection. As mentioned above, spring applicationcontext can create Java objects for you, but not all Java objects should be created through injection. For example, the domain object should not be created through applicationcontext. Spring is an excellent framework, but considering readability and controllability, configuration based on XML configuration may cause trouble when defining many beans. Using dependency injection in transition will make the xml configuration more complex and lengthy. Remember, when using efficient IDE, such as Eclipse and intellij, Java code is easier to read, maintain and manage than making XML files. Conclusion XML is a popular spring configuration format. When a large number of bean definitions exist, XML-based configurations become lengthy and not easy to use. Spring provides a wide range of configuration options. Using these options appropriately can make the xml configuration clearer, but other options, such as automatic assembly, may reduce readability and maintainability. These tips may help you create a clean and easy-to-read xml configuration file.