0 0

SSH集成 UserDao SessionFactory 无法注入,求解5

applicationContext.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<context:annotation-config />
<context:component-scan base-package="xhd.ffms" />

<!-- 
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
-->

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/family">
</property>
<property name="username" value="root"></property>
<property name="password" value="password"></property>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<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>
<prop  key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>xhd/ffms/model/FamilyUser.hbm.xml</value>
<value>xhd/ffms/model/FamilyAccount.hbm.xml</value>
<value>xhd/ffms/model/FamilyItem.hbm.xml</value></list>
</property>
</bean>
<bean id="transactionManager" 
        class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
        <property name="sessionFactory" ref="sessionFactory"> 
        </property> 
    </bean> 
      
    <tx:advice id="txAdvice" transaction-manager="transactionManager"> 
        <tx:attributes> 
            <tx:method name="update*" propagation="REQUIRED" read-only="false" /> 
            <tx:method name="delete*" propagation="REQUIRED" read-only="false" /> 
            <tx:method name="save*" propagation="REQUIRED" read-only="false" /> 
            <tx:method name="*" propagation="SUPPORTS" read-only="true" /> 
        </tx:attributes> 
    </tx:advice> 
 
    <aop:config> 
        <aop:pointcut id="ServiceMethods" expression="execution(public * xhd.ffms.service..*(..))" /> 
        <aop:advisor advice-ref="txAdvice" pointcut-ref="ServiceMethods" /> 
    </aop:config> 

</beans>
UserServiceImpl.java中
@Component("userService")
public class UserServiceImpl implements UserService {
private static final Logger logger = Logger.getLogger(UserServiceImpl.class);
private UserDao userDao;

public UserDao getUserDao() {
return userDao;
}
@Resource
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
        public FamilyUser getUserByUserName(String username) throws Exception {
logger.info("执行UserServiceImpl.getUserByUserName()......");
return userDao.getUserByUserName(username);
}
}
执行getUserByUserName()时抛出nullPointException,UserDao是空



问题补充:
aronlulu 写道
UserDao 加@Component("userDao") 了么
UserDao在这个包下么 xhd.ffms

xhd.ffms.dao.impl.UserDaoImpl.java加了@Component("userDao"),
使用Junit test 可以通过并可以查询数据,但运行页面就出现nullpointException

问题补充:使用Junit Test可以查询数据库,UserDao 和SessionFactory 都注入了,但运行页面时,UserDao 和SessionFactory 都是null
2012年3月20日 17:51
  • 大小: 57.3 KB

3个答案 按时间排序 按投票排序

0 0

那就是少扫描拦截器了。
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
还不行你参考spring官方手册重新配置看看:
肯定是少配一项。

2012年3月20日 19:29
0 0

<bean
  class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>

扫描注释

2012年3月20日 18:30
0 0

UserDao 加@Component("userDao") 了么
UserDao在这个包下么 xhd.ffms

2012年3月20日 18:26

相关推荐

Global site tag (gtag.js) - Google Analytics