springboot 如何取消starter的自动注入

作者:qq_29914229 时间:2023-02-06 21:31:05 

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

标签:springboot,starter,自动注入
0
投稿

猜你喜欢

  • 关于Java集合框架面试题(含答案)上

    2021-06-02 13:29:44
  • 解析Android框架之OkHttp3源码

    2022-12-05 16:28:02
  • C#中Lambda表达式的用法

    2022-12-22 15:59:17
  • 利用Java实现在PDF中添加工具提示

    2022-08-07 18:47:02
  • Android8.0适配前台定位服务service的示例代码

    2021-05-25 17:18:36
  • Spring+MyBatis实现数据读写分离的实例代码

    2021-08-31 04:34:48
  • 使用注解@Validated效验VO参数是否合规

    2023-10-27 20:13:01
  • Java简单实现调用命令行并获取执行结果示例

    2023-02-08 18:53:32
  • Android实现从底部弹出的Dialog示例(一)

    2021-09-12 13:33:09
  • Java 延迟队列的常用的实现方式

    2022-06-30 13:57:00
  • 浅谈java的守护线程与非守护线程

    2023-11-25 06:50:23
  • Android编程中沉浸式状态栏的三种实现方式详解

    2022-02-10 11:56:23
  • c#读取文件详谈

    2023-03-04 14:47:30
  • C# 匿名方法基础回顾

    2023-02-27 13:59:23
  • Springboot多种情况yml配置代码实例

    2022-05-14 23:26:00
  • Android发送xml数据给服务器的方法

    2021-12-19 13:06:44
  • 详解java设计模式中的门面模式

    2021-08-17 18:16:25
  • Spring Boot全局异常处理解析

    2023-07-06 07:16:35
  • Spring零基础入门WebFlux响应式编程

    2022-07-12 07:25:53
  • java删除文件和文件夹具体实现

    2023-09-26 20:46:55
  • asp之家 软件编程 m.aspxhome.com