Robby Robby - 9 months ago 68
Java Question

Spring 4 + Hibernate 4 configuration

I am trying to do Spring 4 + Hibernate 4 configuration. But I am facing sessionFactory = null in the Controller. Below are the configurations and code.
What is the mistake in this configuration?


<beans xmlns=""

<!-- MySQL data source -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/personal" />
<property name="username" value="root" />
<property name="password" value="password" />

<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<property name="hibernateProperties">
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>

<bean id="songDao" class="com.letsdo.impl.SongImpl">
<property name="sessionFactory" ref="sessionFactory"></property>

<!-- MUST have transaction manager, using aop and aspects -->
<bean id="transactionManager"
<property name="sessionFactory" ref="sessionFactory"></property>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<class name="com.letsdo.model.Song" table="song" catalog="personal">
<id name="id" type="int">
<column name="id" length="45" />
<generator class="increment"/>
<property name="filePath" column="filepath"/>
<property name="fileName" column="filename"/>
<property name="album" column="album"/>
<property name="title" column="title"/>
<property name="size" column="size"/>

public interface SongDao {

public List<String> getAllAlbums();

public class SongImpl implements SongDao{

private SessionFactory sessionFactory;

public List<String> getAllAlbums(){
List<String> allAlbums = new ArrayList<String>();
Query query = getSessionFactory().getCurrentSession().createQuery("Select DISTINCT Album from song");
allAlbums = query.list();
return allAlbums;

public SessionFactory getSessionFactory() {
return sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;



public class HomeController {

@Autowired private SongImpl songDao;
@RequestMapping(value="/", method=RequestMethod.GET)
public ModelAndView welcomePageBeforeLogin(HttpServletRequest request, HttpServletResponse response,HttpSession session){
ModelAndView model = new ModelAndView();
List<String> album = songDao.getAllAlbums();
return model;

Answer Source

I think you are ending up with two beans of class SongImpl - one that is defined in xml (named songDao), and another one annotated with @Service (named by default songImpl). The latter one has no SessionFactory autowired, that's why it is null.

You can find more info about it in one of the answers here.

If component scanning is enabled, spring will try to create a bean even though a bean of that class has already been defined in the spring config xml. However if the bean defined in the spring config file and the auto-discovered bean have the same name, spring will not create a new bean while it does component scanning.

The solution is to remove the xml version of the bean (if you don't need it), and to autowire SessionFactory in SongImpl.