Minimize Spring XML configuration

Source: Internet
Author: User

  Automatic assembly is available in the following ways:

1 ByName The name of the ID and the name of the property, to ensure that the name of the attribute in the bean instance is the same as the ID name of the assembly.

2 Bytype determines the assembled bean by type, but when there are multiple types of matching beans, an error occurs.

3 Contructor When constructing the injection, the assembly method is used, and the effect is as bytype.

4 AutoDetect first try to use constructor to automatically assemble the line. If it fails, try using Bytype to automatically assemble the line again. (This test, 3.0.5 version is not available, do not know whether it was removed.) )

The configuration file using ByName mode is as follows:

<?XML version= "1.0" encoding= "UTF-8"?><BeansXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://www.springframework.org/schema/beans"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd ">        <BeanID= "instrument"class= "Com.spring.test.setter.Saxophone"/>    <BeanID= "Kenny"class= "Com.spring.test.setter.Instrumentalist"Autowire= "ByName">         < Propertyname= "Song"value= "Jingle Bells" />         < Propertyname= "Age"value= "+" />     </Bean></Beans>

The configuration file with Bytype is as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"    xmlns= "Http://www.springframework.org/schema/beans"    xsi:schemalocation= "http://www.springframework.org/ Schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">        <bean id=" Test2 " class= "Com.spring.test.setter.Saxophone"/>    <bean id= "test1" class= "Com.spring.test.setter.Saxophone" Primary= "true"/> <!--defaults to False--    <bean id= "Kenny" class= "Com.spring.test.setter.Instrumentalist" Autowire= "Bytype" >         <property name= "song" Value= "Jingle Bells"/>         <property name= "age" value= "25" />     </bean></beans>

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.