What is wrong in my way of explainning DI and IoC?

Yesterday during an interview I was asked what DI and IoC in spring were. My reply was:

when a class(A) extends abstract class(B) or implements interface(B) or create a object of class(B) of any class in it, then A is said said to be dependent on B . Injecting this dependency, ie injecting the object in costructor or in setter method is called DI and in this process control over creating object goes to the "outside world" like XML configuration, this inversion of control is IoC. DI is not necessary IOC. We can still have DI when there is no IOC.

The interviewer didn't agree with me - where was I wrong?

One more thing-

As we used Super class reference variable or coding through interface in constructor or setter method parameter.Is this any way related with DI / IOC or this is only to achieve loose coupling ?


First statement***"when a class(A) extends abstract class(B) or implements interface(B)"***

This is inheritance , which can not be injected as dependency via Spring

Second Statement "create a object of class(B) of any class in it, then A is said said to be dependent on B"

sounds good

" Injecting this dependency, ie injecting the object in costructor or in setter method is called DI "

Not a clear statement to explain dependency injection. Here the meaning of injection needs to be explained. that is Management of dependencies is handled by spring container , It is who control their lifecycle and thus delegate/inject to the classes who ask for them(via spring config file )

"his process control over creating object goes to the "outside world" like XML configuration"

Here control neither goes to the outside world nor to the xml configuration file but to the spring container. And spring container uses this configuration file to do its work.

"this inversion of control is IoC. DI is not necessary IOC. We can still have DI when there is no IOC."

Though no issues with it but seems incomplete. Here IOC needs to be explained. Trying to explain it via image

非国际奥委会和国际奥委会


IoC ( I nversion o f C ontrol) is an abstract concept. It means objects do not create dependent objects directly, it gets from outside of the object scope.

There are several basic techniques to implement inversion of control.

  • Using a factory pattern
  • Using a service locator pattern
  • Using Dependency Injection(DI) , for example
  • Constructor injection
  • Parameter injection
  • Setter injection
  • Interface injection
  • Using a contextualized lookup
  • Using template method design pattern
  • Using strategy design pattern
  • Source

    链接地址: http://www.djcxy.com/p/82194.html

    上一篇: DI和IOC在春季mvc实施

    下一篇: 我解释DI和IoC的方式有什么问题?