组件使用应用程序扫描。
我制作了一个具有组件和配置类的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是Spring Boot,则可以利用Spring AutoConfiguration。
为此,您需要将文件
spring.factories
放入meta-inf/spring.factories
中的.jar文件中。如果您将Gradle或Maven用作构建工具和标准文件夹结构,则文件路径为src/Main/resource/Meta-Inf/Spring.factories
。这是一个我写的库:
如您所见,在第一个弹簧特定行之后,您列出了所有配置类。
您可以了解有关AutoConfiguration 在这里的更多信息。
If you are using Spring Boot, you can take advantage of Spring Autoconfiguration.
For that, you need to place a file
spring.factories
inMETA-INF/spring.factories
in your library's .jar file. If you are using Gradle or Maven as a build tool and the standard folder structure, the file path issrc/main/resources/META-INF/spring.factories
.Here's an example from a library I wrote:
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.
您可以使用
autowirebean()
autowirecapablebeanfactory
的方法来执行此操作。在此处阅读更多信息: autowirecapablebeanfactory
You can do this using the
autowireBean()
method ofAutowireCapableBeanFactory
.Read more here: AutowireCapableBeanFactory