[英]How to autowire a bean which expects a method reference as one of its dependencies
[英]Cannot find bean to autowire which is in maven dependencies in jar,
我使用Maven將jar與bean一起添加。 比我想將它們自動關聯到上下文文件中...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="statisticsService" class="com.ac.kpi.AcStatisticsService">
<constructor-arg name="dataService" ref="acDataService"/>
</bean>
</beans>
問題:運行tomcat時出現錯誤:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ac.kpi.StatisticsService] 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)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1326)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1072)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:967)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:543)
盡管事實上所有maven依賴項都已導入並存在。 此外,在Idea中,在LinuxOS的所有工作中,問題僅在於Eclipse,Windows OS。
如果Spring找不到要自動裝配的bean,則拋出這種異常。 這可能是由於您正在自動裝配接口而沒有該接口的實際實現。 檢查您的StatisticsService是否屬於這種情況。
有時候我也有同樣的問題。 刪除.m2中的本地com.ac. *程序包文件,然后再次下載。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.