The spring container can automatically assemble the relationships between the mutual collaboration beans, which helps reduce the configuration of the XML without having to write a large number of <constructor-arg> and <property> elements based on the spring application.
Automatic assembly mode:
the following automatic assembly modes are available to instruct the spring container to use automatic assembly dependency injection. Use the Autowire property of the <bean/> element to specify automatic assembly mode for a bean definition.
ByName mode
This pattern requires the automatic assembly of attribute names. The spring container on the appearance of the automatic line property is set to the bean in the byname XML configuration file. It then attempts to match and wire The bean whose properties are defined with the same name in the configuration file. If a match is found, it injects the beans, or it throws an exception.
For example, if a bean definition is set to automatically assemble a byname configuration file, it contains the Aspellchecker property (that is, it has a setspellchecker (...). method), spring looks for a bean definition named spell check and uses it to set the property. Still can use the <property> tag to connect the rest of the properties. The following example will illustrate this concept.
To create a spring application:
Here are the contents of the Texteditor.java file:
Package Com.yiibai;
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 related class file Spellchecker.java content:
Package Com.yiibai;
public class Spellchecker {public
spellchecker () {
System.out.println ("Inside Spellchecker constructor.");
}
public void CheckSpelling () {
System.out.println ("Inside checkspelling.");
}
The following are the contents of the Mainapp.java file:
Package Com.yiibai;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mainapp {public
static void Main (string[] args) {
ApplicationContext context =
new Classpathxml ApplicationContext ("Beans.xml");
TextEditor te = (texteditor) context.getbean ("TextEditor");
Te.spellcheck ();
}
The following are the normal configuration file Beans.xml files:
<?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.yiibai.TextEditor" >
<property name= "spellchecker" ref= "Spellchecker"/> <property name=
"name" value= "Generic Text Editor "/>
</bean>
<!--Definition for spellchecker Bean-->
<bean id=" Spellchecker " class= "Com.yiibai.SpellChecker" >
</bean>
</beans>
However, if you want to use automatic assembly "ByName", the 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.yiibai.TextEditor" Autowire= "ByName" >
<property name= "name" value= "Generic Text Editor"/>
</bean>
<!-- Definition for spellchecker Bean-->
<bean id= "spellchecker" class= "Com.yiibai.SpellChecker" >
</bean>
</beans>
After creating the source code and the bean configuration file, let's run the application. If all goes well, this will print the following information:
Inside Spellchecker constructor.
Inside checkspelling.
Bytype mode
the type of attribute is automatically assembled. The spring container has a Autowire property set to the Bean in the bytype XML configuration file on the skin. Then it tries to match and concatenate a property if its type has exactly the same bean name as a matching configuration file. If a match is found, it injects the beans, or it throws an exception.
For example, if a bean definition is set to automatically assemble the Bytype configuration file, it contains the Aspellchecker property of the spelling checker, the spring lookup name is a bean definition for the spelling checker, and it is used to set the property. You can still use the <property> tab wiring for the rest of the properties. The following example illustrates this concept and finds no difference from the above example, except that the XML configuration file has been changed.
Again, to create a spring application description:
Here are the contents of the Texteditor.java file:
Package Com.yiibai;
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 related class file Spellchecker.java content:
Package Com.yiibai;
public class Spellchecker {public
spellchecker () {
System.out.println ("Inside Spellchecker constructor.")
Public
void CheckSpelling () {
System.out.println ("Inside checkspelling.");
}
The following are the contents of the Mainapp.java file:
Package Com.yiibai;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mainapp {public
static void Main (string[] args) {
ApplicationContext context =
new ClassPath Xmlapplicationcontext ("Beans.xml");
TextEditor te = (texteditor) context.getbean ("TextEditor");
Te.spellcheck ();
}
The following are the normal configuration file Beans.xml files:
<?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.yiibai.TextEditor" >
<property name= "spellchecker" ref= "Spellchecker"/> <property name=
"name" value= "Generic Text Editor "/>
</bean>
<!--Definition for spellchecker Bean-->
<bean id=" Spellchecker " class= "Com.yiibai.SpellChecker" >
</bean>
</beans>
However, if you want to use automatic assembly "Bytype", the 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.yiibai.TextEditor" Autowire= "Bytype" >
<property name= "name" value= "Generic Text Editor"/>
</bean>
<!-- Definition for spellchecker Bean-->
<bean id= "spellchecker" class= "Com.yiibai.SpellChecker" >
</bean>
</beans>
When the source code is created and the bean configuration file is complete, let's run the application. If all goes well, this will print the following information:
Inside Spellchecker constructor.
Inside checkspelling.
Automatically assembled by constructors
This pattern is very similar to Bytype, but it is applied to constructor parameters. The Spring container Autowire properties on the appearance of the bean in the XML configuration file. Then it tries to match and connect its constructor parameters to the bean name with only one configuration file. If a match is found, it injects the beans, or it throws an exception.
For example, if a bean definition is set to be automatically assembled by constructing a configuration file, it has a constructor that is one of the parameters of the spelling checker, and the spring looks for a bean definition namedspellchecker and uses it to set the parameters of the constructor. You can still use the <constructor-arg> tab to connect the remaining parameters. The following example will illustrate this concept.
Here are the contents of the Texteditor.java file:
Package Com.yiibai;
public class TextEditor {
private spellchecker spellchecker;
private String name;
Public TextEditor (Spellchecker spellchecker, String name) {
this.spellchecker = spellchecker;
this.name = name;
Public Spellchecker Getspellchecker () {return
spellchecker;
}
Public String GetName () {return
name;
}
public void SpellCheck () {
spellchecker.checkspelling ();
}
}
Here is another related class file Spellchecker.java content:
Package Com.yiibai;
public class Spellchecker {public
spellchecker () {
System.out.println ("Inside Spellchecker constructor.")
Public
void CheckSpelling ()
{
System.out.println ("Inside checkspelling.");
}
The following are the contents of the Mainapp.java file:
Package Com.yiibai;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class Mainapp {public
static void Main (string[] args) {
ApplicationContext context =
new ClassPath Xmlapplicationcontext ("Beans.xml");
TextEditor te = (texteditor) context.getbean ("TextEditor");
Te.spellcheck ();
}
The following are the normal configuration file Beans.xml files:
<?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.yiibai.TextEditor" >
<constructor-arg ref= "spellchecker"/>
<constructor-arg value= "Generic Text Editor"/>
< /bean>
<!--Definition for spellchecker Bean-->
<bean id= "Spellchecker" Com.yiibai.SpellChecker ">
</bean>
</beans>
However, if you want to use automatic assembly by the constructor, the 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.yiibai.TextEditor" Autowire= "Constructor" >
<constructor-arg value= "Generic Text Editor"/>
</bean>
<!- -Definition for spellchecker Bean-->
<bean id= "spellchecker" class= "Com.yiibai.SpellChecker" >
</bean>
</beans>
After creating the source code and the bean configuration file, let's run the application. If all goes well, this will print the following information:
Inside Spellchecker constructor.
Inside checkspelling.
In addition, there are autodetect and the default way, here is no longer detailed.
limitations of automatic assembly:
the best effect of automatic assembly is that it is always used in a project. If the automatic assembly is not used normally, it may be confused as a developer can use it to connect only one or two bean definitions. However, automatic assembly can significantly reduce the need to specify properties or constructor parameters, but you should consider the limitations and drawbacks of automatic assembly before using them.