I autowire a DAO with the controller. Do not ask me, ‘Hey stupid, why do you want to couple the controller with a DAO’. Because I’m setting up the environment. Service tier is not yet ready. Ok!
package org.grassfield.vriksha.controller;
import java.util.List;
import org.grassfield.common.dao.IUserRoleDAO;
import org.grassfield.common.entity.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class VrikshaController {
@Autowired
IUserRoleDAO userRoleDAO;
public IUserRoleDAO getUserRoleDAO() {
return userRoleDAO;
}
public void setUserRoleDAO(IUserRoleDAO userRoleDAO) {
this.userRoleDAO = userRoleDAO;
}
@RequestMapping("/welcome")
public ModelAndView helloWorld(){
System.out.println("1");
List<UserRole> list = this.userRoleDAO.list();
String message = "<h3>Welcome</h3>"+list;
return new ModelAndView("welcome", "message", message);
}
}
Okay, While hitting the page, it is very eager to show me an error page. 
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134) org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014) org.grassfield.common.dao.UserRoleDAO.list(UserRoleDAO.java:26)
It is my mistake on the configuration XML. Let’s add the annotation-driven tag.
<tx:annotation-driven/> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean>
It is not sufficient.
bean schema locations.
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:p=”http://www.springframework.org/schema/p”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:tx=”http://www.springframework.org/schema/tx”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd”>
Here you go! This will help you to resolve this error.