0
votes

I'm using Spring 3 and UserServiceImpl. The service does not see the Dao bean.

This is my UserDao:

    @Repository
    public class UserDaoImpl implements UserDao {
        private SessionFactory sessionFactory;

        public SessionFactory getSessionFactory() {
            return this.sessionFactory;
        }

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

        private Session currentSession() {
            return sessionFactory.getCurrentSession();
        }
    }

Then the Service bean :

    public class UserServiceImpl implements UserService {
        @Autowired
        private UserDao userDao;

        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }


        public String testeoDAO() {
            return userDao.funciona();
        }
    }

The testeoDAO method had writeen only for testing the DAO.The applicationContext

    <context:annotation-config/>

    <bean id="userDao" class="com.apress.usermanager.dao.hibernate.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="userService" class="com.apress.usermanager.service.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>


    <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
          autowire="byName">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.apress.usermanager"/>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
                hibernate.query.substitutions=true 'Y', false 'N'
                hibernate.cache.use_second_level_cache=true
                hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
                hibernate.hbm2ddl.auto=update
                hibernate.use_sql_comments=true
                hibernate.show_sql=true
                hibernate.current_session_context_class=thread
            </value>
        </property>
    </bean>


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="100"/>
        <property name="maxWait" value="1000"/>
        <property name="poolPreparedStatements" value="true"/>
        <property name="defaultAutoCommit" value="true"/>
        <property name="testOnBorrow" value="true"/>
        <property name="validationQuery" value="select 1=1"/>
    </bean>


    <bean id="staticDataImporter" class="com.apress.usermanager.support.StaticDataImporter" depends-on="sessionFactory">
        <property name="dataSource" ref="dataSource"/>
        <property name="staticData" value="classpath:default-data.xml"/>
    </bean>

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

And the error :

java.lang.NullPointerException at com.apress.usermanager.service.UserServiceImpl.testeoDAO(UserServiceImpl.java:49) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597)

1
Where do you call the testeDao method? Did you write a unit test for that? Do you run the program to test it and if so, how do you load the context? - melihcelik

1 Answers

2
votes

I think you need to add

<context:component-scan base-package="com.a,com.b">
</context:component-scan>

To scan package com.a and com.b. This allows the classpath scan and add all @Component and so forth to it.

Annote UserServiceImpl with @Service.

Regards.