Automatic assembly of Spring (VI)

Source: Internet
Author: User
Tags tutorialspoint

First, the automatic assembly model

The following is the automatic connection mode, which can be used to instruct the spring container to use automatic connection for dependency injection. You can use the Autowire property of an element to specify Autowire mode for the bean definition.

You can use bytype or constructor automatic assembly mode to connect arrays and other types of collections.

Limitations of automatic Assembly

When automatic assembly is always used in the same project, it works best. If automatic assembly is not usually used, it may confuse the developer to use it to connect only one or two bean definitions. However, automatic assembly can significantly reduce the attributes or constructor parameters that you need to specify, but you should consider the limitations and drawbacks of automatic assembly before using them.

Spring automatically assembles ' byname '

This pattern is specified by the property name for automatic assembly. The Spring container is considered beans, and the auto-wire property Beans in the XML configuration file is set to byname. It then attempts to match and connect its properties with beans defined in the configuration file as the same name. If a match is found, it injects these beans, otherwise it throws an exception.

For example, in a configuration file, if a bean definition is set to automatically assemble byname, and it contains the spellchecker property (that is, it has a setspellchecker (...) method) , Spring looks for the bean that defines the name spellchecker and uses it to set this property. You can still use the <property> tab to connect the rest of the properties. The following example illustrates this concept.

(1) Writing Texteditor.java

 PackageCom.tutorialspoint; Public classTextEditor {Privatespellchecker spellchecker; PrivateString name;  Public voidSetspellchecker (spellchecker spellchecker) { This. Spellchecker =spellchecker; }        Publicspellchecker Getspellchecker () {returnspellchecker; }        Public voidsetName (String name) { This. Name =name; }        PublicString GetName () {returnname; }        Public voidspellcheck () {spellchecker.checkspelling (); }}

(2) Writing spellchecker. java

 Package Com.tutorialspoint;  Public class spellchecker {   public  spellchecker () {      System.out.println ("Inside spellchecker constructor. "  );   }     Public void checkspelling () {      System.out.println ("Inside checkspelling.")  );   }   }

(3) Writing Mainapp.java

 PackageCom.tutorialspoint;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMainapp { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Beans.xml"); TextEditor te= (TextEditor) context.getbean ("TextEditor");       Te.spellcheck (); }}

(4) Beans.xml

<?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-4.0.xsd "><!--Definition forTextEditor Beans--<bean id= "TextEditor"class= "Com.tutorialspoint.TextEditor"Autowire= "ByName" > <property name= "name" value= "Generic Text Editor"/> </bean> <!--Definition forSpellchecker Beans--<bean id= "Spellchecker"class= "Com.tutorialspoint.SpellChecker" > </bean> </beans>

(5) Main method of running Mainapp.java

Results:

Spring automatically assembles ' Bytype '

This mode specifies automatic assembly by property type. The Spring container is considered beans, and the autowire property Beans in the XML configuration file is set to Bytype. Then, if its type matches exactly one of the beans names in the configuration file, it will attempt to match and concatenate its properties. If a match is found, it injects these beans, otherwise it throws an exception.

For example, in a configuration file, if a bean definition is set to automatically assemble bytype, and it contains the spellchecker property of the spellchecker type, Spring It looks for the bean that defines the name spellchecker and uses it to set this property. You can still use the <property> tab to connect the rest of the properties. The following example illustrates this concept, and you will find no difference from the above example, except that the XML configuration file has been changed.

The Bytype example is no different from the above byname except for the XML configuration, just change the XML configuration so that the corresponding main method can be run:

<?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-4.0.xsd "><!--Definition forTextEditor Beans--<bean id= "TextEditor"class= "Com.tutorialspoint.TextEditor"Autowire= "Bytype" > <property name= "name" value= "Generic Text Editor"/> </bean> <!--Definition forSpellchecker Beans--<bean id= "Spellchecker"class= "Com.tutorialspoint.SpellChecker" > </bean> </beans>

Spring is automatically assembled by the constructor function

This pattern is very similar to Bytype , but it is applied to constructor parameters. The Spring container is considered beans, and the autowire property Beans in the XML configuration file is set to constructor. It then attempts to match and wire the parameters of its constructor with one of the beans names in the configuration file. If a match is found, it injects the beans, otherwise it throws an exception.

For example, in a configuration file, if a bean definition is set to be automatically assembled through a constructor , and it has a constructor with one of the parameters of the spellchecker type, Spring looks for a definition named The spellchecker Bean and uses it to set the parameters of the constructor. You can still use the <constructor-arg> tab to connect the rest of the properties. The following example illustrates this concept.

First, the preparation of Texteditor.java

 PackageCom.tutorialspoint; Public classTextEditor {Privatespellchecker spellchecker; PrivateString name;  PublicTextEditor (spellchecker spellchecker, String name) { This. Spellchecker =spellchecker;  This. Name =name; }        Publicspellchecker Getspellchecker () {returnspellchecker; }        PublicString GetName () {returnname; }        Public voidspellcheck () {spellchecker.checkspelling (); }}

Ii. Preparation of Spellchecker.java

 Package Com.tutorialspoint;  Public class spellchecker {     public  spellchecker () {          System.out.println ("Inside Spellchecker constructor. "  );       }         Public void checkspelling ()       {          System.out.println ("Inside checkspelling.")  );       }  }

Iii. Preparation of Mainapp.java

 PackageCom.tutorialspoint;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMainapp { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Beans.xml"); TextEditor te= (TextEditor) context.getbean ("TextEditor");       Te.spellcheck (); }}

Iv. Preparation of Beans.xml

<?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-4.0.xsd "><!--Definition forTextEditor Beans--<bean id= "TextEditor"class= "Com.tutorialspoint.TextEditor" > <constructor-arg ref= "spellchecker"/> <constructor-arg value= "G Eneric Text Editor "/> </bean> <!--Definition forSpellchecker Beans--<bean id= "Spellchecker"class= "Com.tutorialspoint.SpellChecker" > </bean> </beans>

V. The main method in running Mainapp.java

The results are as follows:

Automatic assembly of Spring (VI)

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.