Java異常之----Spring boot啓動失敗@org.springframework.beans.factory.annotation.Autowired(required=true)

本文目錄

一、背景描述

二、報錯信息

三、錯誤原因

四、解決方案

4.1 方案一:擴大註解 @EnableFeignClients掃描的包的範圍

4.2 方案二:在註解@EnableFeignClients指定clients


一、背景描述

微服務項目,spring boot (v2.1.5.RELEASE) ,今天在ServiceA微服務裏添加一個功能,通過FeignClient調用ServiceB的接口。

在本項目裏通過@Autowired自動注入方式注入客戶端接口

@Autowired
private ScreenSaverClient screenSaverClient;

然後啓動項目,結果就報 APPLICATION FAILED TO START 項目啓動失敗。

二、報錯信息

報錯內容如下圖所示:

三、錯誤原因

報這個錯,是因爲啓動類裏的註解 @EnableFeignClients沒有掃描到這個包,在本項目裏是沒有掃描到com.iot.basic.iotsmarthome.api.client.screensaver.ScreenSaverClient這個。

以下是本項目的錯誤的啓動類代碼:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
@ComponentScan(basePackages = {"com.uiotsoft.back","com.uiotsoft.framework"})
@EnableFeignClients(basePackages = {"com.uiotsoft.back.operation.facade.feign"})
@ServletComponentScan(basePackages ={"com.uiotsoft.back.operation.config"} )
@EnableSwagger2
@EnableHystrix
@EnableConfigurationProperties
public class OperationApplication {
	
	public static void main(String[] args) throws Exception {
		SpringApplication.run(OperationApplication.class, args);
	}
	
}

四、解決方案

方案有多種,這裏先寫兩種,以後再更新:

4.1 方案一:擴大註解 @EnableFeignClients掃描的包的範圍

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
@ComponentScan(basePackages = {"com.uiotsoft.back","com.uiotsoft.framework"})
@EnableFeignClients(basePackages = {"com.uiotsoft"})
@ServletComponentScan(basePackages ={"com.uiotsoft.back.operation.config"} )
@EnableSwagger2
@EnableHystrix
@EnableConfigurationProperties
public class OperationApplication {
	
	public static void main(String[] args) throws Exception {
		SpringApplication.run(OperationApplication.class, args);
	}
	
}

網上有說“將 FeignClient 這個 bean 放在和 Application 啓動類同級目錄”,其道理和作用是和方案一相同的。

4.2 方案二:在註解@EnableFeignClients指定clients

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
@ComponentScan(basePackages = {"com.uiotsoft.back","com.uiotsoft.framework"})
@EnableFeignClients(basePackages = {"com.uiotsoft.back.operation.facade.feign"}, clients = ScreenSaverClient.class)
@ServletComponentScan(basePackages ={"com.uiotsoft.back.operation.config"} )
@EnableSwagger2
@EnableHystrix
@EnableConfigurationProperties
public class OperationApplication {
	
	public static void main(String[] args) throws Exception {
		SpringApplication.run(OperationApplication.class, args);
	}
	
}

當然方案二有個弊端,就是項目裏存在多個clients(需要調用很多其他的服務時)的時候,就需要指定很多個,所以如果通過FeignClient調用多個服務的時候,用方案一比較合適。

 

 

完結!

 

以下不用看,只是方便被搜索到

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.uiotsoft.back.operation.facade.service.impl.ScreenSaverClientServiceImpl required a bean of type 'com.uiotsoft.basic.iotsmarthome.api.client.screensaver.ScreenSaverClient' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.uiotsoft.basic.iotsmarthome.api.client.screensaver.ScreenSaverClient' in your configuration.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章