Spring Boot異常:BeanCreationException:Injection of autowired dependencies failed;
問題描述:
在使用spring boot框架實現事務控制demo時,啟動專案報錯如下:
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'queryTypeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field:
- at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537
- at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
- at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
- at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
- at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
- at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
- at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
- at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
- at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
- at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
- at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
- at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
- at org.springframework.boot.context.web.SpringBootServletInitializer.run(SpringBootServletInitializer.java:117)
- at org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:108)
- at org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:68)
- at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)
原因:
後來一步步排查發現,只要在實現事務管理的service層類上加入@transactional註解,啟動就會報上面的錯,不加則沒事。網上Google和stack overflow上找了好久,最後嘗試把事務註解加在類的介面上,結果就好了。
檢視官方案例,Spring Boot 使用事務非常簡單,首先使用註解 @EnableTransactionManagement 開啟事務支援後,然後在訪問資料庫的Service方法上添加註解
@Transactional ,在Service中,被 @Transactional 註解的方法,將支援事務;如果註解在類上,則整個類的所有方法都預設支援事務。
關於事務管理器,不管是JPA還是JDBC等都實現自介面 PlatformTransactionManager 如果你新增的是 spring-boot-starter-jdbc 依賴,框架會預設注入 DataSourceTransactionManager 例項。如果你新增的是 spring-boot-starter-data-jpa 依賴,框架會預設注入 JpaTransactionManager 例項。正是這些SpringBoot為我們自動做了,所以相對於我們並不透明,我現在依舊不是很明白為什麼必須加在介面上,是不是和Spring
Boot預設的註解注入有衝突,還是什麼原因?
解決方案:
目前可行的是將註解通過介面的方式注入,然後就是在有多個事務管理器時,使用value具體指定使用哪個事務管理器@Transactional(value="txManager1")。
擴充套件話題
在找這個異常時,看到了這樣一封關於Spring Boot介面使用的郵件回覆,內容如下圖:
相關推薦
Spring Boot異常:BeanCreationException:Injection of autowired dependencies failed;
問題描述: 在使用spring boot框架實現事務控制demo時,啟動專案報錯如下: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'qu
BeanCreationException:Injection of autowired dependencies failed解決思路(宜立方專案)
今天在用maven整合smm框架的時候遇到了bug: 按報錯的描述來說應該是註解建立失敗,其出錯點在controller那裡。但是回去看了一下controller層並沒有語法編寫錯誤, 那麼可能的原因就是在@service注入的時候出現了問題 service層:
異常:Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException
leg not 出現 resolve auto -s 啟動 文件名 書寫 這個異常是出現在註入配置文件中配置好的屬性時報錯的: Injection of autowired dependencies failed; nested exception is java.lan
Injection of autowired dependencies failed; autowire 自動註入失敗,測試類已初始化過了Spring容器。
ins cor als proc methods ase servle res resolved 1 嚴重: StandardWrapper.Throwable 2 org.springframework.beans.factory.BeanCreationExc
spring管理載入bean錯誤;Injection of autowired dependencies failed;
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itemController': Injection of autowired dependen
Injection of autowired dependencies failed; 的解決辦法!
1.檢查你專案中有沒有多餘的@Autowired 如果你引入了多餘(或者錯誤的)的@Autowired就會導致以上錯誤 一般這個錯誤都是發生在controll,dao,service中,看看你的註解是否有多餘或者錯誤 Dao—@Repo
Injection of autowired dependencies failed解決方式
異常如下: ERROR [RMI TCP Connection(3)-127.0.0.1] - Context initialization failed org.springframework.beans.factory.BeanCreationException: Error cre
品優購Error creating bean with name 'itemPageServiceImpl': Injection of autowired dependencies failed
異常資訊 嚴重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.spr
解決Injection of autowired dependencies failed的報錯
錯誤: 嚴重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springfr
Redis 報錯 Injection of autowired dependencies failed;
Injection of autowired dependencies failed; nested exception is org.springframework.data.redis.Redis
Error creating bean with name 'bookShopListImpl': Injection of autowired dependencies failed;
Could not autowire field Error creating bean with name 'bookShopListImpl': Injection of autowired dependencies failed; org.springframew
Error creating bean with name '***(類名)': Injection of autowired dependencies failed;
整springMVC時報錯如下圖: 參考了好多方法,可能是由於和別人jar包和別的環境差別的原因,總是500錯誤 資訊裡有句“Error creating bean with name ‘productController’: Injection of
解決:Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.
今天除錯專案程式碼,出現的引resource的Injection報錯,查原因查了好長時間才找到,現在這裡總結一下,以免以後忘掉以及給大家參考。 最終原因是迴圈呼叫,也就是我要注入的那個類也呼叫了我當前在寫的類; 報錯大致內容入下: Error starting Ap
spring注入失敗 Injection of resource dependencies failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'visitCopyRightSubjectService': Injection of resou
Error creating bean with name 'fileController': Injection of resource dependencies failed;
alt context http original pen isp listener uestc file 1.錯誤描述 Exception sending context initialized event to listener instance of class o
Injection of resource dependencies failed;錯誤幾種解決方法
Error creating bean with name 'connDataController': Injection of resource dependencies failed; 最近研究ssm專案的時候出現這個問題,去網上一搜那答案真是差不多,都沒有解決我的問題,後來在重複搭建專案後
解決Error creating bean with name XXX: Injection of resource dependencies failed
解決SSH整合時出現的錯誤,錯誤如上圖所示。 該錯誤是因為Mybatis中需要的註解,spring沒有檢測到此註解資訊。 錯誤原因可能有四處: (1):web.xml沒有配置spring的上下文環境和核心的監聽器 解決辦法: 在web.xml新增 <!--s
Error creating bean with name 'investExcelController': Injection of resource dependencies failed;
啟動springboot的過程中,報以下錯誤: Error creating bean with name ‘investExcelController’:Injection of resource dependencies failed; Error creating bean with
Error creating bean with name 'userController': Injection of resource dependencies failed;
Springmvc問題 今天,做開發時,剛配置好環境,測試一下環境是否正常,然後日誌報錯是: org.springframework.beans.factory.BeanCreationExcep
Spring Boot(三):AOP&日誌操作&異常處理
-1 aop str image pan handle ssa upload java代碼 一、AOP:HttpAspect.java 二、操作日誌 在HttpAspect.java中調用org.slf4j.Logger、org.slf4j.LoggerFactor