如何避免使用Spring Boot由另一个库配置外部库 [英] How to avoid to configure an external library by another library using Spring Boot

查看:49
本文介绍了如何避免使用Spring Boot由另一个库配置外部库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个Spring引导项目.第一个项目只是一个提供各种功能的支持库.第二个是使用支持项目(jar)的主"项目(主库).

Let’s assume I have two Spring boot projects. The first project is just a supporting library providing various functionalities. The second is the "main" project (primary library) which uses the supporting project (jar).

支持库具有一些应由主项目自动连接的服务.

The supporting library has some services which should be autowired by the primary project.

如何配置支持库,以便主库不必对支持库进行任何配置.本质上,我是在谈论组件扫描.

How can I configure the supporting library so that the primary library does not have to do any configurations for the supporting library. In essence I am talking about component scan.

到目前为止,我读到的是,主库必须在组件扫描中包括支持库的软件包.这是真的吗?我希望不会,因为我认为支持库应该自己完成所有配置,而仅使用该支持库的另一个库(或项目)就不必处理支持库的配置.

What I have read so far is, that the primary library has to include the packages of the supporting library in component scan. Is this really true? I hope not, because in my opinion a supporting library should do all its configurations by its own and another library (or project) just using this supporting library should not have to deal with the configuration of the supporting library.

从本质上讲,我想知道支持库是否有可能通过 @Configuration 注释或类似的注释(或 @SpringBootApplication )进行自身配置,从而使另一个库能够不必处理其他库的所有细节.我真的希望它能以某种方式起作用,因为我认为,另一个库就其配置而言,必须为其支持库做些工作.

In essence I am wondering if it is possible that a supporting library configures itself via a @Configuration annotation or that like (or @SpringBootApplication) so that another library does not have to deal with all that details of the other library. I really hope that this does work somehow because I think it is kind of extremely wired that another library has to do the job for it’s supporting library in regard of their configuration.

推荐答案

您需要在项目中添加依赖项 spring-boot-configuration-processor ,例如@M.Deinum提到了.这是有效的示例.但是,使用这种方法时,我还没有找到一种拥有多个bean的方法.

You need to add a dependency spring-boot-configuration-processor in your project, like @M. Deinum mentioned. Here's the link of a working example. However, I haven't found a way to have multiple beans when using this method.

此操作的另一种方法显示在堆栈溢出的 answer 中.

Another way to do this is shown in this answer on Stack Overflow.

这篇关于如何避免使用Spring Boot由另一个库配置外部库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆