![](/img/trans.png)
[英]UnsatisfiedDependencyException: Error creating bean with name
[英]UnsatisfiedDependencyException: Error creating bean with name 'registrationController'
我對 spring-boot 完全陌生,我正在嘗試創建一個注冊后端,但存在一個讓我感到困惑的錯誤。 我瀏覽了很多類似的問題,但沒有一個答案對我有幫助。
WARN 18732 --- [main] ConfigServletWebServerApplicationContext:在上下文初始化期間遇到異常 - 取消刷新嘗試:org.springframework.beans.factory.UnsatisfiedDependencyException:創建名稱為“registrationController”的bean時出錯:通過字段“registrationService”表示的依賴關系不滿足; 嵌套異常是 org.springframework.beans.factory.UnsatisfiedDependencyException:創建名稱為“registrationService”的 bean 時出錯:通過字段“accApplMapper”表示的依賴關系不滿足; 嵌套異常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有可用的“com.example.demo.mapper.AccApplMapper”類型的合格 bean:預計至少有 1 個有資格作為自動裝配候選者的 bean。 依賴注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
說明:com.example.demo.service.RegistrationService 中的字段 accApplMapper 需要一個類型為 > 'com.example.demo.mapper.AccApplMapper' 的 bean,但無法找到。 注入點有以下注解: - @org.springframework.beans.factory.annotation.Autowired(required=true) 行動:考慮在你的配置中定義一個 'com.example.demo.mapper.AccApplMapper' 類型的 bean。
注冊控制器.java
package com.example.demo.controller;
import com.example.demo.result.Result;
import com.example.demo.pojo.NewUser;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.demo.service.RegistrationService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
public class RegistrationController {
@Autowired
RegistrationService registrationService;
@CrossOrigin
@PostMapping(value = "api/registration")
@ResponseBody
public Result registration(@RequestBody NewUser user) {
System.out.println(user.toString());
if(user!=null && user.getCompanyName()!=null) {
int insert = registrationService.addAcctAppl(user.getCompanyName());
return insert>=0 ? new Result(200) : new Result(500);
}
else {
return new Result(400);
}
}
}
注冊服務.java
package com.example.demo.service;
import com.example.demo.mapper.AccApplMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class RegistrationService {
@Autowired
private AccApplMapper accApplMapper;
public int addAcctAppl(String CompanyName) {
return accApplMapper.addAcctAppl(CompanyName);
}
}
AccApplMapper.java
package com.example.demo.mapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface AccApplMapper {
@Insert("INSERT INTO ACCT_APPL(ENG_COMP_NAME) VALUES(#{CompanyName}")
public int addAcctAppl(@Param("CompanyName") String CompanyName);
}
@Mapper
is not a spring bean annotation, you need to use @MapperScan(basepackages="your mapper package location")
annotation on a class with a annotation @Configuration
(usually you could use @MapperScan
on the Springboot main class) to enable spring將@Mapper
類注冊為bean。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.