Junior — Middle
Have you used the @Conditional annotation in your projects?
sobes.tech AI
Answer from AI
The @Conditional annotation in Java, most commonly used in Spring Framework, allows conditionally including or excluding beans from the context based on specified conditions. This is useful for configuring the application differently for various environments or parameters.
For example, you can create a bean that loads only if a certain class is present in the classpath or if a specific property is set.
Example:
@Configuration
public class AppConfig {
@Bean
@ConditionalOnProperty(name = "feature.enabled", havingValue = "true")
public MyService myService() {
return new MyService();
}
}
In this example, the myService bean is created only if the property feature.enabled is set to true in the application settings. This allows flexible control over application behavior without changing code.