Reputation: 1550
I've been trying to learn to use various featured of Spring 3.0.x and I've come across an issue when I'm trying to inject a session factory into a DAO implementation. I receive a NullPointerException when I try to use my injected SessionFactory instance variable which leads me to believe that the problem exists in the bean definition.
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.timerecorder"/>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@xxx.xxx.x.xxx:xxxx:xx"/>
<property name="username" value="xxx"/>
<property name="password" value="xxx"/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
</value>
</property>
</bean>
</beans>
EntryImpl.java
package com.timerecorder.entity;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class EntryImpl implements EntryDao
{
private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
@Override
public boolean saveEntry(EntryForm efd)
{
// Place the details from the entry form into the Entry entity
Entry e = new Entry();
e.setId(Long.valueOf(efd.getUserId()));
this.sessionFactory.getCurrentSession().save(e);
return true;
}
@Override
public boolean removeEntry(Long id)
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public List getEntries(Long id)
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public Entry getEntry(Long id)
{
return new Entry();
}
}
Any help would be appreciated.
Kind Regards,
Justin
Upvotes: 0
Views: 3402
Reputation: 18028
Looks like it may be a naming convention problem in your bean definations.
Try renaming
id= "mySessionFactory"
to
id= "sessionFactory"
Let me know if that helps.
Upvotes: 2
Reputation: 1205
Try the method init() to autowire the session factory.
This is how it works for me ->
@Repository
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@Autowired
public void init(SessionFactory factory) {
setSessionFactory(factory);
}
}
setSessionFactory is the method provided by HibernateDaoSupport
Upvotes: 0
Reputation: 477
It looks like you're autowiring by name but you need to autowire your sessionFactory by type instead - or you can rename the bean definition to sessionFactory and it should just work.
Also, you need to be sure you're getting your bean out of the application context instead of just new'ing it.
Upvotes: 0