springboot專案報錯---在a.impl中使用b.Impl
阿新 • • 發佈:2018-12-17
錯誤資訊:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 11:14:43.841 ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter 42 report -
*************************** APPLICATION FAILED TO START ***************************
Description:
A component required a bean of type 'com.xxx.xxx.api.system.service.ThingService' that could not be found.
Action:
Consider defining a bean of type 'com.xxx.xxx.api.system.service.ThingService' in your configuration.
Process finished with exit code 0
原因:
1. 背景---一個a.Impl裡,引用另一個b.Impl中的方法,方法寫錯
在建類自動生成時,少寫@Resource 忘記繼承 implements ThingService @Override
a.Impl寫法:
用法:thingService.updateThing(url, thingRequest, "");//呼叫改方法
public class DeviceServiceImpl implements DeviceService { @Resource ThingService thingService; //宣告引用的service /** * 新增一條裝置資訊 * * @param request * @return */ @Override public String add(DeviceAddRequest request) { //若模板為空,只存資料庫,不存模型 if (StrKit.isEmpty(request.getTemplateid())) { return addToDatabase(request,""); }else{//若模板不為空,先存模型,後存資料庫 //先調模型,將裝置名稱,模板id存入模型 ThingAddRequest thingRequest = new ThingAddRequest(); thingRequest.setName(request.getDevicename()); thingRequest.setThingTemplateId(request.getTemplateid()); String resultsId = thingService.updateThing(url, thingRequest, "");//呼叫改方法 //存資料庫 return addToDatabase(request,resultsId); } }
b.Impl寫法:
@Service
public class ThingServiceImpl implements ThingService {
/**
* 新建thing
* @param targetUrl 請求地址
* @param requestObj 請求引數,新增thing陣列
* @return 操作結果,檢視results屬性,返回的String[]是新增節點的uri
* "results": ["a01e8e76-9d5e-435d-8c9e-8fd14f5e75ca"]
*/
@Override
public String updateThing(String targetUrl , ThingAddRequest requestObj, String token){
String requestStr = JSON.toJSONString(requestObj);
String url = targetUrl + "/thing" ;
try {
String responseStr = HttpHelper.post(url, requestStr, token);
JSONObject responseJson = (JSONObject) JSON.parse(responseStr);
if (responseJson.getString("statuscode").equals("-1")){
throw new CustomException(responseJson.getString("message"));
}
return responseJson.getString("results");
} catch (RuntimeException e){
throw new CustomException(e.getMessage());
}
}
}