Minimize Spring XML configuration

Source: Internet
Author: User

Spring provides several tricks that can help us reduce The number of XML configurations:

1. Automatic assembly (autowiring) helps to reduce or even eliminate configuration <property> elements and <constructor-arg > element that lets Spring automatically identify how to assemble a Bean 's dependencies.

2. Automatic detection (AutoDiscovery) is a step closer to automatic assembly, allowing spring to automatically identify which classes need to be configured as spring Beans , thereby reducing the <bean> the use of elements.

1.1, auto-assemble Bean Properties 1.1.1,4 types of automatic assembly

1, Byname- and bean other with the same name as the property bean auto assemble to bean bean The property is not assembled.

2. bytype- automatically assembles other beans of the same type as the bean 's attributes into the corresponding attributes of the Bean . If there is no Bean that matches the type of the property , the property is not assembled.

3. The constructor- automatically assembles the beans with other beans of the same type as the Bean 's constructor into the bean The corresponding entry parameter of the constructor.

4, autodetect- first try to use constructor for automatic assembly. If it fails, try using bytype for automatic assembly.


ByName Automatic Assembly

<bean id= "Kenny2" class= "com.springinaction.springidol.Instrumentalist" ><property name= "song" Value= " Jingle Bells "></property><property name=" instrument "ref=" saxophone "></property></bean >

You can write this.

<bean id= "instrument" class= "com.springinaction.springidol.Saxophone"/><bean id= "Kenny2" class= " Com.springinaction.springidol.Instrumentalist "autowire=" byname "><property name=" song "Value=" Jingle Bells " ></property></bean>

Bytype Automatic Assembly

Bytype Automatic assembly works similar to byname automatic assembly, except that it is no longer the name of the matching attribute but the type of the property. When we try to use Bytype to assemble automatically, spring looks for which Bean's type matches the type of the property.

But Bytype automatic assembly has one limitation: What if Spring finds multiple beans whose type matches the type of attributes that need to be automatically assembled? In this scenario, spring does not guess which Bean is more suitable for automatic assembly, but instead chooses to throw an exception. Therefore, the app allows only one bean to match the type of attribute that needs to be automatically assembled.

To avoid ambiguity caused by the use of Bytype automatic assembly, Spring provides us with two additional options: A preferred bean for automatic assembly, or the ability to cancel a candidate for automatic assembly of a bean.

To identify a preferred bean for automatic assembly, you can use the primary property of the <bean> element. If only the primary property of a candidate bean that is automatically assembled is set to true, the bean will be preferred over other candidate beans.

But the primary property has a very strange point: it is set to True by default. Therefore, in order to use the primary property, we have to set all non-preferred primary properties to false. For example:

<bean id= "instrument" class= "Com.springinaction.springidol.Saxophone" primary= "false"/>

The primary property is only meaningful for identifying preferred beans. If we want to exclude certain beans from automatic assembly, we can set the Autowire-candidate property of these beans to false, as follows:

<bean id= "instrument" class= "Com.springinaction.springidol.Saxophone" autowire-candidate= "false"/>

Constructor Assembly

Constructor automatic assembly has the same limitations as Bytype automatic assembly. When multiple beans are found to match the arguments of a constructor, spring does not attempt to guess which Bean is more suitable for automatic assembly. In addition, if a class has multiple constructors that meet the conditions of automatic assembly, spring does not attempt to guess which constructor is more appropriate for use.


Best Automatic assembly

When configuring a bean's Autowire property to AutoDetect, spring will first use constructor to assemble automatically, and if no bean matching the constructor is found, spring will attempt to use Bytype automatic assembly.


3.1.2, default automatic assembly

All we need to do is add a default-autowire attribute to the root element <beans> :

<span style= "color: #009900;" ><?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema /beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd </span><span style= "color:# ff0000; " >default-autowire= "Bytype" </span><span style= "color: #009900;" >></span>

Default-autowire applies to all beans in the specified spring configuration file, not to all beans in the Spring app context.

You can define multiple profiles in a spring application context, each of which can have its own default automatic assembly policy.

Again, not because we have configured a default automatic assembly policy, it means that all beans can only use this default automatic assembly policy. We can also use the Autowire property of the <bean> element to override the default automatic assembly policy configured by the <beans> element.



Minimize Spring XML configuration

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.