简体   繁体   中英

Autowired annotation error in spring with java

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.

The issue is coming due to parameterised constructor in the Address class whose constructor expects two parameters to be used to create a bean or object when you autowire it as there is no default constructor available.

Solution of the issue.

Keep Emp class as it is:

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() + "'" + "}";
    }

}

Keep Address class it is:

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() + "'" + "}";
    }

}

Update awannconfig.xml to this:

<?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">
         <constructor-arg value="PA-24" type="String"/>
         <constructor-arg value="Muradnagar" type="String"/>
    </bean>
    <!-- more bean definitions go here -->
</beans>

Output on my console.

{ address='{ street='PA-24', city='Muradnagar'}'}

Remember: Don't mix both constructor or setter injection as this will lead to confusion and unwanted errors.

Change bean configuration with constructor arg dependency injection, you missed that in emp bean definition. Change bean definition like below.

<bean class="com.springcore.autowire.Annotations.Emp" id="emp">
 <constructor-arg ref = "address"/>
</bean>

<bean class="com.springcore.autowire.Annotations.Address" id="address">
        <property name="street" value="PA-24" />
        <property name="city" value="Muradnagar" />
</bean>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM