简体   繁体   English

在Spring JavaConfig中自动装配Spring XML bean

[英]Autowire Spring XML bean inside Spring JavaConfig

I have a xml bean definition and JavaConfig bean definition in the same project. 我在同一项目中有一个xml bean定义和JavaConfig bean定义。 The JavaConfig need to reference the XML bean. JavaConfig需要引用XML bean。 But When i autowire the xml bean definition inside JavaConfig I get NoSuchBean definition error. 但是,当我在JavaConfig中自动装配xml bean定义时,出现NoSuchBean定义错误。 My question is: Isnt the xml beans and JavaConfig beans visible to each other? 我的问题是:xml bean和JavaConfig bean是否彼此可见? If not how do i inject the xml bean into JavaConfig? 如果没有,我如何将xml bean注入JavaConfig? Thanks 谢谢

Edit: 编辑:

Changing @Autowire to @Resource fixed it, but i dont understand why. 将@Autowire更改为@Resource可以修复它,但是我不明白为什么。 Autowire should go after type and the XML bean is of the type Autowire is annoted with. Autowire应该紧随类型,并且XML bean的类型应与Autowire一起注释。

If your bean is not being picked up by Autowire, you need to ensure the configuration file for the beans is being read and in turn is generating the beans. 如果未通过Autowire拾取bean,则需要确保已读取bean的配置文件,并且正在生成bean。

One easy way to check is if you are using an IDE with Bean detection capability to see if the IDE is picking up the bean. 一种简单的检查方法是,是否正在使用具有Bean检测功能的IDE,以查看IDE是否在拾取该bean。

If this is working then please paste in your code of the xml with bean in it and xml containg your scan ability. 如果此方法有效,则请在其中包含bean的xml代码中粘贴,并且xml包含您的扫描能力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM