今天啟動項目時報錯:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'colorController': Unsatisfied dependency expressed through field 'jobService'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'jobService': Bean with name 'jobService' has been injected into other beans [ProcessService,ProcessManualService] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.
首先控制台是從一個controller報出的錯
跟蹤發現
於是去jobService中看看
然后到這兩個service中再看看,結果發現
構成了循環引用,去掉沒有使用的引用,問題解決!
當然,如果你的系統過於龐大,不好找哪里構成了循環引用,可以使用spring提供的bean懶加載解決:
@Autowired @Lazy private ClassA classA; @Autowired @Lazy private ClassB classB;
不過解決循環依賴,當然最好的方法是重構你的代碼,進行解耦。