@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注解已經在底層做好了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM