组件使用应用程序扫描。

发布于 2025-02-10 13:55:52 字数 193 浏览 2 评论 0原文

我制作了一个具有组件和配置类的Java库。

当我在其他Spring Boot服务中使用库时,没有注册Bean,因为组件和配置类不在类路径中。

我知道我们可以使用@componentscan,但我不想更改服务的代码。 是否有一种方法可以使用application.properties将类添加到类路径? 还是我可以在库中做些什么,以便将豆子注册?

I made a java library that has component and configuration classes.

When I use the library in other spring boot services, the beans are not registered because the component and configuration classes are not in the classpath.

I know we can use @ComponentScan but I don't want to change the service's code.
Is there a way of adding the classes to the classpath using application.properties?
Or is there anything I can do in the library so that the beans get registered?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中楼上月下 2025-02-17 13:55:52

如果您使用的是Spring Boot,则可以利用Spring AutoConfiguration。

为此,您需要将文件spring.factories放入meta-inf/spring.factories中的.jar文件中。如果您将Gradle或Maven用作构建工具和标准文件夹结构,则文件路径为src/Main/resource/Meta-Inf/Spring.factories

这是一个我写的库

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.requirementsascode.spring.behavior.web.BehaviorConfiguration,\
org.requirementsascode.spring.behavior.web.SerializationConfiguration,\
org.requirementsascode.spring.behavior.web.BehaviorController

如您所见,在第一个弹簧特定行之后,您列出了所有配置类。

您可以了解有关AutoConfiguration 在这里的更多信息。

If you are using Spring Boot, you can take advantage of Spring Autoconfiguration.

For that, you need to place a file spring.factories in META-INF/spring.factoriesin your library's .jar file. If you are using Gradle or Maven as a build tool and the standard folder structure, the file path is src/main/resources/META-INF/spring.factories.

Here's an example from a library I wrote:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.requirementsascode.spring.behavior.web.BehaviorConfiguration,\
org.requirementsascode.spring.behavior.web.SerializationConfiguration,\
org.requirementsascode.spring.behavior.web.BehaviorController

As you can see, after the first, Spring specific line, you list all of your configuration classes.

You can learn more about autoconfiguration here, for example.

云之铃。 2025-02-17 13:55:52

您可以使用autowirebean() autowirecapablebeanfactory的方法来执行此操作。
在此处阅读更多信息: autowirecapablebeanfactory

You can do this using the autowireBean() method of AutowireCapableBeanFactory.
Read more here: AutowireCapableBeanFactory

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文