异常:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name

Project scenario: SpringBoot+Mybatis.

This exception occurs mainly because the bean cannot be created in the container. There are mainly the following situations:

1. Annotations are not added:

controller:

@RestController
@AllArgsConstructor
@RequestMapping("/enterprise")
@Api(value = "企业数据", tags = "企业数据接口")
public class EnterpriseController {
	private final IEnterpriseService service;
}

Note:

  • The controller class must be annotated with @RestController, and the @AllArgsConstructor annotation depends on the situation.
  • The private final IEnterpriseService service is introduced, so it needs to be injected. You can add the @AllArgsConstrctor annotation to the controller class.
  • Or use @Autowired to modify the injected variable.
    @Autowired
	private final IEnterpriseService service;

service:

@Service
@AllArgsConstructor
public class EnterpriseServiceImpl extends BaseServiceImpl<EnterpriseMapper, Enterprise> implements IEnterpriseService {
	
	public List<Map> countByType() {
		return baseMapper.countByType();
	}
}

Note:

  • The service layer needs to add the @Service annotation.
  • If Mapper variables are introduced in the service layer, you need to add the @AllArgsConstrctor annotation; of course, you can also use the @Autowired annotation to modify the variables. However, it is recommended to use the @AllArgsConstrctor annotation, because it only needs to be introduced once on the class; using the @Autowired annotation requires modifying each introduced Mapper, which is extremely cumbersome.
@Service
public class EnterpriseServiceImpl extends BaseServiceImpl<EnterpriseMapper, Enterprise> implements IEnterpriseService {

    @Autowired
	private final EnterpriseMapper mapper;

    @Autowired
	private final BlogMapper mapper;

	public List<Map> countByType() {
		return baseMapper.countByType();
	}
}

2. Does the interface have a corresponding implementation class, and whether the interface implemented by the implementation class is correct.

3. Whether the files in the project have the same name, locate the abnormal files globally, and modify them to different file names.

4. Check whether all mappers are scanned by the annotations in the startup class.

@EnableScheduling
@SpringBootApplication
@ComponentScan({"org.springblade","com.hello","com.test"})
@MapperScan({"com.hello.**.mapper.**","com.test.**.mapper.**"})
public class Application {

	public static void main(String[] args) {
		BladeApplication.run(CommonConstant.APPLICATION_NAME, Application.class, args);

	}

}

Note:

  • The @ComponentScan annotation and @MapperScan annotation must scan all file paths, and multiple paths are separated by commas.
  • Or configure the mapper scan path in the yml configuration file.

5. Whether the entity class path corresponding to the resultMap configured in the Mapper.xml file is correct, and whether the resultMap or resultType path corresponding to various Mybatis tags is correct.

6. Whether the address corresponding to the interface is repeated. As shown in the figure below, both interfaces use the same address, and an exception will occur.


	@GetMapping("/count")
	public R<EnterpriseVO> test(@RequestBody  Enterprise enterprise){
		EnterpriseVO detail = service.test(enterprise);
		return R.data(detail);
	}
	
	@PostMapping("/count")
	public R<Enterprise> update(@RequestBody Enterprise enterprise){
		boolean flag = service.updateByBody(enterprise);
		Enterprise entity = service.selectInfo(enterprise);
		return R.data(entity);
	}

Exception troubleshooting tips:

Usually when this kind of exception occurs, a bunch of exception information will be printed on the console. Most of the exception information above the console is not directly related to the real problem of the program. Here are the following exception troubleshooting tips:

  1. View the error report on the console: View from bottom to top.
  2. Make good use of idea's global search and positioning function to troubleshoot abnormalities.

Guess you like

Origin blog.csdn.net/qq_44973310/article/details/128302144