在拆分springboot項目的時候,有時候各模塊之間需要相互引用。如A需要使用了B中的dao層(即數據庫操作)故需要引入B的依賴,B又引用了A的依賴。這時候就會出現循環依賴的問題。
解決的方法就是建立一個common模塊,將共同需要使用的文件放在這個模塊中。
當然引入模塊之後,我們也需要修改啓動類中的註解。也就是掃描mapper的位置。
@MapperScan("com.*.*.common.dao")
public class **Application {
public static void main(String [] args){
SpringApplication.run(**Application.class);
}
}
當然,項目中也可能用到一些工具類。例如通過讀取配置文件的一些工具類。
@Component
@PropertySource("classpath:adUrls.properties")
@ConfigurationProperties(prefix = "adimage")
public class AdUrls {
private List<String> imgUrls = new ArrayList<>();
public List<String> getImgUrls() {
return imgUrls;
};
public void setImgUrls(List<String> imgUrls) {
this.imgUrls = imgUrls;
}
}
這樣的一些通多讀取.properties文件的工具類,我們通過引入common模塊之後,編譯之後自動注入(@Autowire)還是會出錯。解決的關鍵就是@Component,啓動類並沒有將這個組件掃描到。
@SpringBootApplication(scanBasePackages={"com.*.*.common.util","com.czc.*.*.*"})
解決方法就是在添加上面的註解。記得將本身項目中需要掃描的包也寫進去。若不寫進去,雖然運行成功了,但是隻掃描了工具類中的組件,也就是原來項目中的Controller等都沒掃描到。這樣就處理不到請求。
可參考:
https://www.cnblogs.com/MaxElephant/p/8108140.html
https://blog.csdn.net/weixin_41540822/article/details/88852973#2_Spring_Boot__ComponentScan_70