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

max*_*mus 12 java eclipse spring maven

我在我的程序中使用hsqldb作为db.我想在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>
Run Code Online (Sandbox Code Playgroud)

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

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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

我应该用什么呢?

May*_*res 10

我使用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"
Run Code Online (Sandbox Code Playgroud)

 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"
Run Code Online (Sandbox Code Playgroud)

它使用命名而不是索引构造函数args工作得很好.


卢声远*_* Lu 9

我想你正在使用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>
Run Code Online (Sandbox Code Playgroud)

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