4

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;
        }
Ashish Sharma
  • 1,124
  • 2
  • 24
  • 49

2 Answers2

1

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.

billmill
  • 331
  • 1
  • 3
  • 14
1

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.

Tito
  • 8,894
  • 12
  • 52
  • 86
  • that's just flat out wrong - http://stackoverflow.com/questions/2387431/spring-autowiring-class-vs-interface – chrismarx May 08 '12 at 13:56
  • 1
    @chrismarx Spring does not support the injection of an interface through a constructor argument XML configuration. Your link is to a question about autowiring using an interface type reference and the autowired annotation. In that case it only works if a candidate (implementation) for autowiring to that interface can be found. If there is no implementation, then technically in that scenario interface injection is not supported. So no, tito is not "flat out wrong". –  Jun 27 '12 at 18:02