14

プログラムで hsqldb をデータベースとして使用しています。春にコンストラクターの値を注入したい。

ここに私の豆があります:

<?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' に表示できません

代わりに何を使用すればよいですか?

4

4 に答える 4

11

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"

インデックス付きのコンストラクター引数の代わりに名前付きを使用するとうまくいきました。

于 2013-08-12T11:27:30.640 に答える
11

あなたはSping 2.xを使用していると思います。index属性を使用して、コンストラクタ引数のインデックスを明示的に指定します:</p>

   <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 以降では、コンストラクターのパラメーター名を使用して値を明確にすることもできます。

于 2012-12-02T13:48:44.463 に答える
0

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.

于 2014-08-26T16:07:33.987 に答える