Reputation: 1144
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;
}
Upvotes: 4
Views: 11198
Reputation: 9044
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.
Upvotes: 1
Reputation: 351
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.
Upvotes: 1