java 如何调试 Spring NoSuchBeanDefinitionException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/375656/
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-29 12:08:23  来源:igfitidea点击:

How to Debug Spring NoSuchBeanDefinitionException

javaspringtesting

提问by Paul Croarkin

We have a (non-web app) Spring application that throws a NoSuchBeanDefinitionException when running tests on our CruiseControl continuous integration linux box. The test runs fine on Windows in Eclipse.

我们有一个(非 Web 应用程序)Spring 应用程序,它在 CruiseControl 持续集成 linux 机器上运行测试时抛出 NoSuchBeanDefinitionException。该测试在 Eclipse 中的 Windows 上运行良好。

The exception is thrown on the getBean() method:

在 getBean() 方法上抛出异常:

ApplicationContext context = new ClassPathXmlApplicationContext(CONTEXT_FILE);

MyBean bean = (MyBean)context.getBean("myBean");

The context file is rather large and complicated. The context file is in the classpath and Spring is finding it. I'd prefer it if Spring would throw an exception when trying to load the context file and build the dependencies so that we could have an idea as to where to start. Is there a way to force Spring to throw an exception at the time of creating the context?

上下文文件相当大且复杂。上下文文件位于类路径中,Spring 正在查找它。如果 Spring 在尝试加载上下文文件和构建依赖项时抛出异常,我更喜欢它,这样我们就可以知道从哪里开始。有没有办法强制 Spring 在创建上下文时抛出异常?

回答by Paul Croarkin

Solved. The instantiation of "myBean" in CONTEXT_FILE occurred after another bean that depended on it. This really should not be an issue, but I suspect that the parser on the Linux box must be stricter. Anyway, changing the order of definitions made it work on both Windows and Linux.

解决了。CONTEXT_FILE 中“myBean”的实例化发生在另一个依赖它的 bean 之后。这真的不应该是一个问题,但我怀疑 Linux 机器上的解析器必须更严格。不管怎样,改变定义的顺序使它在 Windows 和 Linux 上都可以工作。

回答by matt b

Are you certain that there is a bean named "myBean" in the CONTEXT_FILE? Are you using more than one context file? Have you turned up the Spring's logging to verify that the CONTEXT_FILE is being loaded correctly, and furthermore, than the file Spring is loading from the classpath is the exact file you think it should be loading?

您确定在 CONTEXT_FILE 中有一个名为“myBean”的 bean?您是否使用了多个上下文文件?您是否打开了 Spring 的日志记录以验证 CONTEXT_FILE 是否被正确加载,此外,Spring 从类路径加载的文件是您认为应该加载的确切文件?

I think you might be looking for the wrong solution to your problem - I've never seen Spring throw a NoSuchBeanDefinitionException except for the case where the bean you are asking for is not defined in the context, simple as that.

我认为您可能正在为您的问题寻找错误的解决方案 - 我从未见过 Spring 抛出 NoSuchBeanDefinitionException 除非您要求的 bean 未在上下文中定义,就这么简单。

回答by Joel

I had the same problem. It turned out that it was a subtle classpath issue. Specifically - my classpath on linux was set up so that my config directory containing my build.xml file came after my x-jars (external jars) directory - and some other beans.xml was being picked up from my x-jars which, as the log message correctly said, did not contain the required bean. It would have helped if the full path to the beans.xml file had been loggged with the Spring error message.

我有同样的问题。结果证明这是一个微妙的类路径问题。具体来说 - 我在 linux 上的类路径是这样设置的,这样我的包含 build.xml 文件的配置目录就在我的 x-jars(外部 jars)目录之后 - 并且从我的 x-jars 中提取了一些其他 beans.xml,作为日志消息正确地说,不包含所需的 bean。如果 bean.xml 文件的完整路径已与 Spring 错误消息一起记录,那将会有所帮助。