0% found this document useful (0 votes)
26 views

Autowiring Example

The document contains code for an Employee class with name and age properties, an Address class with street, city, and state properties, and a TestAuto class that uses Spring to inject dependencies and print an employee's details. The Spring configuration defines beans for an Employee with properties set and an Address bean that is set as a property of the Employee bean.

Uploaded by

Sushma Sumant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Autowiring Example

The document contains code for an Employee class with name and age properties, an Address class with street, city, and state properties, and a TestAuto class that uses Spring to inject dependencies and print an employee's details. The Spring configuration defines beans for an Employee with properties set and an Address bean that is set as a property of the Employee bean.

Uploaded by

Sushma Sumant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

TestAuto.

java

package com.spr;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAuto {

public static void main(String[] args) {


// TODO Auto-generated method stub
ApplicationContext con = new
ClassPathXmlApplicationContext(("spring.xml"));

Employee employee = (Employee) con.getBean("Employee");

System.out.println("The name of Employee is : " + employee.getName());


System.out.println("The age of Employee is : " + employee.getAge());
System.out.println(employee);

Address.java
package com.spr;

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;
}
public Address(String street, String city, String state) {
super();
this.street = street;
this.city = city;
this.state = state;
}
public Address() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Address [street=" + street + ", city=" + city + ", state=" +
state + "]";
}

Employee.java

package com.spr;

public class Employee {


private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Employee(String name, int age, Address address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
private Address address;
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + ", address=" +
address + ", getName()=" + getName()
+ ", getAge()=" + getAge() + ", getAddress()=" +
getAddress() + "]";
}
public Employee() {
super();
// TODO Auto-generated constructor stub
}
}

Spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="Employee" class="com.spr.Employee" autowire="constructor">

<property name="name" value="Bob" />


<property name="age" value="28" />

</bean>

<bean id="Address" class="com.spr.Address">

<property name="street" value="JSD Street" />


<property name="city" value="Mumbai" />
<property name="state" value="Maharashtra"></property>
</bean>

</beans>

Output:

You might also like