Reference: http://developer.51cto.com/art/200906/129820.htm
This article introduces the last six of the 12 best methods of the XML configuration file in spring, including adding the first comment for each configuration file, and setter injection is better than constructor injection.
7. Use Id as the bean identifier
You can specify an ID or name as the bean identifier. Although using ID cannot improve readability, it can use XML analysis programs to verify bean reference. If you cannot use an id due to the XML idref constraint, you can use the name as the bean identifier. The constraint of XML idref is that the ID must start with a letter (or a punctuation mark defined in the XML specification), followed by letters, numbers, hyphens, underscores, colons, or periods. In fact, XML idref constraints are rarely encountered.
8. Use dependency-check during development)
You can set a non-default value for the dependency-check attribute in bean definition, such as simple, objects, or all, so that the container can perform dependency check. Dependency check is useful when you need to explicitly or automatically bind all attributes (or certain attributes) of the bean.
<Bean id = "orderservice"
Class = "com. lizjason. Spring. orderservice"
Dependency-check = "objects">
<Property name = "companyName"
Value = "lizjason"/>
<Constructor-Arg ref = "orderdao"/>
</Bean>
In this example, the container ensures that the attribute set for orderservice bean is not primitives or collections. You can also set the default dependency check for all beans, but we seldom do this, because some bean attributes do not need to be set at all.
9. Add the first comment for each configuration file
It is best to replace the built-in annotations in the XML configuration file with descriptive IDs and names. In addition, it is useful to add a configuration file header, which can be used to outline the bean defined in the file. You can add the description to the description label. For example:
<Beans>
<Description>
This file defines Billing Service
Related beans and it depends on
Baseservices. XML, which provides
Service bean templates...
</Description>
...
</Beans>
One advantage of using the description tag is that you can easily use the tool to select a description from the tag.
10. For changes, the team members should actively communicate with each other
When refactoring Java code, you must update the configuration file at any time and notify team members. Xml configuration files are also code, and they are vital parts of the application, but they are difficult to read and maintain. In most cases, you need to read both the xml configuration file and the running Java code.
11. setter injection is better than constructor Injection
Spring provides three types of dependency injection: constructor injection, setter injection, and method injection ). We generally only use the first two methods.
<Bean id = "orderservice"
Class = "com. lizjason. Spring. orderservice">
<Constructor-Arg ref = "orderdao"/>
</Bean>
<Bean id = "billingservice"
Class = "com. lizjason. Spring. billingservice">
<Property name = "billingdao"
Ref = "billingdao">
</Bean>
In this example, the orderservice class uses constructor injection, while the billingservice class uses setter injection. Constructor injection ensures that beans are not created in an invalid state, but setter injection is more flexible and easier to manage, especially when the class contains many attributes and is optional.
12. Do not abuse dependency Injection
At the last point, spring applicationcontext can create Java objects for you, but not all Java objects should be created through dependency injection. For example, a global object cannot be created using applicationcontext. Spring is a great framework. However, for readability and manageability, if a large number of beans are defined, XML-based configuration may become a problem. Over-using dependency injection makes xml configuration complicated and bloated. You must know that with powerful IDE (such as Eclipse and intellij), Java code is easier to read, maintain, and manage than XML files.
Conclusion
XML is a common spring configuration method. However, if a large number of beans are defined, the XML-based configuration will become lengthy and not practical. Spring provides a wide range of configuration options to make the xml configuration clearer by making appropriate use of the options. However, some options (such as autowiring) tend to reduce the readability and maintainability of the configuration file. Following the best practices described in this article will help you create clear and easy-to-read xml configuration files.