Pivotal Knowledge Base

Follow

Optional Name Attribute in the Element Error: "No Matching Constructor Found in Bean"

Environment

 Product  Version
 Spring  All

Symptom

You see a no matching constructor found in bean exception when using a code similar to this example:

<bean id="foo" class="com.foo.Foo"> 

<constructor-arg name="string1" value="string1" />

   <constructor-arg name="string2" value="string2" />

   <constructor-arg name="string3" value="string3" />

  <constructor-arg name="int1" value="123" />

</bean>

package com.foo;
 public class Foo {
   public Foo() {
   }
 public Foo(String string1, String string2, String string3, int int1) { 
  super();
  this.string1 = string1;
  this.string2 = string2;
  this.string3 = string3;
  this.int1 = int1;
   }
 public String getString1() {
  return string1;
   }
 public String getString2() {
  return string2;
   }

 public String getString3() {
  return string3;
   }
 public int getInt1() {
  return int1;
  }

 private String string1;
 private String string2;
 private String string3;
 private int int1;
  }

The exception for this example:

Exception in thread "main"
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'foo' defined in class path resource [test.xml]: 4 constructor arguments specified but no matching constructor found in bean 'foo' (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
at
org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor
(ConstructorResolver.java:175)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor
(AbstractAutowireCapableBeanFactory.java:993)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance
(AbstractAutowireCapableBeanFactory.java:897)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean
(AbstractAutowireCapableBeanFactory.java:485)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean
(AbstractAutowireCapableBeanFactory.java:456)
at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject (AbstractBeanFactory.java:291)

Resolution

Spring Framework 3.0.x has an optional name attribute which allows you to specify constructor parameter values using the name of the parameter, instead of using the index value of the parameter in the list of constructor parameters. The parameter name resolution is performed using Spring's ASM-based LocalVariableTableParameterNameDiscoverer class.

However, a class compilation must be done with the debugging option enabled (that is, using javac -g or javac -g:vars). This causes an issue if you use the optional name attribute with third party classes which have not been compiled with the required debug information.

To solve this issue, use SpringSource Tool Suite (STS) 2.3.2 during testing. This version has the debugging option active by default.

To verify or change this option: Right-click the SpringSource Tool Suite, click Properties, then select the Java Compiler set of options.

In the Classfile Generation section, the Add variable attributes to generated class files (used by the debugger) option controls this functionality. Enabling this option adds the required information about the constructor parameter names to the resulting .class file.

Comments

Powered by Zendesk