英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论