genkio / blog

Stay hungry stay foolish
https://slashbit.github.io/blog/
0 stars 1 forks source link

Three ways of Spring beans dependency injection #95

Open genkio opened 8 years ago

genkio commented 8 years ago

Property based injection

@RestController
public class PageController {
  @Autowired
  private NotificationService notificationService;
}

Setter based injection

@RestController
public class PageController {
  private NotificationService notificationService;

  @Autowired
  public void setNotificationService(NotificationService notificationService) {
    this.notificationService = notificationService;
  }
}

Constructor based injection

public class PageController {
  private NotificationService notificationService;

  @Autowired
  public PageController(NotificationService notificationService) {
    this.notificationService = notificationService;
  }
}
ghost commented 7 years ago

顶,才看到