I have created a package com.springcore.autowire.Annotations which contains 4 files Emp.java, Address.java, awannconfig.xml, Test.java .
Emp.java contains class Emp with code as -
package com.springcore.autowire.Annotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Emp {
private Address address;
public Address getAddress() {
return this.address;
}
@Autowired
@Qualifier("address")
public void setAddress(Address address) {
this.address = address;
}
public Emp(Address address) {
this.address = address;
}
@Override
public String toString() {
return "{" + " address='" + getAddress() + "'" + "}";
}
}
Address.java contains class Address with code-
package com.springcore.autowire.Annotations;
public class Address {
private String street;
private String city;
public String getStreet() {
return this.street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
public Address(String street, String city) {
this.street = street;
this.city = city;
}
@Override
public String toString() {
return "{" + " street='" + getStreet() + "'" + ", city='" + getCity() + "'" + "}";
}
}
awannconfig.xml file -
<?xml version="1.0" encoding="UTF-8"?>
<!-- We get this template from documentation -->
<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<bean class="com.springcore.autowire.Annotations.Emp" name="emp" />
<bean class="com.springcore.autowire.Annotations.Address" name="address">
<property name="street" value="PA-24" />
<property name="city" value="Muradnagar" />
</bean>
<!-- more bean definitions go here -->
</beans>
Test.java contains Test class with main method to check use of @Autowired annotations .
package com.springcore.autowire.Annotations;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/springcore/autowire/Annotations/awannconfig.xml");
Emp obj = (Emp) context.getBean("emp");
System.out.println(obj);
}
}
When I run Test.java file it is showing this exception -
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'emp' defined in class path resource [com/springcore/autowire/Annotations/awannconfig.xml]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'address' defined in class path resource [com/springcore/autowire/Annotations/awannconfig.xml]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency
annotations: {}
Please help with this code as I cannot see any error.