[Bug] Spring相关bug收集

本文汇总了Spring Boot开发中遇到的一些常见错误及其解决方案,包括:TestRestTemplate注入异常、启动时@ComponentScan错误、工厂bean引用自身问题以及属性绑定失败到HikariDataSource等。通过分析错误信息和调整代码,成功解决了这些问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1. RequiredArgsConstructor空指针错误

2.TestRestTemplate注入时异常NoSuchBeanDefinitionException

3. 错误信息:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.

4.factory-bean reference points back to the same bean definition


1. RequiredArgsConstructor空指针错误

定义了一个类,用以下注解修饰

@Service("ABC")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))

拿掉@RequiredArgsConstructor(onConstructor = @__(@Autowired))这个注解之后报空指针错

ABC这个bean没有注入进去

@NoArgsConstructor, @RequiredArgsConstructor, @AllArgsContructor。是Lombok插件三种生成不同构造方法的注解,来完成项目中不同构造方法的需求。

@RequiredArgsConstructor的作用是: 会生成一个包含常量,和标识了NotNull的变量的构造方法。生成的构造方法是私有的private

2.TestRestTemplate注入时异常NoSuchBeanDefinitionException

错误信息如下:

Unsatisfied dependency expressed through field 'testRestTemplate'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.test.web.client.TestRestTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

解决方式:在spring test的启动类里面将启动类的注解的SpringBootTest注解改为如下即可:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 

3. 错误信息:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.

在自己新建一个maven项目之后,引入了spring的相关依赖,随即在java包当中写了一个spring boot的启动类,但是在启动时报了如下错误:

** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.


2019-03-13 22:23:48.872  WARN 7753 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [Application]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurer.class] cannot be opened because it does not exist
2019-03-13 22:23:48.885  INFO 7753 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application wi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值