【问题标题】:Spring - Attribute 'name' is not allowed to appear in element 'constructor-arg'Spring - 属性“name”不允许出现在元素“constructor-arg”中
【发布时间】:2012-11-20 02:59:38
【问题描述】:

我在我的程序中使用 hsqldb 作为数据库。我想在 spring 中注入构造函数值。

这是我的豆子:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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">

    <bean id="ConnectionManager" class="at.tuwien.group2.vpm.persistence.ConnectionManager"
        scope="singleton">
        <constructor-arg name="url" value="jdbc:hsqldb:file:vpmDatabasetest" />
        <constructor-arg name="user" value="sa" />
        <constructor-arg name="password" value="" />
    </bean>

我的构造函数看起来像这样:

public ConnectionManager(String url, String user, String password) {
    if(url == null || user == null || password == null) {
        throw new NullPointerException("Paramaeter cannot be null!");
    }
    this.url = url;
    this.user = user;
    this.password = password;
}

但是,当我想执行我得到的代码时:

属性“name”不允许出现在元素“constructor-arg”中

属性“name”不允许出现在元素“constructor-arg”中

我应该改用什么?

【问题讨论】:

  • 事实上它应该可以正常工作。我目前正在我们的项目中查看类似的示例。您使用的是哪个版本的 Spring?

标签: java eclipse spring maven


【解决方案1】:

我猜你使用的是 Sping 2.x。使用 index 属性显式指定构造函数参数的索引:

   <bean id="ConnectionManager" ...>
        <constructor-arg index="0" value="jdbc:hsqldb:file:vpmDatabasetest" />
        <constructor-arg index="1" value="sa" />
        <constructor-arg index="2" value="" />
    </bean>

此外,从 Spring 3.0 开始,您还可以使用构造函数参数名称进行值消歧。

【讨论】:

【解决方案2】:

我在使用 Spring 3.1.2 库时遇到了同样的问题。 我的错误是我使用了旧的模式位置。 当我从

xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                    http://www.springframework.org/schema/aop 
                    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"

 xsi:schemaLocation="http://www.springframework.org/schema/beans 
                     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                     http://www.springframework.org/schema/aop 
                     http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"

使用命名而不是索引构造函数参数效果很好。

【讨论】:

    【解决方案3】:

    只需去掉name属性

    <bean id="ConnectionManager" class="at.tuwien.group2.vpm.persistence.ConnectionManager"
            scope="singleton">
            <constructor-arg value="jdbc:hsqldb:file:vpmDatabasetest" />
            <constructor-arg value="sa" />
            <constructor-arg value="" />
        </bean>
    

    它会起作用的。并获取最新的 Spring 版本,您似乎正在使用一个非常旧的版本。另外,我建议将http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/ 作为有关 Spring 的主要信息来源。

    【讨论】:

      【解决方案4】:

      如果您使用 Maven,请尝试添加较新的 spring-beans 依赖项。我通过更新 jar 依赖项而不更新 xsd 版本来解决此问题。

      【讨论】:

        猜你喜欢
        • 2016-08-11
        • 2015-04-21
        • 2018-03-22
        • 2023-04-08
        • 1970-01-01
        • 1970-01-01
        • 2013-06-03
        • 2014-11-27
        • 1970-01-01
        相关资源
        最近更新 更多