如何使用功能标志禁用application.properties设置

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

How to use feature flags to disable application.properties setting

问题

我明白我可以使用 @ConditionalOnExpression 来启用或禁用组件,比如 @RestController@Service@Entity。这一点很清楚。

然而,Spring Boot 还有更多。它有 application.properties 配置,有 @Autowired 属性,有 @Test 类和方法。

例如,假设我在 application.properties 文件中定义了一个特性标志:

myFeature.enabled=false

我想要禁用以下内容:

  • application.properties 中禁用数据库连接设置
    // 我想使用 myFeature.enabled 标志禁用所有这些
    spring.jpa.hibernate.ddl-auto=update
    spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
    spring.datasource.username=springuser
    spring.datasource.password=ThePassword
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    #spring.jpa.show-sql: true
  • 在类中禁用@Autowired属性,我只想禁用注入的这一个属性
    @Component
    public class FooService {  
        @Autowired
        private Foo foo; // 我只想使用 myFeature.enabled 标志禁用这个属性
    }
  • 禁用测试类(我知道可以使用 @Ignore@Disable)使用 myFeature.enabled 标志
    @SpringBootTest
    public class EmployeeRestControllerIntegrationTest {
    
        ...
    
        // 测试方法
    }

我如何使用 myFeature.enabled 标志禁用这些?

我知道在 @Entity 或 @Controller 类的情况下,我可以简单地像下面这样做,但我不知道如何使用相同的机制来禁用上述三种情况:

    @Entity
    @ConditionalOnExpression(${myFeature.enabled})
    public class Employee {
       ...
    }
英文:

I understand that I can use @ConditionalOnExpression to enable or disable a component such as a @RestController, or @Service, or @Entity. That is all clear.

However, Spring Boot has more. It has application.properties configuration, it has @Autowired properties, it has @Test classes/methods.

For example, given I have feature flag defined in my application.properties file as:

myFeature.enabled=false

I want to disabled following:

  • database connectivity settings in application.properties
    // I want to disable all these using myFeature.enabled flag
    spring.jpa.hibernate.ddl-auto=update
    spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
    spring.datasource.username=springuser
    spring.datasource.password=ThePassword
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    #spring.jpa.show-sql: true
  • an@Autowired property in a class and I only want to disable that one injected property
    @Component
    public class FooService {  
        @Autowired
        private Foo foo; // I want to disable only this property using myFeature.enabled flag
    }
  • Test class and I want to disable it (I know I can use @Ignore or @Disable) using myFeature.enabled flag
    @SpringBootTest
    public class EmployeeRestControllerIntegrationTest {
    
        ...
    
        // tests methods
    }

How do I disable these using myFeature.enabled flag?

I know that in case of an @Entity or @Controller class I can simply do something like below, but I dont know how to use same mechanism to disable the 3 above cases:

@Entity
@ConditionalOnExpression(${myFeature.enabled})
public class Employee {
   ...
}

答案1

得分: 1

根据你的问题,我认为使用 Spring Profiles 是更合适的选择。通过使用 profiles,你可以:

  • 拥有一个包含特定配置的 application-{profile}.properties 文件,这些配置只有在相应的 profile 激活时才会生效。
  • 如果某个 profile 激活,你可以选择启用 FooService 的特定实现并禁用默认版本。
  • 只有在特定 profile 激活时,你才能 启用一个测试
英文:

From your question, I think using spring profiles is a much better fit. With profiles you can:

  • have an application-{profile}.properties file containing profile-specific properties that are only used if the profile is active.
  • You can choose to enable a specific implementation of your FooService and disable the default version if a profile is active.
  • You can enable a test only if the profile is active.

huangapple
  • 本文由 发表于 2023年2月27日 08:07:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575801.html
匿名

发表评论

匿名网友

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

确定