欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

springboot 如何取消starter的自动注入

时间:2021-12-24 11:07:07|栏目:JAVA代码|点击:

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的自动注入

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

controller

service

dao

配置文件如下

项目目录

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

上一篇:Hibernate传入Java对象创建动态表并录入数据

栏    目:JAVA代码

下一篇:IDEA项目重命名的操作

本文标题:springboot 如何取消starter的自动注入

本文地址:http://www.codeinn.net/misctech/187932.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有