Java Reference
In-Depth Information
Constructor Injection
In this strategy, the dependent object is passed as part of the constructor call, as shown in
Listing 2-10.
Listing 2-10. with Constructor Injection
public class CarServiceImpl implements CarService{
private CarDao carDao;
public void CarServiceImpl (CarDao carDao){
this.carDao = carDao;
public void refuel(Car car){
carDao.updateFuelConsumed(car) ;
To achieve constructor injection, you need to alter the configuration as well, as
depicted in Listing 2-11.
Listing 2-11. spring-config.xml with Constructor Injection
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
<bean name="carDao"
class="com.apress.simpleapp.dao.CatDaoImpl" />
<bean name="carService"
<ref bean="carDao"/>
Search WWH ::

Custom Search