The Spring autowire by constructor xml example shows you how to use autowire constructor xml configuration. If we enable the autowire constructor, Spring will look up exactly one bean of property in the container then autowire a property using constructor with arguments. If there are multiple beans of the same type, Spring will throw NoUniqueBeanDefinitionException exception. Whereas the property will not be initialized if there are no matching beans. The autowiring constructor is enquivalent with the autowiring byType.
Let’s begin:
Create beans for autowiring
We create two beans Fruit and Country. The Fruit bean has a property of Country and a constructor with arguments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.javabycode.spring.core.autowired; public class Fruit { private Country country; public Fruit(Country country) { this.country = country; } @Override public String toString() { return "Fruit{" + "country=" + country + '}'; } } |
Notice: Don’t forget the constructor because we are using constructor 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 |
<bean class="com.javabycode.spring.core.autowired.Fruit"> <constructor-arg 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 Constructor
Here, we must enable the spring autowire by constructor. The wiring is done by constructor, 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 |
<bean class="com.javabycode.spring.core.autowired.Fruit" autowire="constructor"/> <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 by constructor 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{country=Country{name='USA'}} |
That’s all on the Spring autowire by constructor xml example
References
Spring Autowire Doc
Download complete source code, click link below
spring-autowire-by-constructor-xml-example.zip (265 downloads)