Spring Autowiring by Name
Spring provides a way to automatically detect the relationships between various beans. This can be done by declaring all the bean dependencies in Spring configuration file.
Using autowiring by Name Spring looks up the configuration file for a matching bean name. If found, this bean is injected in the property. However, if no such bean is found, an error is raised.
With autowire by name enabled, you do not need to declares the property tag anymore. As long as the "address" bean is the same name as the property of employee" bean , which is "address", Spring will wire it automatically.
Bean definitionsapplication-context.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.javacodestuffs.spring.app" /> <bean id="employee" class="com.javacodestuffs.spring.app.model.Employee"
autowire="byName"> <property name="name" value="Anna Pal"/> </bean> <bean id="address" class="com.javacodestuffs.spring.app.model.Address" > <property name="addressLine1" value="Plot No12,Near Sancheti" /> <property name="addressLine2" value="University Road" /> <property name="city" value="Pune" /> <property name="state" value="Maharashtra" /> <property name="country" value="India" /> <property name="pincode" value="411005" /> </beans>
Employee.java
package com.javacodestuffs.spring.app.model;
public class Employee {
private String name;
private Address address;
//@getter and Setter
}
Address.java
package com.javacodestuffs.spring.app.model;
public class Address {
private String addressLine1;
private String addressLine2;
private String city;
private String state;
private String country;
private int pincode;
// @Getter and @Setter
}
Then we have Main class
package com.javacodestuffs.spring.app; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; package com.javacodestuffs.spring.app.model.Employee; public class EmployeeTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "application-context.xml" }); Employee employee = (Employee) context.getBean("employee"); System.out.println(employee.getName()); System.out.println(employee.getAddress().getAddressLine1()); System.out.println(employee.getAddress().getAddressLine2()); System.out.println(employee.getAddress().getAddresCity()); System.out.println(employee.getAddress().getAddresPincode()); } } output: Anna Pal Plot No12,Near Sancheti University Road Pune 411005
Question/Articles related Autowired annotation in Spring.
Autowired annotation in Spring
What is the use of autowired annotation in spring?
How to do setter injection in spring using annotations?
How to enable MVC annotations in spring?
Which is the spring boot annotation used for caching auto-configuration?
Spring Boot with multiple databases – Example
How to load multiple Spring bean configuration file?
Spring Auto-Wiring Beans.
Spring Autowiring by Constructor – Example
Spring Autowiring by Type – Example
Spring Autowiring @Qualifier – Example
In this article, we have seen the Spring Autowiring by Name with examples. All source code in the article can be found in the GitHub repository.
0 Comments
Post a Comment