最近在搭建springboot繼承dubbo的分布式項目時,測試dao層遇到的問題,啟動測試方法時,出現如下錯誤:
java.lang.IllegalStateException: Found multiple @SpringBootConfiguration annotated classes [Generic bean: class [com.example.cqw.DubboDaoApplication]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\study\dubbo-parent\dubbo-dao\target\classes\com\example\cqw\DubboDaoApplication.class], at org.springframework.util.Assert.state(Assert.java:94) at org.springframework.boot.test.context.SpringBootConfigurationFinder.scanPackage(SpringBootConfigurationFinder.java:69)
如圖:
分析原因:因為項目是springboot+dubbo架構,dao項目依賴於pojo項目,在pojo項目中也有啟動類@SpringBootApplication,因為在dao的測試類中啟動方法時,會加載pojo項目啟動類,所以會造成上圖所示錯誤,注釋掉pojo項目啟動類上的@SpringBootApplication注解即可
簡單的說,就是你在項目中有兩個@SpringBootApplication,注釋掉一個你不用的就可以。