我创建了一个包 com.springcore.autowire.Annotations,其中包含 4 个文件 Emp.java、Address.java、awannconfig.xml、Test.java。
Emp.java 包含类 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 包含带有代码的类 Address-
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 包含带有主要方法的测试类,用于检查@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: {}
请帮助处理此代码,因为我看不到任何错误。
答案 0 :(得分:1)
这个问题是由于 Address 类中的参数化构造函数造成的,当你自动装配它时,它的构造函数需要使用两个参数来创建一个 bean 或对象,因为没有可用的默认构造函数。 >
问题的解决方案。
保持 const [filtered, setFiltered] = useState([]);
类不变:
Emp
保留 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
将 awannconfig.xml 更新为:
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() + "'" + "}";
}
}
在我的控制台上输出。
<?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>
记住:不要混合使用构造函数或setter注入,因为这会导致混淆和不必要的错误。
答案 1 :(得分:-1)
使用构造函数 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>