This spring autowire byType xml example shows you how to use Spring autowire byType. If we enable the autowire byType, Spring will attempts to find out exactly a bean which is matching with the property and autowire that property automatically. Whereas Spring will throw a NoUniqueBeanDefinitionException exception if there are multiple beans of the same type.
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 element which has a same name as name of the property and a ref attribute that references the bean to be wired.
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 ByType
Here, we must enable the spring autowire by type. The wiring is done by type, so that if there is a bean with the same type 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="byType"> <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 byType 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 byType xml example
References
Spring Autowire Doc
Download complete source code, click link below
spring-autowire-byType-xml-example.zip (254 downloads)