可能是問題標題似乎是重複的,但我得到相同的錯誤,並不能找到答案在stackoverflow。無法autowire字段注入自動裝配依賴失敗
我有一個控制器
@Controller
public class MyController{
@Autowired
BeanA beanA;
@RequestMapping(value="/home")
public String showHomeScreen(){
return "home";
}
}
我BeanA類:
public class BeanA
{
private Map<Object, Object> maps;
//Setters,Getters
}
我在春天的配置以這種方式
<bean id="beanA" class="com.mycompany.beans.BeanA">
<property name="maps">
<map>
<entry key="Key 1" value="1" />
<entry key="Key 2" value="2" />
</map>
</property>
</bean>
更新配置BeanA:
堆棧跟蹤:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.mycompany.beans.BeanA com.mycompany.controller.MyController.beanA ; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.mycompany.beans.BeanA] 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.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
我也註冊了我的上下文基礎包現在
<context:component-scan base-package="com.mycompany.*"/>
當我部署我的應用程序獲得儘可能無法自動裝配場「beanA」自動裝配Autowired依賴注射失敗的錯誤。
有人可以幫我擺脫這個問題....
你的代碼不能編譯,你還沒有提供異常和spring配置文件的堆棧跟蹤。 – 2014-10-04 05:59:39
已更新stacktrace ...看起來它試圖將BeanA類映射到mycontroller beanA ...您可以在堆棧跟蹤中看到它說com.mycompany.beans.BeanA com.mycompany.controller.MyController.beanA – pathfinder 2014-10-07 07:00:07
I可以看到Bean com.mycompany.beans.BeanA未註冊.... from stacktrace:沒有符合條件的bean [com.mycompany.beans.BeanA] – pathfinder 2014-10-07 07:14:50