Spring Boot Annoatations


1. @Autowired

  • Used for dependency injection (Spring automatically injects the required bean).
  • By default, it matches by type.
@Component
class Engine {
    public String start() { return "Engine started"; }
}

@Component
class Car {
    @Autowired
    private Engine engine;

    public void drive() {
        System.out.println(engine.start() + " - Car is driving");
    }
}

2. @Qualifier

  • Used when multiple beans of the same type exist, to avoid ambiguity.
  • Works with @Autowired.
@Component("dieselEngine")
class DieselEngine implements Engine {}

@Component("petrolEngine")
class PetrolEngine implements Engine {}

@Component
class Car {
    @Autowired
    @Qualifier("dieselEngine")
    private Engine engine;
}

3. @Primary

  • If multiple beans are available, Spring uses the @Primary bean unless @Qualifier is used.
@Component
@Primary
class PetrolEngine implements Engine {}

@Component
class DieselEngine implements Engine {}

@Component
class Car {
    @Autowired
    private Engine engine; // PetrolEngine will be injected (because of @Primary)
}

4. @Resource

  • From Jakarta (javax.annotation), not Spring-specific.
  • By default, injects beans by name (unlike @Autowired which is by type).
@Component("myEngine")
class Engine {}

@Component
class Car {
    @Resource(name = "myEngine")
    private Engine engine;
}

5. @Value

  • Injects literal values or values from application.properties.

application.properties

car.name=Tesla

Java:

@Component
class Car {
    @Value("${car.name}")
    private String carName;
}

6. @ConfigurationProperties

  • Maps external configuration properties (from .yml or .properties) to a POJO.

application.yml

car:
  name: Tesla
  model: Model-S
  year: 2025

Java:

@Component
@ConfigurationProperties(prefix = "car")
class CarProperties {
    private String name;
    private String model;
    private int year;

    // getters & setters
}

Now you can inject it:

@Component
class CarService {
    private final CarProperties carProperties;

    public CarService(CarProperties carProperties) {
        this.carProperties = carProperties;
    }
}

7. @Lazy

  • Delays bean creation until it’s needed (by default, Spring creates beans eagerly).
@Component
@Lazy
class HeavyEngine {
    public HeavyEngine() {
        System.out.println("HeavyEngine initialized");
    }
}

Only when you call context.getBean(HeavyEngine.class), it will initialize.


8. @Order

  • Defines the execution order of beans when multiple beans implement the same interface (used in Lists).
public interface Engine {}

@Component
@Order(1)
class DieselEngine implements Engine {}

@Component
@Order(2)
class PetrolEngine implements Engine {}

@Component
class Car {
    @Autowired
    private List<Engine> engines; // Will inject [DieselEngine, PetrolEngine] in order
}

Summary Table

AnnotationPurpose
@AutowiredAuto-inject dependency (by type).
@QualifierResolve ambiguity when multiple beans exist.
@PrimaryMark default bean when multiple exist.
@ResourceInject by name (JSR annotation).
@ValueInject values from properties.
@ConfigurationPropertiesBind external properties to POJO.
@LazyCreate bean only when needed.
@OrderDefine order for bean execution.

Leave a Reply