java 从 WAR 中的其他 Maven 模块导入 Spring bean?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3781755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:21:21  来源:igfitidea点击:

Importing Spring beans from other Maven modules inside a WAR?

javaspringservletsjarjavabeans

提问by HDave

I have a new web app that is packaged as a WAR as part of a multi-module Maven project. The applicationContext.xml for this WAR references beans that are imported from the "service" module, which in turn imports beans from the "dao" module. The import statement in applicationContext.xml looks like this:

我有一个新的 Web 应用程序,它被打包为 WAR,作为多模块 Maven 项目的一部分。此 WAR 的 applicationContext.xml 引用从“service”模块导入的 bean,后者又从“dao”模块导入 bean。applicationContext.xml 中的 import 语句如下所示:

<import resource="classpath*:service.xml" />

and the one inside the service.xml file looks like this:

service.xml 文件中的一个如下所示:

<import resource="classpath*:dao.xml" />

Neither Spring STS, nor Eclipse show any warnings or errors in my bean files. I reference the imported beans all over the place. The Maven build works fine and the DAO integration tests all pass (they use the beans). I don't have any service integration tests yet.

Spring STS 和 Eclipse 都没有在我的 bean 文件中显示任何警告或错误。我到处都引用了进口的豆子。Maven 构建工作正常,DAO 集成测试全部通过(它们使用 bean)。我还没有任何服务集成测试。

But when I start up the WAR in Jetty I get an error:

但是当我在 Jetty 中启动 WAR 时,我收到一个错误:

Error creating bean with name 'securityService' 
Cannot resolve reference to bean 'userDAO' while setting constructor argument

All of the imported bean XML files can be found inside their respective JAR files in the WEB-INF/lib directory. Indeed, the service bean that threw the error is itself defined inside the service.xml file inside the service module's JAR file.

所有导入的 bean XML 文件都可以在 WEB-INF/lib 目录中各自的 JAR 文件中找到。实际上,引发错误的服务 bean 本身是在服务模块的 JAR 文件中的 service.xml 文件中定义的。

Apparently the service module can't find the bean that it imported from the dao module. Obviously I don't understand something...seems like this should this Just Work?

显然,服务模块找不到它从 dao 模块导入的 bean。显然我不明白一些事情......似乎这应该只是工作吗?

采纳答案by HDave

I enabled DEBUG logging for 'org.springframework' in order to see if I could learn anything. What I found were messages to the effect that the DAO beans had been created, but there was also a message about them having no name or id.

我为“org.springframework”启用了调试日志记录,以查看我是否能学到任何东西。我发现的是 DAO bean 已创建的消息,但也有一条消息表明它们没有名称或 ID。

I check the file, and they all did have an id. So what was it? I check the XML namespace and saw:

我检查了文件,他们都有一个 id。那是什么?我检查了 XML 命名空间并看到:

http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"

and noticed it was old (I am using Spring 3.0.2) and changed it to:

并注意到它很旧(我使用的是 Spring 3.0.2)并将其更改为:

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

Once I changed it, Spring instantly threw half a dozen errors regarding beans that were defined incorrectly (but never used apparently). Once I fixed those errors, everything Just Worked. I've since gone through the entire system checking Spring XML file namespace versions.

一旦我改变了它,Spring 立即抛出了六个关于错误定义的 bean 的错误(但从未明显使用过)。一旦我修复了这些错误,一切就正常了。我已经检查了整个系统,检查 Spring XML 文件命名空间版本。

Thanks to all for the help. Can't believe I wasted a day on this stupidity!!

感谢大家的帮助。不敢相信我在这种愚蠢上浪费了一天!!

回答by Sean Patrick Floyd

The difference between the classpath:thingy.xmland classpath*:thingy.xmlnotation is that the former uses the standard classpath mechanism to resolve one resource (using ClassLoader.getResource(name)), whereas the latter will use ClassLoader.getResources(name)to retrieve all matching resources on the classpath, a distinction that should be irrelevant in your situation as I guess there is only one dao.xmlfile on the class path.

classpath:thingy.xmlclasspath*:thingy.xml符号的区别在于前者使用标准的类路径机制来解析一个资源(使用ClassLoader.getResource(name)),而后者将使用ClassLoader.getResources(name)来检索类路径上的所有匹配资源,一个这应该与您的情况无关,因为我猜dao.xml类路径上只有一个文件。

I think your problem is different, you are missing a leading slash.

我认为您的问题有所不同,您缺少前导斜线。

Use this for a single resource

将此用于单个资源

<import resource="classpath:/dao.xml" />

and this for multiple resources

这适用于多种资源

<import resource="classpath*:/dao.xml" />

See

回答by Jigar Joshi

It should be like

它应该像

<import resource="classpath:service.xml"/>

回答by Raghuram

Are you having multiple applicationContexts and possibly the parent context is referring to a bean defined in the child context?

您是否有多个 applicationContext 并且父上下文可能是指在子上下文中定义的 bean?