項(xiàng)目場(chǎng)景:SpringBoot+Mybatis。
出現(xiàn)這種異常主要是無(wú)法創(chuàng)建bean到容器中,主要有以下幾種情況:
1.注解沒(méi)有添加:
controller:
@RestController
@AllArgsConstructor
@RequestMapping("/enterprise")
@Api(value = "企業(yè)數(shù)據(jù)", tags = "企業(yè)數(shù)據(jù)接口")
public class EnterpriseController {
private final IEnterpriseService service;
}
注:
- controller類(lèi)要加入@RestController注解,@AllArgsConstructor注解視情況而定。
- 引入了private final IEnterpriseService service,所以需要注入,可以在controller類(lèi)上加入@AllArgsConstrctor注解修飾。
- 或者用@Autowired修飾注入變量。
@Autowired
private final IEnterpriseService service;
service:
@Service
@AllArgsConstructor
public class EnterpriseServiceImpl extends BaseServiceImpl<EnterpriseMapper, Enterprise> implements IEnterpriseService {
public List<Map> countByType() {
return baseMapper.countByType();
}
}
注:
- service層要加入@Service注解。
- 如果service層中引入了Mapper變量,則需要加入@AllArgsConstrctor注解;當(dāng)然也可以用@Autowired注解修飾變量。不過(guò)推薦使用@AllArgsConstrctor注解,因?yàn)橹恍枰陬?lèi)上引入一次;使用@Autowired注解需要修飾每一個(gè)引入的Mapper,極其繁瑣。
@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.接口有沒(méi)有對(duì)應(yīng)的實(shí)現(xiàn)類(lèi),以及實(shí)現(xiàn)類(lèi)實(shí)現(xiàn)的接口是否正確。
3.項(xiàng)目中的文件是否重名,全局定位異常文件,并修改為不同文件名。
4.啟動(dòng)類(lèi)中注解是否掃描到所有mapper。
@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);
}
}
注:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-833800.html
- @ComponentScan注解和@MapperScan注解一定要掃描到所有文件路徑,多個(gè)路徑之間用逗號(hào)分隔開(kāi)。
- 或者在yml配置文件中配置mapper掃描路徑。
5.Mapper.xml文件中配置的resultMap對(duì)應(yīng)的實(shí)體類(lèi)路徑是否正確,以及各種Mybatis標(biāo)簽對(duì)應(yīng)的resultMap或者resultType路徑是否正確。
6.接口對(duì)應(yīng)的地址是否重復(fù)。如下圖,兩個(gè)接口均使用了相同的地址,會(huì)出現(xiàn)異常。
@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);
}
異常排查技巧:
通常出現(xiàn)這種異常會(huì)在控制臺(tái)打印出一堆異常信息,在控制臺(tái)上方的異常信息大多是和程序真正的問(wèn)題沒(méi)有直接關(guān)系的,這里給出以下異常排查技巧:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-833800.html
- 控制臺(tái)看報(bào)錯(cuò):從下往上看。
- 利用好idea的全局搜索定位功能去排查異常。
到了這里,關(guān)于異常:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!