繁体   English   中英

spring 与 java 中的自动装配注释错误

[英]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 包含 class Emp,代码为 -

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 包含 class 地址与代码-

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 文件 -

<?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 包含测试 class 和检查 @Autowired 注释使用的主要方法。

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);

    }
}

当我运行 Test.java 文件时,它显示了这个异常 -

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: {}

请帮助处理此代码,因为我看不到任何错误。

问题的出现是由于地址 class 中的参数化构造函数,其构造函数需要两个参数来创建 bean 或 object,因为没有可用的默认构造函数。

问题的解决方案。

保持Emp class 原样:

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 class 为:

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更新为:

<?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 在我的控制台上。

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

请记住:不要混合使用构造函数setter注入,因为这会导致混淆和不必要的错误。

使用构造函数 arg 依赖注入更改 bean 配置,您在 emp bean 定义中错过了这一点。 如下更改 bean 定义。

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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