我有以下java类:
package configuration;
import common.config.ConfigurationService;
public class AppConfig {
private ConfigurationService configurationService;
public AppConfig(ConfigurationService configurationService){
this.configurationService = configurationService;
}
还
public class ConfigurationServiceImpl
implements ConfigurationService, Runnable
{...
应用上下文文件如下:
<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>
在初始化期间,我收到以下错误并且我的 bean 未初始化:
原因:org.springframework.beans.factory.BeanCreationException:创建在类路径资源 [conf/applicationContext.xml] 中定义的名称为“appConfig”的 bean 时出错:指定了 1 个构造函数参数,但在 bean 'appConfig' 中找不到匹配的构造函数(提示:为简单参数指定索引和/或类型参数以避免类型歧义)
如果我将java类代码修改如下,则Spring注入可以工作:
package configuration;
import common.config.ConfigurationServiceImpl;
public class AppConfig {
private ConfigurationServiceImpl configurationService;
public AppConfig(ConfigurationServiceImpl configurationService){
this.configurationService = configurationService;
}