Spring Automatic Assembly

Source: Internet
Author: User

Today's mood is more complex, long story!!! Let's study hard. Address: https://www.w3cschool.cn/wkspring/o1qy1h9q.html

"ByName" of automatic assembly

Or an example of the previous editor calling the spelling checker:

Spellchecker.java:

 Package Com.lee.byName;  Public class spellchecker {    public  spellchecker () {        System.out.println ("Inside spellchecker Constructor ");    }          Public void checkspelling () {        System.out.println ("Inside checkspelling");}    }

Texteditor.java:

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

Mainapp.java:

 PackageCom.lee.byName;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMainapp { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Beans.xml"); TextEditor TD= (TextEditor) context.getbean ("TextEditor");    Td.spellckeck (); }}

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-3.0.xsd ">    class=" Com.lee.byName.TextEditor "        autowire=" ByName " >        <property name= "name" value= "Genric Text Editor" ></property>    </bean>     Class= "Com.lee.byName.SpellChecker" >    </bean></beans>

The Autowire property of beans in the XML configuration file is set to ByName, and its properties are matched and connected with beans defined in the configuration file as the same name. My understanding is that:

<bean id= "spellchecker" class= "Com.lee.byName.SpellChecker" > here's ID name spellchecker As defined in the previous texteditor, the same private spellchecker spellchecker;

"Bytype" of automatic assembly

The only difference here is the Beans.xml configuration file:

<?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 ">    class=" Com.lee.byType.TextEditor "        autowire=" Bytype " >        <property name= "name" value= "Genric Text Editor" ></property>    </bean>     Class= "Com.lee.byType.SpellChecker" >    </bean></beans>

The Autowire property of the beans in the XML configuration file is set to Bytype, and then the type matches and joins the same in the beans name in the configuration file. My understanding is that:

<bean id= "spellchecker" class= "Com.lee.byName.SpellChecker" > here's ID name spellchecker As defined in the previous texteditor, the same private spellchecker spellchecker;

byname: Property name

bytype: attribute type

Automatic assembly "Automatic assembly of Constructors"

Spring Constructor Auto-assembly is very similar to Bytype, but it is applied to constructor parameters, and beans Autowire in the XML configuration file is set to constructor.

Spellchecker.java:

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

Textedictor.java:

 PackageCom.lee.constructor; Public classTextedictor {Privatespellchecker spellchecker; PrivateString name;  Publictextedictor (spellchecker spellchecker,string name) { This. spellchecker=spellchecker;  This. Name =name; }     Publicspellchecker Getspellchecker () {returnspellchecker; }     PublicString GetName () {returnname; }     Public voidspellcheck () {spellchecker.checkspelling (); }    }

Mainapp.java:

 PackageCom.lee.constructor;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMainapp { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Beans3.xml"); Textedictor TD= (textedictor) context.getbean ("TextEditor");    Td.spellcheck (); }}

Beans3.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-3.0.xsd ">    class=" Com.lee.constructor.TextEdictor "        autowire=" Constructor ">        <constructor-arg value=" genric Text Editor "/>    </bean>    class = "Com.lee.constructor.SpellChecker" >    </bean></beans>

Code Cloud: Https://gitee.com/lemon_le/w3-Spring/tree/master/AutoBeans

Spring Automatic Assembly

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.