【问题标题】:How to provide dynamic configration in Spring Boot如何在 Spring Boot 中提供动态配置
【发布时间】:2019-01-24 13:22:54
【问题描述】:

我想在生产中动态注入类。假设我们有一个 Shape 接口,它提供了 Rectangle 和 Triangle 这两个类的实现。

对于某些类,我想提供 Triangle 实例,而对于某些类,我想提供一个 Rectangle 类实例。

所以问题是如何在 Spring Boot 中做到这一点

interface Shape{
}

class Rectangle implements Shape{
}


class Triangel implements Shape{
}

【问题讨论】:

标签: spring-boot


【解决方案1】:

欢迎来到 SO

如果您在编译时知道要注入 Shape 类的哪个实现,那么您可以使用 @Qualifier 注释。你可以找到一些例子。

如果你想基于某些可以在运行时改变的属性注入 Bean,那么你可以使用@ConditionalAnnotations。您还将找到这方面的示例。

【讨论】:

    猜你喜欢
    • 2016-01-04
    • 1970-01-01
    • 2014-12-03
    • 2016-08-23
    • 2020-05-15
    • 2019-04-18
    • 2017-07-12
    • 2020-08-07
    • 1970-01-01
    相关资源
    最近更新 更多