Difference between creating new object and dependency injection

后端 未结 6 2267
粉色の甜心
粉色の甜心 2020-12-08 01:24

What is the difference between creating a new object and dependency injection? Please explain in detail.

6条回答
  •  眼角桃花
    2020-12-08 01:52

    Of course both create objects. The difference is in who is responsible for the creation. Is it the class which needs its dependencies or a container like Spring for example, which wires the components dependencies. You configure the dependencies in a separate(typically XML) configuration file.

    It is really a separation of concerns. The class says I need this, this and this component so I functionate properly. The class doesn't care how it gets its components. You plug in them into the class with a separate configuration file.

    To give you an example lets consider having a shopping class which needs a payment module. You don't want to hardcode which payment module will be used. To achieve this you inverse the control. You can change the used payment module with a few keystrokes in the configuration file of the container. The power is that you aren't touching any Java code.

提交回复
热议问题