目录
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