Emmachen / SapUi5Test

1 stars 1 forks source link

An example of Dependency injection #35

Open ghost opened 8 years ago

ghost commented 8 years ago

Suppose Order Service needs Stock service as dependency.

Solution

The OrderService simply needs to define a setter method:

public void setStockService(StockService) {
this.stockService = stockService;
}

Then in an XML file you can define the OrderService like this:

<bean class="de.hybris.platform.order.OrderService">
<property name="stockService">
<bean class="de.hybris.platform.stock.StockService"/>
</property>
</bean>

Here you define the OrderService and have the StockService injected (hence the name of the pattern). A so-called container (application context) reads this configuration file, resolves the dependencies and puts together the objects. When the objects are ready, all the dependencies are already injected.