服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - springboot 如何取消starter的自动注入

springboot 如何取消starter的自动注入

2021-12-28 00:51qq_29914229 Java教程

这篇文章主要介绍了springboot 如何取消starter的自动注入操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

springboot 取消starter的自动注入

starer是spring boot中一个很重要的概念,starter相当于一个模块,它能将所需要的的依赖整合在一起并对模块内的bean自动装配到spring IOC容器,使用者只需要在maven中依赖相应的starter包并无需做过多的依赖即可进行开发。

看例子

比如,我们导入了mybatis相关的依赖,但是我可能暂时没用到数据库,所以就没有做数据库相关的配置,这时候项目就会无法启动

2020-03-08 22:13:10.396 WARN 10692 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource

[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed;

nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate
[com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception;

nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

因为springboot中默认的数据库连接池是hikari,你没有在application.properties里面进行数据库相关的配置的话,那么就会无法自动装载dataSource

Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]

重点来了

如何取消相关starer的自动注入?

我们还是以数据库的这个为例子:

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

那么,就需要在启动类加上如上配置,取消DataSourceAutoConfiguration的自动注入

springboot 如何取消starter的自动注入

springbootApplication是一个组合注解,其实里面真正实现自动注入功能的,是这个EnableAutoConfiguration注解

 

SpringBoot 自动注入问题

Description:

Field service in com.test.controller.UserController required a bean of type 'com.test.service.UserService' that could not be found.

Action:

Consider defining a bean of type 'com.test.service.UserService' in your configuration.

项目启动的时候出现出现问题

run

springboot 如何取消starter的自动注入

controller

springboot 如何取消starter的自动注入

service

springboot 如何取消starter的自动注入

dao

springboot 如何取消starter的自动注入

配置文件如下

springboot 如何取消starter的自动注入

项目目录

springboot 如何取消starter的自动注入

找了几个类,该注解的也注解了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_29914229/article/details/104742294

延伸 · 阅读

精彩推荐