Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Spring Boot FAQ: Top Questions

46. What is the use of @Conditional annotation in Spring Boot?

@Conditional is used to conditionally register a Spring bean depending on the result of a specified condition.

πŸ—ΊοΈ Steps:

  1. Define a class implementing Condition.
  2. Annotate bean method or class with @Conditional(MyCondition.class).

πŸ“₯ Example:

public class OnWindowsCondition implements Condition {
  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    return System.getProperty("os.name").contains("Windows");
  }
}

πŸ† Expected Output:

Bean is registered only on Windows OS.

πŸ› οΈ Use Cases:

  • Load beans based on environment, classpath, or property.
  • Enable/disable configuration features conditionally.