@Autowired annotation – We can use Spring @Autowired annotation for spring bean autowiring.
@Autowired annotation can be applied on variables and methods for autowiring byType.
We can also use @Autowired annotation on constructor for constructor based spring autowiring.
For @Autowired annotation to work, we also need to enable annotation based configuration in spring bean configuration file.
This can be done by context:annotation-config element
To implement this we do not need setter method of address.
Remove setter of address from student.
Use autowire like this. Student and spconfig.xml will look like this.
<context:annotation-config />
<bean id="address" class="com.javabykiran.Address">
<property name="landmark" value="park plaza"></property>
</bean>
<bean id="stu" class="com.javabykiran.Student" scope="singleton">
<property name="age" value="30"></property>
<property name="mobileNos">
<list>
<value>8888809416</value>
<value>9552343698</value>
</list>
</property>
</bean>
package com.javabykiran;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
public class Student {
@Autowired
Address address;
int age;
ArrayList<String> mobileNos;
public Address getAddress() {
return address;
}
// keep same code for age and mobileNos.
}