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

eclipse, java, maven, spring

Solution

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>

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

Problem

I am using hsqldb as a db in my program. I want to inject the constructor values over 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' Attribute 'name' is not allowed to appear in element 'constructor-arg' What should I use instead?

Original source