The Spring autowire byName xml example shows you how to use Spring autowire by name. When the autowire by name mode is enabled, Spring attempts to find out a bean in the spring bean configuration file with the same name as the property that needs to be autowired. If a bean found with with id as property name then that class object will be injected into that property by calling setter injection. For example, suppose we have a Fruit bean that exposes a Country property. Spring container will look for the Country bean, if one exists and wire it automatically. If no matching bean is found the that property remains un-wired, but never throws any exception.
Create beans for autowiring
We create two beans Fruit and Country. The Fruit bean has a property of Country and this property has a setter method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.javabycode.spring.core.autowired; public class Fruit { private String name; private Country country; public void setName(String name) { this.name = name; } public void setCountry(Country country) { this.country = country; } @Override public String toString() { return "Fruit{" + "name='" + name + '\'' + ", country=" + country + '}'; } } |
Notice: Don’t forget the setter method because we are using setter based DI otherwise the injection will not work
Next, we create Country bean which is autowired into the Fruit bean.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.javabycode.spring.core.autowired; public class Country { private String name; public void setName(String name) { this.name = name; } @Override public String toString() { return "Country{" + "name='" + name + '\'' + '}'; } } |
Spring bean Explicitly wiring Configuration
Now, we can declare the bean explicitly wiring using the
1 2 3 4 5 6 7 8 |
<bean id="fruit" class="com.javabycode.spring.core.autowired.Fruit"> <property name="name" value="Apple"/> <property name="country" ref="country"/> </bean> <bean id="country" class="com.javabycode.spring.core.autowired.Country"> <property name="name" value="USA"/> </bean> |
Spring bean Autowire By Name
Here, we must enable the spring autowire by name. The wiring is done by name, so that if there is a bean with the same name as the property in the class, it is wired automatically.
1 2 3 4 5 6 7 |
<bean id="fruit" class="com.javabycode.spring.core.autowired.Fruit" autowire="byName"> <property name="name" value="Apple"/> </bean> <bean id="country" class="com.javabycode.spring.core.autowired.Country"> <property name="name" value="USA"/> </bean> |
Demo Spring application
Create the main class to test the Spring autowire byName xml example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.javabycode.spring.core.autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MySpringApplication { public static void main(String... args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); Fruit fruit = context.getBean(Fruit.class); System.out.println(fruit); } } |
Run the main above, the console will print the following message
1 |
Fruit{name='Apple', country=Country{name='USA'}} |
That’s all on the Spring autowire byName xml example
References
Spring Autowire Doc
Download complete source code, click link below
spring-autowire-byname-xml-example-1.zip (267 downloads)