选择性的Bean加载

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

Selective bean loading

问题

我有一个库和不同的应用程序使用该库。
问题在于每个应用程序仅使用库的一部分,而非全部。
那么我该如何避免在未使用的应用程序中加载这些“beans”呢?
非常感谢。

英文:

I have a library and different applications that use that library.
The problem comes that each application uses a part of the library, not all.
So how can I avoid loading the beans in the application that are not used?
thank you very much

答案1

得分: 0

我可以看到 3 个选项。

  1. spring profiles(https://www.baeldung.com/spring-profiles)- 将你的 bean 标记为由一个或另一个配置文件激活,并让应用程序定义要使用的配置文件。
  2. @ConditionalOnProperty(https://www.baeldung.com/spring-conditionalonproperty)- 将你的 bean 标记为由特定属性的特定值激活,并让应用程序定义这些属性。
  3. 更加复杂一些 - 将你的库转换为 Spring Boot Starter 并定义多个自动配置类:https://www.baeldung.com/spring-boot-starters
英文:

I can see 3 options.

  1. spring profiles (https://www.baeldung.com/spring-profiles) - mark your beans as activated by one or another profile and let applications define the profile to use.
  2. @ConditionalOnProperty (https://www.baeldung.com/spring-conditionalonproperty) - mark your beans as activated by specific value of specific property and let applications define those properties.
  3. A bit more complicated - turn your library into spring boot starter and define multiple auto configuration classes: https://www.baeldung.com/spring-boot-starters

huangapple
  • 本文由 发表于 2023年2月19日 18:29:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499464.html
匿名

发表评论

匿名网友

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

确定