ERROR 12020 --- [ main] o.s.boot.SpringApplication : Application run failed

2024-05-12 20:49:46.373 ERROR 12020 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.13.jar:5.3.13]
	at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.13.jar:5.3.13]
	at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.13.jar:5.3.13]
	at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_412]
	at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.13.jar:5.3.13]
	at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.13.jar:5.3.13]
	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.13.jar:5.3.13]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.13.jar:5.3.13]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.0.jar:2.6.0]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) [spring-boot-2.6.0.jar:2.6.0]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) [spring-boot-2.6.0.jar:2.6.0]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) [spring-boot-2.6.0.jar:2.6.0]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) [spring-boot-2.6.0.jar:2.6.0]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1290) [spring-boot-2.6.0.jar:2.6.0]
	at com.atguigu.rabbitmq.SpringApplication.main(SpringApplication.java:10) [classes/:na]

解决方案:

添加@EnableWebMvc注解

### Spring Boot 应用启动失败的原因分析 Spring Boot 报错 `No qualifying bean of type 'com.example.service.UserService' available` 表明在应用程序上下文中未找到符合条件的 `UserService` 类型 Bean。此问题通常由以下几个原因引起: #### 原因一:组件扫描范围不足 如果 Spring Boot 的启动类所在包未能覆盖到 `UserService` 所属的包,则不会将其注册为 Spring 容器中的 Bean[^1]。 解决方案是在确保 Spring Boot 启动类的位置能够扫描到所有必要的组件。例如,假设项目结构如下: ``` com.example ├── application │ └── Application.java └── service └── UserService.java ``` 此时,`Application.java` 需要位于根目录下(即 `com.example.application.Application`),以便其默认的组件扫描机制可以识别并加载 `UserService`。 #### 原因二:Mapper 接口未被正确扫描 当使用 MyBatis 或其他持久化框架时,可能需要显式声明哪些 Mapper 接口应被转换为实现类。如果没有配置对应的注解或 XML 文件,可能导致依赖注入失败[^5]。 解决方法之一就是在主程序入口处添加 `@MapperScan` 注解来指定 Mapper 接口所在的包名。例如: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import tk.mybatis.spring.annotation.MapperScan; @SpringBootApplication @MapperScan("com.example.mapper") // 替换为实际的 mapper 路径 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` #### 原因三:Bean 定义缺失 有时开发者忘记给目标服务类标注适当注解(如 `@Service`, `@Component`)。这会阻止 Spring 自动装配过程正常工作[^3]。 修正方式很简单,在对应的服务类上方加上合适的注释即可。比如对于 `UserService` 来说应该是这样的形式: ```java package com.example.service; import org.springframework.stereotype.Service; @Service public class UserService { private final UserMapper userMapper; @Autowired public UserService(UserMapper userMapper){ this.userMapper = userMapper; } // Other methods... } ``` 另外需要注意的是,某些情况下即使已经定义好了 Service 层也需要确认 Repository/DAO 是否同样存在同样的问题[^2]^。 #### 原因四:SQL 映射文件错误或者路径不对 假如遇到类似于 “Invalid bound statement (not found)” 这样的提示信息则可能是由于 SQL 映射语句找不到所造成的。检查一下 resources 下面是否有正确的 xml 文件以及它们的名字是否匹配 controller 中调用的方法名称很重要[^4]。 --- ### 总结建议 综上所述,针对当前状况可以从以上几个方面逐一排查解决问题根源,并采取相应措施加以改正。通过合理设置组件扫描区域、运用恰当的注解标记重要业务逻辑单元等方式往往能有效缓解此类异常现象的发生频率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值