属性“default-dependency-check”不允许出现在元素'beans'中

时间:2016-05-31 09:31:15

标签: spring

我在这里遇到奇怪的错误:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">

</beans>

我删除了所有bean,但仍然有错误:

Attribute "default-dependency-check" is not allowed to appear in element 'beans'

修改

问题在于DOCTYPE,当我删除它时没关系,但为什么呢?

1 个答案:

答案 0 :(得分:0)

在Spring 3.0中不推荐使用依赖项检查属性。您可以使用构造函数注入而不是setter注入来确保设置正确的属性,或者使用@Required注释创建setter方法,其中您需要在类中进行依赖性检查

例如:在Person类中强制使用名称依赖

import org.springframework.beans.factory.annotation.Required;

public class Person{
    private int id;
    private String name;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    @Required
    public void setName(String name) {
        this.name = name;
    }
}