When building
Spring controllers, it is often useful to access a DAO instance which has been set up in
applicationContext.xml
. To do so, add a variable in the controller for the DAO as well as a setter for it. Once this is done, wire it up by reference in
dispatcher-servlet.xml
.
Example DAO in applicationContext.xml:
<bean id="listHandler" class="com.example.dao.ListHandler">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
Add code like this to the controller class:
private ListHandler listHandler;
public void setListHandlerDao(ListHandler p) {
listHandler = p;
}
Then add the bold item below to the controller definition in dispatcher-servlet.xml:
<bean name="sampleController"
class="com.example.SampleController"
p:listHandlerDao-ref="listHandler"
/>