Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to cancel the automatic injection of starter by springboot

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to cancel the automatic injection of starter by springboot". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "springboot how to cancel the automatic injection of starter"!

Catalogue

Springboot cancels automatic injection of starter

Look at the example.

Here comes the point.

SpringBoot automatic injection problem

The configuration file is as follows

Project catalog

Springboot cancels automatic injection of starter

Starer is a very important concept in spring boot. Starter is equivalent to a module, which can integrate the needed dependencies together and automatically assemble the bean in the module to the spring IOC container. Users only need to rely on the corresponding starter package in maven and do not need to do too much dependency to develop.

Look at the example.

For example, we have imported mybatis-related dependencies, but I may not use the database for the time being, so I have not done any database-related configuration, and the project will not be able to start

2020-03-08 22 ConfigServletWebServerApplicationContext 13 charge 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

Because the default database connection pool in springboot is hikari, if you do not configure the database in application.properties, you will not be able to load dataSource automatically

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]

Here comes the point.

How to cancel the automatic injection of related starer?

Let's take this database as an example:

@ SpringBootApplication (exclude = DataSourceAutoConfiguration.class)

Then, you need to add the above configuration to the startup class to cancel the automatic injection of DataSourceAutoConfiguration

SpringbootApplication is a combined annotation. In fact, it is this EnableAutoConfiguration annotation that really implements the function of automatic injection.

SpringBoot automatic injection problem

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.

There was a problem when the project was started.

Run

Controller

Service

Dao

The configuration file is as follows

Project catalog

Several classes have been found, and the ones that should be annotated have also been annotated.

At this point, I believe you have a deeper understanding of "springboot how to cancel the automatic injection of starter". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report