I'm getting value as null in console if i use annotation to get the output, this has also occured in previous programs also & if i use context:annotation-config in xml file i will get exceptions. please help.
This is Address class
package com.springcore.auto.wire.annotation;
public class Address
{
private String street;
private String city;
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;
}
@Override
public String toString()
{
return "Address [street=" + street + ", city=" + city + "]";
}
}
This is Emp class
package com.springcore.auto.wire.annotation;
import org.springframework.beans.factory.annotation.Autowired;
public class Emp
{
@Autowired
private Address address;
public Address getAddress()
{
return address;
}
public void setAddress(Address address)
{
System.out.println("Setting value using set");
this.address = address;
}
public Emp(Address address)
{
super();
this.address = address;
System.out.println("Setting value using constructor");
}
public Emp()
{
super();
}
@Override
public String toString()
{
return "Emp [address=" + address + "]";
}
}
This is Main method class
package com.springcore.auto.wire.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("com/springcore/auto/wire/annotation/autoconfig.xml");
Emp emp1 = context.getBean("emp1", Emp.class);
System.out.println(emp1);
}
}
This is my xml file
<?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"
xmlns:p="=http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="com.springcore.auto.wire.annotation.Address" name="tempaddress" >
<property name="street" value="Temp street" />
<property name="city" value="Mysore" />
</bean>
<bean class="com.springcore.auto.wire.annotation.Emp" name="emp1" />
</beans>
i'm getting this output :
Emp [address=null]