What is wiring?
Connecting two different objects is called as wiring or establishing the dependency between two objects
is called wiring. In other words bean wiring is the process of combining beans with Spring container.
Spring has come with a feature known as autowire. Using autowire beans can be wired automatically.
Autowire attribute takes different values. They are:
- no/default: If autowire="no" the spring container check whether all the properties are mapped or not. If not mapped it will check autowire value. As it is "no", it will not take care of autowiring.
- byName: If autowire="byName" spring container checks whether all the properties are mapped or not. If mapped autowiring will be performed. If not mapped it takes the property name and checks if there is any spring bean id available with that property name. If it is available it will establish the dependencies.
- byType: If autowire="byType" spring container checks whether all the properties are mapped or not. If not matching it checks the autowire attribute. As it is "byType" spring container will check is there any spring whose data type matches with the property data type. If it matches it will establish the dependencies.
- constructor: This is analogous to byType, but applies to constructor arguments.
First create an Address bean under src as shown below.
File: com.ram.beans.Address.java
package com.ram.beans; public class Address { private String street; private String city; private String state; public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } }
Next create a Person bean under src as shown below.
File: com.ram.beans.Person.java
package com.ram.beans; public class Person { private String pid; private String name; private Address address; public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } }
Next create applicationContext.xml file under src. The file is given below.
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="addr" class="com.ram.beans.Address"> <property name="street" value="Richmond"></property> <property name="city" value="Melbourne"></property> <property name="state" value="Victoria"></property> </bean> <bean id="person" class="com.ram.beans.Person" autowire="byName"> <property name="pid" value="1"></property> <property name="name" value="Ram"></property> </bean> </beans>
Next create a main class under src as shown.
File: com.ram.app.AutoWireExample.java
package com.ram.app; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ram.beans.Person; public class AutoWireExample { public static void main(String[] args) { ApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = container.getBean("person", Person.class); System.out.println("Person Id = "+person.getPid()); System.out.println("Person Name = "+person.getName()); System.out.println("Street = "+person.getAddress().getStreet()); System.out.println("City = "+person.getAddress().getCity()); System.out.println("State = "+person.getAddress().getState()); } }
<bean id="person" class="com.ram.beans.Person" autowire="byType">
No comments:
Post a Comment