2014-10-04 141 views
0

可能是问题标题似乎是重复的,但我得到相同的错误,并不能找到答案在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依赖注射失败的错误。

有人可以帮我摆脱这个问题....

+0

你的代码不能编译,你还没有提供异常和spring配置文件的堆栈跟踪。 – 2014-10-04 05:59:39

+0

已更新stacktrace ...看起来它试图将BeanA类映射到mycontroller beanA ...您可以在堆栈跟踪中看到它说com.mycompany.beans.BeanA com.mycompany.controller.MyController.beanA – pathfinder 2014-10-07 07:00:07

+0

I可以看到Bean com.mycompany.beans.BeanA未注册.... from stacktrace:没有符合条件的bean [com.mycompany.beans.BeanA] – pathfinder 2014-10-07 07:14:50

回答

0

你是对的@Serge贝勒斯特。豆A不被春天识别。 关于自动装配了一个错字我在原岗位更新,自动装配Autowired

上BeanA所以我加入@Component也改变上下文组件扫描这样

<context:component-scan base-package="com.mycompany.*"/> 

<context:component-scan base-package="com.mycompany"/> 

但仍donnoŸ即使我提到在春季配置xml我的豆没有注册。

Anywayz现在我没有得到任何错误,问题解决了。

1

堆栈跟踪说:

  • 控制器MyController正确的春天扫描,但也有依赖关系的错误
  • BeanA不存在是相同的应用背景

可能的原因:

  • 其中beanA声明不是由春(最有可能)
  • 有一个错字某处处理配置文件(它看起来像没有,但你写Autowire没有结束d ...)
  • 其中beanA声明的情况下,既不相同语境MyController一个,也不是父上下文
相关问题