春季-属性'名称'不允许出现在元素'constructor-arg'中 [英] Spring - Attribute 'name' is not allowed to appear in element 'constructor-arg'
问题描述
我在程序中使用hsqldb作为数据库.我想在春季注入构造函数值.
I am using hsqldb as a db in my program. I want to inject the constructor values over 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;
}
但是,当我想执行代码时,会得到:
However, when I want to execute the code I get:
属性名称"不允许出现在元素"constructor-arg"中
Attribute 'name' is not allowed to appear in element 'constructor-arg'
属性名称"不允许出现在元素"constructor-arg"中
Attribute 'name' is not allowed to appear in element 'constructor-arg'
我应该用什么代替?
推荐答案
我猜您正在使用Sping2.x.使用 index 属性明确指定构造函数参数的索引:
I guess you are using Sping 2.x. Use the index attribute to specify explicitly the index of constructor arguments:
<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 开始,您还可以使用构造函数参数名称来消除歧义.
Moreover, as of Spring 3.0 you can also use the constructor parameter name for value disambiguation.
这篇关于春季-属性'名称'不允许出现在元素'constructor-arg'中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!