Multiple Constructors with Different Parameters Springs

Majid Hussain picture Majid Hussain · Jul 15, 2015 · Viewed 9.2k times · Source
public class DisplayMessage {
String Message;
private String Message1;
private String Message2;
public DisplayMessage(String Message, String Message1) {
    this.Message=Message;
    this.Message1=Message1;
}
public DisplayMessage(String Message2){
    this.Message2=Message2;
}
public void display(){
    System.out.println(Message);
    System.out.println(Message1);
    System.out.println(Message2);
}
}

In above code Im injecting using 2 constructors with different parameters. I inserted values as below in xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        <bean id="displayMessage" class="org.demo.DisplayMessage">
            <constructor-arg value="Hi Welcome To Springs"></constructor-arg>
            <constructor-arg value="Hello"></constructor-arg>
            <constructor-arg value="I am Second"></constructor-arg>
        </bean>
 </beans> 

When im using it using ClientApp Im getting lots of errors as

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'displayMessage' defined in class path resource [springbeans.xml]: 3 constructor arguments specified but no matching constructor found in bean 'displayMessage' (hint: specify index and/or type arguments for simple parameters to avoid type ambiguities)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:181)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:925)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:835)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at org.demo.ClientApp.main(ClientApp.java:9)

Answer

Little Santi picture Little Santi · Jul 15, 2015

It's simple: Your class has a constructor with 2 arguments, but you have declared 3 arguments in your context file. Remove one of them.