[英]UnsatisfiedDependencyException: Error creating bean with name 'registrationController'
I am completely new to spring-boot and I am trying to create a registration backend, but there exist an error that confused me.我对 spring-boot 完全陌生,我正在尝试创建一个注册后端,但存在一个让我感到困惑的错误。 I looked through a lot of similar questions, but no one answer to them can't help me.
我浏览了很多类似的问题,但没有一个答案对我有帮助。
WARN 18732 --- [ main] ConfigServletWebServerApplicationContext: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'registrationController': Unsatisfied dependency expressed through field 'registrationService';
WARN 18732 --- [main] ConfigServletWebServerApplicationContext:在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“registrationController”的bean时出错:通过字段“registrationService”表示的依赖关系不满足; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'registrationService': Unsatisfied dependency expressed through field 'accApplMapper';
嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“registrationService”的 bean 时出错:通过字段“accApplMapper”表示的依赖关系不满足; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.mapper.AccApplMapper' available: expected at least 1 bean which qualifies as autowire candidate.
嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“com.example.demo.mapper.AccApplMapper”类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean。 Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
Description: Field accApplMapper in com.example.demo.service.RegistrationService required a bean of type > 'com.example.demo.mapper.AccApplMapper' that could not be found.
说明:com.example.demo.service.RegistrationService 中的字段 accApplMapper 需要一个类型为 > 'com.example.demo.mapper.AccApplMapper' 的 bean,但无法找到。 The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.example.demo.mapper.AccApplMapper' in your configuration.
注入点有以下注解: - @org.springframework.beans.factory.annotation.Autowired(required=true) 行动:考虑在你的配置中定义一个 'com.example.demo.mapper.AccApplMapper' 类型的 bean。
RegistrationController.java注册控制器.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);
}
}
}
RegistrationService.java注册服务.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 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 to register @Mapper
classes as bean. @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.