2011-11-18 163 views
0

我使用Spring 3和UserServiceImpl。該服務沒有看到Dao bean。Spring 3不注入DAO bean

這是我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(); 
     } 
    } 

那麼服務豆:

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

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


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

的testeoDAO方法以前只writeen用於測試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"/> 

而錯誤:

顯示java.lang.NullPointerException 在com.apress.usermanager.service.UserServiceImpl.testeoDAO(UserServiceImpl.java:49) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本機方法) 在sun.reflect.NativeMethodAccessorImpl。調用(NativeMethodAccessorImpl.java:39) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在java.lang.reflect.Method.invoke(Method.java:597)

+0

你在哪裏調用testeDao方法?你爲此寫了一個單元測試嗎?你運行程序來測試它嗎?如果是的話,你如何加載上下文? – melihcelik

回答

2

我想您需要添加

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

掃描包com.acom.b。這允許classpath scan並將所有@Component等等添加到它。

Annote UserServiceImpl@Service

問候。

+0

和是不一樣的? ' ' –

+0

即要添加到sessionfactory – ssedano

+0

thanks!我的問題,如果它是相同的sessionFactory中的屬性:packagesToScan我從書'春天在行動',閱讀相同的上下文:組件掃描... –