Twelve best methods for xml configuration files in spring (I)

Source: Internet
Author: User

Reference: http://developer.51cto.com/art/200906/129816.htm

 

    This article describes the first six of the 12 best methods for xml configuration files in spring, including the use of concise forms and naming conventions.
  •  
    1. <BeanId = "orderservice"
    2. Class = "com. lizjason. Spring. orderservice"
    3. Autowire = "byname"/> 
    1. <BeanId = "orderservice"
    2. Class = "com. lizjason. Spring. orderservice"> 
    3. <PropertyName = "companyName"> 
    4. <Value>LizjasonValue> 
    5. Property> 
    6. <Constructor-Arg> 
    7. <RefBean = "orderdao"> 
    8. Constructor-Arg> 
    9. Bean> 
    1. <BeanId = "orderservice"
    2. Class = "com. lizjason. Spring. orderservice"> 
    3. <PropertyName = "companyName"
    4. Value = "lizjason"/> 
    5. <Constructor-ArgRef = "orderdao"/> 
    6. Bean>
    1. <BeanId = "abstractservice" abstract = "true"
    2. Class = "com. lizjason. Spring. abstractservice"> 
    3. <PropertyName = "companyName"
    4. Value = "lizjason"/> 
    5. Bean> 
    6. <BeanId = "shippingservice"
    7. Parent = "abstractservice"
    8. Class = "com. lizjason. Spring. shippingservice"> 
    9. <PropertyName = "shippedby" value = "lizjason"/> 
    10. Bean>
    1. <Beans> 
    2. <ImportResource = "billingservices. xml"/> 
    3. <ImportResource = "shippingservices. xml"/> 
    4. <BeanId = "orderservice"
    5. Class = "com. lizjason. Spring. orderservice"/> 
    6. <Beans>
    1. String [] serviceresources =
    2. {"Orderservices. xml ",
    3. "Billingservices. xml ",
    4. "Shippingservices. xml "};
    5. Applicationcontext orderservicecontext = new
    6. Classpathxmlapplicationcontext (serviceresources );
  •  

    1. Avoid using the autowiring Function
    Spring can automatically bind dependencies through Bean class introspection, so you do not need to explicitly specify bean attributes and constructors. Bean attributes can be automatically bound by property names or type matching. The constructor performs automatic binding through type matching. You can even specify the autowiring mode, which can guide spring to select an appropriate running mechanism. Let's take a look at the following example:

    The attribute name of the orderservice class is used in the container to match the bean instance. Automatic Binding may save some typing workload and reduce confusion. However, this method should not be used in real projects because it sacrifices the readability and maintainability of configurations. Many guides and introductions boast that automatic binding is an excellent feature of spring, without mentioning the sacrifice of this feature. In my opinion, this is like the object-pooling in spring. to a greater extent, it is just a publicity gimmick. It is a good method for streamlining xml configuration files, but it actually increases complexity, especially when running projects that contain a large number of class declarations. Although spring allows both automatic binding and explicit binding, xml configuration is even more obscure.

    2. Use naming conventions
    This principle also applies to Java encoding. Using clear, descriptive, and consistent naming conventions in a project helps developers understand XML configurations. For example, the bean ID can be named according to the Java field name conventions. The bean ID of the orderservicedao instance should be named orderservicedao. For large projects, you can add the package name as the prefix before the bean ID.

    3. Simple Form
    The conciseness avoids the lengthy format because it moves the attribute value and reference from the child element into the attribute. For example:

    You can rewrite the above Code in a concise form as follows:

    The concise form can be used since version 1.2. Note that, for, there is no concise form.
    The concise form not only saves the typing workload, but also makes the xml configuration file clearer. When a configuration file defines a large number of classes, it can significantly improve readability.

    4. For constructor parameter matching, the type is better than the subscript
    When the constructor contains more than one parameter of the same type or the tag of the attribute value is occupied, spring allows subscript starting from 0 to avoid confusion. For example:

 
 
  1. <bean id="billingService" 
  2.         class="com.lizjason.spring.BillingService"> 
  3.         <constructor-arg index="0" value="lizjason"/> 
  4.         <constructor-arg index="1" value="100"/> 
  5.     bean> 

It is better to use the type attribute, as shown below:

 
 
  1. <bean id="billingService" 
  2.         class="com.lizjason.spring.BillingService"> 
  3.         <constructor-arg type="java.lang.String" 
  4.             value="lizjason"/> 
  5.  
  6.         <constructor-arg type="int" value="100"/> 
  7.     bean>  

Using index can reduce some code, but it is more error-prone and difficult to read than the type attribute. Index should be used only when the constructor parameters are not clear.

5. Try to reuse the defined Bean
Spring provides a mechanism similar to inheritance to reduce the replication of configuration information and simplify xml configuration. Define a subclass to inherit the configuration information from the parent class, and the parent class actually becomes a template of the subclass. This is the so-called reuse of large projects. You only need to set abstract = true in the parent bean and specify the parent reference in the Child bean. For example:

The shippingservice class inherits the value of the companyName attribute from the abstractservice class-lizjason. If a bean does not specify a class or factory method, the bean is abstract.

6. During import, the bean definition is compiled by applicationcontext.
Like the import in the ant script, the import element of spring is very useful for compiling modular bean definitions. For example:

However, compared to using import for preassembly in xml configuration, configuring these beans through applicationcontext is more flexible. Using applicationcontext makes xml configuration easier to manage. You can pass a set of bean definitions to the applictioncontext constructor as follows:

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.