Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 从另一个Maven模块创建Bean_Spring_Maven - Fatal编程技术网

Spring 从另一个Maven模块创建Bean

Spring 从另一个Maven模块创建Bean,spring,maven,Spring,Maven,在Maven项目中创建bean相当简单。将@Autowired注释添加到要创建的bean中,然后在spring-config.xml中声明其配置: @Autowired private ExampleBean exampleBean; . . . <bean id="exampleBean" class="path.to.your.bean"> <property name="hibernateTemplate" ref="hibernateTemplate" /&g

在Maven项目中创建bean相当简单。将
@Autowired
注释添加到要创建的bean中,然后在spring-config.xml中声明其配置:

@Autowired
private ExampleBean exampleBean;
.
.
.

<bean id="exampleBean" class="path.to.your.bean">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
我还在
AccountService
上保留了
@Autowired
注释(这是我想在本模块中使用的bean)

在声明
AccountService
的另一个模块中,我添加了bean声明:

<bean id="accountService" class="fr.bnp.dsp2aisp.service.AccountService">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
但我确信该类确实存在于指定的路径中


是否有其他方法可以使用在另一个Maven模块中声明的bean

您可以通过导入其他maven project bean上下文表单类路径来实现这一点:

<import resource="classpath:path/to/another/maven/module/context.xml" />


然后,您可以在实际的maven项目中使用在其他maven项目上下文中定义的所有bean,我终于让它工作了。当您有一个多模块Maven项目,并且您试图将其中一个模块导入另一个模块以使用其类时,您不应该从构建路径(属性->Java构建路径->项目->添加)添加模块,因为编译器在运行时将无法找到这些类

您需要做的是简单地将这些模块作为Maven依赖项添加到您希望在其中使用它们的模块的POM.xml文件中

例如:


com.test
测试红外线
0.0.1-快照

这个解决方案非常简单,但发现它有点累。我希望它能帮助其他人解决同样的问题

谢谢你的回答。我试试看,然后告诉你进展如何。我没工作。也许我遗漏了什么?我将更新我的OP以准确显示我所做的操作。不要使用“*”,要导入的上下文路径必须准确。要使用的文件分隔符是“/”而不是“\”。第一个目录不得以“/”作为前缀。如果您使用的是eclispe,请检查Java构建路径的配置:项目->属性->Java构建路径。如果不起作用,请发布不同maven模块项目的pom.xml文件。
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [fr.bnp.dsp2aisp.service.AccountService] for bean with name 'accountService' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]; nested exception is java.lang.ClassNotFoundException: fr.bnp.dsp2aisp.service.AccountService
<import resource="classpath:path/to/another/maven/module/context.xml" />
    <dependency>
        <groupId>com.test</groupId>
        <artifactId>test-infra</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>