在多模块Maven项目中注入自动assembly字段失败 – NoSuchBeanDefinitionException
我在Stackoverflow和其他网站上阅读了很多关于这个问题的post,但没有找到解决方案。
我有我的Maven模块的以下结构,其中一个主要父pom声明了所有这些模块(我在这里简化了结构以便仅显示相关部分):
Maven模块http://sofzh.miximages.com/java/fc0f91.png
基础和“A”模块依赖于base-api模块。 基本模块包含base-api模块中包含的接口的实现。
我在“base-api”模块中有一个接口IFoo。 接口IFoo由“基础”模块中的类Foo实现。 类Foo用Spring的“@Service”注释注释。
我希望Foo服务在我的测试类中自动assembly,该测试类包含在模块“A”中:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class FooTest { @Autowired private IFoo foo;
我还为我的测试创建了一个上下文配置文件,其中包含以下行
IFoo和Foo都包含在xyz的子包中(在不同的maven模块中,如上所述)。
当我在Eclipse中运行测试(使用m2eclipse插件)时,它会正确传递。 但是,当我运行maven构建(mvn clean install)时,会发生以下错误:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [xyzvIFoo] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我究竟做错了什么?
如果module A
不依赖于base module
,则module A
上的任何执行都将找不到base module
任何组件。 因此, module A
依赖于base module
的实现的任何组件都将失败(因为从module A
看不到实现)。
如果您只是希望module A
可以访问base module
中的module A
来运行测试,则可以将base module
的依赖项添加到module A
并将scope
设置为test
。 这样,您对module A
的测试就会运行得很好。 如果需要,您可以灵活地在运行时使用不同的IFoo
实现引入完全不同的JAR。