Spring自动装配组件列表

时间:2017-06-28 19:25:24

标签: java spring autowired

我有一个界面:

public interface IExternalCommandExecutor {
    String[] getCommandNames();
    void process(String command) throws FailedOperationException;
}

两个实现(第二个类似):

@Service
public class ExecutorImpl implements IExternalCommandExecutor {
    //some code...
}

服务,我在IExternalCommandExecutor列表中使用@Autowired注释:

@Service
public class ExternalCommandsService {

    @Autowired
    List<IExternalCommandExecutor> commandExecutors;

    // other code
}

当我试图运行它时,我收到了这个错误:

NoSuchBeanDefinitionException: No qualifying bean of type 
'java.util.List<com.mpcomp.entity.system.IExternalCommandExecutor>' available: 
expected at least 1 bean which qualifies as autowire candidate

Spring的奇怪行为。正如在this articlejavadoc中提到的那样,Spring应该尝试自动装配IExternalCommandExecutor的所有元素,而不是尝试查找类型java.util.List的bean。

如果我将List<IExternalCommandExecutor>更改为IExternalCommandExecutor[],则错误非常相似:

NoSuchBeanDefinitionException: No qualifying bean of type 
'com.mpcomp.entity.system.IExternalCommandExecutor[]' available: 
expected at least 1 bean which qualifies as autowire candidate

我正在使用Spring版本4.3.8 RELEASE。任何帮助将不胜感激!

修改 接口和服务在核心模块中,接口的实现在“子”模块中,这取决于核心模块。当我将服务转移到这个'儿童'模块时,一切正常!

0 个答案:

没有答案