Java Reference
In-Depth Information
Listing 2-8. CarServiceImpl.java
public class CarServiceImpl implements CarService{
private CarDao carDao;
public void refuel(Car car){
carDao.updateFuelConsumed(car) ;
}
public void setCarDao(CarDao carDao){
this.carDao = carDao;
}
}
The CarDao object is passed by the Spring IOC container using the setCarDao method. Now
you must wire things up so that Spring knows how to resolve and inject the dependency.
You can do this with a simple configuration, as shown in Listing 2-9.
Listing 2-9. spring-config.xml : Setter Injection
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=" http://www.springframework.org/schema/beans"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
>
<bean name="carDao"
class="com.apress.simpleapp.dao.CatDaoImpl" />
<bean name="carService"
class="com.apress.simpleapp.service.CatServiceImpl">
<property name="carDao"
ref="carDao" />
</bean>
</beans>
 
Search WWH ::




Custom Search