Spring auto assemble byType
Spring auto assembly byType
This mode specifies automatic assembly by attribute type. The Spring container is regarded as "beans". In the "XML" configuration file, the "autowire" attribute of "beans" is set to "byType". Then, if its , type , exactly matches one of the , beans , names in the configuration file, it will try to match and connect its properties. If a match is found, it will inject these} beans, otherwise it will throw an exception.
For example, in the configuration file, if a bean definition is set to auto assemble {byType, and it contains the} spellchecker} attribute of {spellchecker}, then} Spring will find the} bean of type} spellchecker}, and use it to set this attribute. You can still connect the remaining properties using the < property > tag. The following example will illustrate this concept. You will find that it is no different from the above example, except that the XML configuration file has been changed.
Let's use the Eclipse IDE in the right place, and then follow the steps below to create a Spring application:
step | describe |
---|---|
1 | Create a project named "SpringExample", and create a package "com. Com" in the "src" folder of the created project tutorialspoint. |
2 | Use the "Add External JARs" option to add the required "Spring" library, which is described in the "Spring Hello World Example" chapter. |
3 | On , com Create the # Java # classes # TextEditor, SpellChecker # and # MainApp # in the tutorialspoint # package. |
4 | Create the configuration file of # Beans # in the # src # folder # Beans xml. |
5 | The final step is to create the contents of all Java Bean configuration files and bean configuration files and run the application, as explained below. |
This is texteditor Contents of Java} file:
package com.tutorialspoint; public class TextEditor { private SpellChecker spellChecker; private String name; public void setSpellChecker( SpellChecker spellChecker ) { this.spellChecker = spellChecker; } public SpellChecker getSpellChecker() { return spellChecker; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void spellCheck() { spellChecker.checkSpelling(); } }
Here is another dependent class file, spellchecker Contents of Java:
package com.tutorialspoint; public class SpellChecker { public SpellChecker(){ System.out.println("Inside SpellChecker constructor." ); } public void checkSpelling() { System.out.println("Inside checkSpelling." ); } }
Here is mainapp Contents of Java} file:
package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); TextEditor te = (TextEditor) context.getBean("textEditor"); te.spellCheck(); } }
The following is the normal configuration file: beans XML 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"> <!-- Definition for textEditor bean --> <bean id="textEditor" class="com.tutorialspoint.TextEditor"> <property name="spellChecker" ref="spellChecker" /> <property name="name" value="Generic Text Editor" /> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> </bean> </beans>
However, if you want to use auto assembly "byType", your XML} configuration file will be as follows:
<?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"> <!-- Definition for textEditor bean --> <bean id="textEditor" class="com.tutorialspoint.TextEditor" autowire="byType"> <property name="name" value="Generic Text Editor" /> </bean> <!-- Definition for spellChecker bean --> <bean id="SpellChecker" class="com.tutorialspoint.SpellChecker"> </bean> </beans>
Once you have finished creating the source code and the bean configuration file, we can run the application. If your application is all right, it will print the following message:
Inside SpellChecker constructor. Inside checkSpelling.