spring的bean不能注入的几种原因及分析
生活随笔
收集整理的這篇文章主要介紹了
spring的bean不能注入的几种原因及分析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、異常信息
2.有可能引起的原因:
1、在applicationContext.xml的配置文件里的包掃描不對。
2、在web.xml里沒有加載spring容器。
3、分布式工程,使用dubbo或者hsf通信,在服務層,或者消費層,單詞寫錯了。
4、還有一種可能,有可能是pom 里的jar包沖突。
5、從ApplicationContext 獲取bean的時候getBean傳的參數與配置的bean的id不一致導致無法取到,建議bean的id與接口名保持一致,不要添加或去除字母:
public static RedisUtilService getRedisUtilService() {// 此處應為redisUtilreturn (RedisUtilService)getApplicationContext().getBean("redisUtilService");} <hsf:consumer id="redisUtil" interface="com.asiainfo.retail.service.util.RedisUtilService"version="${version}" group="${moon}"></hsf:consumer>6、web.xml的加載順序與它們在 web.xml 文件中的先后順序無關。不會因為 filter 寫在 listener 的前面而會先加載 filter。加載順序依次為:listener -> filter -> servlet,所以在filter中是無法使用@autowire注解注入bean的,需要我們手動加載,在過濾器中使用service是需要手動注入的。代碼如下:
private static ApplicationContext getApplicationContext() {ApplicationContext ac = null;try {ac = new ClassPathXmlApplicationContext("applicationContext.xml");} catch (Exception e) {e.printStackTrace();}return ac; }public static StaffInfoService getStaffInfoService() {return (StaffInfoService)getApplicationContext().getBean("staffInfoService"); }總結
以上是生活随笔為你收集整理的spring的bean不能注入的几种原因及分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 旅游系统_旅游景区安全标识系统设计原则
- 下一篇: JAVA WEB篇3——JSP