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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13669798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:31:55  来源:igfitidea点击:

Spring - Attribute 'name' is not allowed to appear in element 'constructor-arg'

javaeclipsespringmaven

提问by maximus

I am using hsqldb as a db in my program. I want to inject the constructor values over spring.

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

Here is my bean:

这是我的豆子:

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

My Constructor looks like that:

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

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

However, when I want to execute the code I get:

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

Attribute 'name' is not allowed to appear in element 'constructor-arg'

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

Attribute 'name' is not allowed to appear in element 'constructor-arg'

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

What should I use instead?

我应该用什么代替?

采纳答案by u290629

I guess you are using Sping 2.x. Use the indexattribute to specify explicitly the index of constructor arguments:

我猜你正在使用 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>

Moreover, as of Spring 3.0you can also use the constructor parameter name for value disambiguation.

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

回答by Mayoares

I had same problem using Spring 3.1.2 libraries. My mistake was that I used old schema location. When I changed from

我在使用 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"

to

 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"

it worked fine to use named instead of indexed constructor-args.

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

回答by Evgeniy Dorofeev

Simply remove the name attribute

只需删除 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>

it will work. And get the latest Spring version, you seem to be using a very old one. Also, I would suggest http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/as the primary source of information about Spring.

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

回答by chenrui

If you use Maven, try to add the newer spring-beans dependency. I fix this issue by updating the jar dependency without updating the xsd version.

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