Jeonguk Lee

I am java developer.

Spring Core Annotations

11 Jun 2018 » java, spring

Spring Core Annotations

org.springframework.beans.factory.annotation과 org.springframework.context.annotation 패키지의 주석을 사용하여 Spring DI 엔진의 기능을 활용할 수 있습니다.

DI-Related Annotations

@Autowired

  • Constructor injection:
class Car {
    Engine engine;
 
    @Autowired
    Car(Engine engine) {
        this.engine = engine;
    }
}
  • Setter injection:
class Car {
    Engine engine;
 
    @Autowired
    void setEngine(Engine engine) {
        this.engine = engine;
    }
}
  • Field injection:
class Car {
    @Autowired
    Engine engine;
}

@Bean

  • @Bean marks a factory method which instantiates a Spring bean:
@Bean
Engine engine() {
    return new Engine();
}
@Bean("engine")
Engine getEngine() {
    return new Engine();
}