4
votes

I have following java class:

package configuration;
import common.config.ConfigurationService;

public class AppConfig {

    private ConfigurationService configurationService;  

    public AppConfig(ConfigurationService configurationService){
        this.configurationService = configurationService;
    }

also

public class ConfigurationServiceImpl
  implements ConfigurationService, Runnable
{...

and the application context file is as follows:

<bean id="appConfig" class="configuration.AppConfig" scope="prototype">
        <constructor-arg ref="configurationService"></constructor-arg>
    </bean>

    <bean id="configurationService"  class="common.config.ConfigurationServiceImpl" scope="singleton" />
    <bean id="propertyPlaceholderConfigurer"    class="common.config.PropertyPlaceholderConfigurer">
        <constructor-arg ref="configurationService" />
        <constructor-arg ref="serviceName" />
    </bean>

     <bean id="serviceName" class="java.lang.String"><constructor-arg value="filter"/></bean>  

during initialization I am getting following error and my beans are not initialized:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appConfig' defined in class path resource [conf/applicationContext.xml]: 1 constructor arguments specified but no matching constructor found in bean 'appConfig' (hint: specify index and/or type arguments for simple parameters to avoid type ambiguities)

While Spring injection works if I modify the java class code as follows:

package configuration;
import common.config.ConfigurationServiceImpl;

    public class AppConfig {

        private ConfigurationServiceImpl configurationService;  

        public AppConfig(ConfigurationServiceImpl configurationService){
            this.configurationService = configurationService;
        }
2
what have you updated when it start working for you. Can you point out? - Zaheer Ahmed
@Zaheer when I changed AppConfig class. - Ashish Sharma
what version of spring are you using, does dependency injection via interface (proxies) work anywhere in your application, and do you have any other instances that implement ConfigurationService in your app? - chrismarx
Can you try with <constructor-arg index="0" ref="configurationService"/> ? - ndeverge
I tried to reproduce your problem but I can't, Impl class gets injecte to the constructor expecting an interface without any problem... - Tomasz Nurkiewicz

2 Answers

1
votes

Just Looking at it, the package name for AppConfig in the Spring configuration does not match the package declared in the Java source. You have "common.config" versus "configuration". It may be that the error text is misleading, that the reason the constructor is not found is that the class itself is not found.

1
votes

First of all , you have to know that Spring do not support interface injection, and thats why the code in your first case do not work,because you are passing ConfigurationService which is an interface as the constructor args.

In the second case , you are doing it right by passing the implementation class of ConfigurationService and taking it as the constructor argument.