Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autowired sessionFactory object is null

Getting null pointer exception in UesrDAO class as the sessionFactory object is always null. If this helps, I found out that this thing happens if the DAO is manually created using 'new' instead of letting spring handle it. I am not doing so, I am injecting UserDAO in service class but still getting sessionFactory as null.

Any help is appreciated, thanks.

Below is the content of relevant files:

hibernate.cfg.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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>/WEB-INF/properties/database.properties</value>
        </property>
    </bean>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="packagesToScan">
            <list>
                <value>org.questionbank.dto</value>
            </list>
        </property>
    </bean>
</beans>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:security="http://www.springframework.org/schema/security"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  http://www.springframework.org/schema/security
  http://www.springframework.org/schema/security/spring-security-3.2.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <import resource="hibernate.cfg.xml"/>
    <mvc:annotation-driven />
    <tx:annotation-driven />
    <context:annotation-config />
    <!--  To scan the components -->
    <context:component-scan base-package="org.questionbank"/>
    <bean id = "transactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name = "sessionFactory" ref = "sessionFactory" />
    </bean> 
</beans>    

DAO class:

public class UserDAO 
{
    protected static Logger logger = Logger.getLogger("dao");

    @Autowired
    private SessionFactory sessionFactory;

    public UserDTO searchDatabase(String username) 
    {
        logger.debug("Seraching for user in DB");
        UserDTO user=new UserDTO();
        Query query = sessionFactory.getCurrentSession().createQuery("FROM UserDTO u WHERE u.userName = :userName");
        query.setParameter("userName", username);
        user = (UserDTO) query.uniqueResult();
        if(user!=null)
            return user;
        logger.error("User does not exist!");
        throw new RuntimeException("User does not exist!");
    }
}

Service class:

@Transactional(readOnly = true)
public class CustomUserDetailsService implements UserDetailsService 
{
    protected static Logger logger = Logger.getLogger("service");
    @Autowired
    private UserDAO userDAO;
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException 
    {
        UserDetails user = null;
        try 
        {
            UserDTO dbUser = userDAO.searchDatabase(username);
            user =  new User(
                    dbUser.getUserName(), 
                    dbUser.getPassword().toLowerCase(),
                    true,
                    true,
                    true,
                    true,
                    getAuthorities(Integer.parseInt(dbUser.getAccess())));
            } catch (Exception e) {
            logger.error("Error in retrieving user");
            e.printStackTrace();
            throw new UsernameNotFoundException("Error in retrieving user");
        }
        return user;
    }
like image 578
usercpd Avatar asked Oct 31 '22 15:10

usercpd


1 Answers

Your current configuration, with <context:annotation-config /> and <context:component-scan base-package="org.questionbank"/> enables the first half of the functionally that you need in order to make Spring correctly recognize and inject your bean.

The other half is actually declaring the bean as a Spring-managed component.

You can do this in one of two ways. Either declare the bean in applicationContext.xml with a <bean /> tag (just as you did with your transactionManager) or use a stereotype annotation (such as @Component, @Repository, @Service or @Controller).

@Repository
public class UserDAO { // ... 

And

@Service
public class CustomUserDetailsService { // ...
like image 165
Anthony Accioly Avatar answered Nov 11 '22 17:11

Anthony Accioly