-
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController': Unsatisfied dependency expressed through field 'tokenService';
-
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.xx.common.security.service.TokenService' available: expected at least 1 bean which qualifies as autowire candidate.
-
原因:
@SpringBootApplication
只會掃描本包子包下的bean對象,由於TokenService和RedisService與鑒權部分路徑不同,因為分別在common模塊的兩個子模塊中(包名肯定不一樣,雖然寫成一樣不會出現這樣的問題,但是一般又不會寫成一樣)。所以掃描不到。 -
解決方法:
-
在common子模塊(需要在其他地方使用的工具類所在模塊中)的resources下創建文件
META-INF/spring.factories
手動注入org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.*(最好具體到類,下面注解同)
-
或者在啟動類上添加
@SpringBootApplication(scanBasePackages={"com.example.*"})
其他博客有看到會出現一些問題,還是建議在多模塊中,那些工具類什么的,還是使用第一種解決方法。
注意:啟動類上加了@SpringBootApplication(basePackages = {})這個注解會導致controller掃描不到導致的,因為如果加了這個注解就不會掃描所在的包及子包的文件,需要將controller所在的包加入到注解@SpringBootApplication的大括號中
-