禁用 DataSourceAutoConfiguration Spring Boot。

huangapple go评论80阅读模式
英文:

Disable DataSourceAutoConfiguration Spring boot

问题

我有一个使用spring-jdbc的库,该库包含需要在多个项目中进行标准化的通用实用方法。
当将该库用于其他Spring Boot应用程序时,会导致项目失败,出现没有找到DataSourceConfuguration异常的情况。

我已阅读有关在@SpringBootApplication上排除DataSourceConfiguration的提示,但这意味着必须更改每个使用该库的应用程序,无论该应用程序是否需要数据源。

另一个选择是在库本身的spring.factories中排除DataSourceConfiguration,但这将停止任何使用该库的应用程序的自动配置能力,并且必须手动定义DataSource。

是否有可能使这种情况对库和任何希望使用该库的其他项目都起作用,但又不必定义数据源,并且仍然能够像正常的Spring Boot应用程序一样运行?

英文:

I have a library that makes use of spring-jdbc, the library contains common utility methods that need to be standardized across multiple projects.
The library when used in other spring boot application causes the project to fail with no bean on type DataSourceConfuguration Exception.

I have read tips to exclude DataSourceConfiguration on @SpringBootApplication but that would mean making change on every application that uses the library regardless of whether the application needs a datasource or not.

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}

The other option is to exclude DataSourceConfiguration in spring.factories of the library itself, but then it would stop the autoconfig ability of any application using the library and will have to manually define DataSource.

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

Is there a possible way to make this situation work for the library and any other project that wants to use the library but doesn't have to define a datasource and still function like a normal Spring Boot Application ?

答案1

得分: 1

以下是来自Spring文档的内容:

import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}

来源链接:https://docs.spring.io/spring-boot/docs/1.3.8.RELEASE/reference/html/using-boot-auto-configuration.html

英文:

The below is from Spring Documentation

import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}

https://docs.spring.io/spring-boot/docs/1.3.8.RELEASE/reference/html/using-boot-auto-configuration.html

huangapple
  • 本文由 发表于 2020年5月4日 20:52:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/61592644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定