@AutoWired注解使用时可能会发生的错误

  @Autowired注解:用于对Bean的属性变量、属性的set方法及构造函数进行标注,配合对应的注解处理器完成Bean的自动配置工作。@Autowired注解默认按照Bean类型进行装配。


 1、在applicationContext.xml文件中的<beans>标签里填写xsi:schemaLocation的时候,对引进的包的版本与填写的location的版本不对应。

  像是,本来引进的包是"spring-context-3.2.5.RELEASE.jar",版本是3.2.5,但是在xsi:schemaLocation中填入的地址的是"http://www.springframework.org/schema/context/spring-context-4.2.xsd",那就有可能会发生错误。

2、在使用@Autowired注解的时候,没有把实体类之前setter方法删除掉。

3、在使用@Autowired注解之后没有在applicationContext.xml进行<context:component-scan>元素的配置。

  因为<context:component-scan>元素会自动注册AuthowiredAnnotationBeanPostProcessor实例(使用Autowired注解必须要注册AutowiredAnnotationBeanPostProcessor实例,用于解析@Autowired注解)。

4、在没有使用<context:component-scan>元素配置的情况下,还没有用<bean>标签对AuthowiredAnnotationBeanPostProcessor进行注册。即:

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

5、在没使用@Autowired注解配置之前,在applicationContext.xml就写好了注入实例的<prototype>元素,但是在使用@Authowired注解之后需要把<prototype>元素删除。

  因为该配置好的元素@Autowired注解已经在底层做好了。

猜你喜欢

转载自www.cnblogs.com/NYfor2018/p/8973097.html
今日推荐