May 14, 2021 Spring
byName
This pattern specifies automatic assembly by the property name. T he Spring container is considered beans, and the auto-wire property of beans in the XML profile is set to byName. I t then tries to match and connect its properties to beans defined as the same name in the profile. 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 definition named spellChecker and uses it to set that property. Y ou can still connect the rest of the properties using the .lt;property.gt; tag. The following example illustrates this concept.
Let's use the Eclipse IDE in the right place, and then follow these steps to create a Spring application:
Steps | Describe |
---|---|
1 | Create a project named SpringExample, and create a package com.tutorialspoint in the src folder of the project you have created. |
2 | Use the Add External JARs option to add the Spring library you want, as described in the Spring Hello World Example section. |
3 | Create Java-like TextEditor, SpellChecker, and MainApp in the com.tutorialspoint package. |
4 | Create Beans' profile Beans in the src .xml. |
5 | The final step is to create the contents of all Java files and bean profiles and run the application, as explained below. |
Here is the contents of .java textEditor 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's what's in another class-dependent .java SpellChecker:
package com.tutorialspoint;
public class SpellChecker {
public SpellChecker() {
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}
}
Here's what .java mainApp files are:
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();
}
}
Here is the profile Beans file .xml normal:
<?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-assemble "byName", your XML profile will become 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="byName">
<property name="name" value="Generic Text Editor" />
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
</beans>
Once you're done creating the source code and bean profile, we can run the application. If your application is all right, it will print the following message:
Inside SpellChecker constructor.
Inside checkSpelling.