SSH集成getHibernateTemplate查询时报空指针异常。 本帖最后由 ff0617 于 2011-03-31 14:51:07 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 很明显,注入没成功,使用PetInfoDaoImpl 的地方也是要注入的。 <bean id="PetInfo" class="com.yh.dao.impl.PetInfoDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property></bean>我这一段没有注入成功吗? <bean id="PetInfo" class="com.yh.dao.impl.PetInfoDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property></bean>bean id 怎么能大写呢? 你看看你在service里面 实例化是怎么写的? 那个不能够大写的么? public PetInfoId HibLoad(Integer id) { PetInfoDao p=new PetInfoDaoImpl(); PetInfoId pet=p.HibLoad(id); return pet; } ...... PetInfoDao p=new PetInfoDaoImpl(); 是不是该这样? private PetInfoDAO petInfoDAO = null; public PetInfoDAO getPetInfoDAO() { return petInfoDAO; } public void setPetInfoDAO(PetInfoDAO petInfoDAO) { this.petInfoDAO = petInfoDAO; } public PetInfo load(Integer id) { PetInfo item = this.petInfoDAO.load(id); return item; } 不要用new ,Spring会注入的 在PetInfoDaoImpl类里加上下面代码,private PetInfoDao PetInfo = null;然后get和set方法。试一试 biz里面已经像7楼那样写了还不行吗? 这个是加在DAO里面的?我把他像7楼那样架在BIZ里面了不对的么? 嗯,是加到Dao里的,因为private PetInfoDao PetInfo = null; 依赖注入的时候会帮我们new,即使PetInfoDao PetInfo = new PetInfoDaoImpl();如果你要配置Biz的话,先配置Dao,而且配置要在Biz配置上面,然后在BizImpl类里,private PetInfoBiz petInfoBiz = null,get和set方法。XML配置应该是<bean id="petInfoBiz " class="com.yh.dao.impl.PetInfoBizImpl"> <property name="PetInfo" ref="PetInfo"></property></bean>注意 bean里的Id必须和在DaoImpl或者BizImpl里的那个属性名字一样。。 更正一下,biz的引用是在Action里,dao引用是Bizimpl里 是不是我没有把所有的配置都配完的话就会报错?我现在是只配置到了biz,然后就报错了。是不是得一直配置到action完才能运行的? 感觉我错的原来越远了还是怎么了?如果private PetInfoDao PetInfo = null;.....这一段是写在dao里面的话那查询方法还怎么写? 这个不是的,你之前的错误是你的属性名和bean的Id值不相同,所以出现空指针异常,现在如果你没有做Action的话,那么你就应该手动 去new bizImpl,而不是靠Spring的IOC帮你new。也就是 把 private PetInfoBiz petInfoBiz = null,改为 private PetInfoBiz petInfoBiz = new PetInfoBizImpl();在这里就不需要 get和set的方法了 这个已经更正了,是在bizImpl里写,你的查询方法写在DaoImpl 我把我现在的代码贴出来一下,你说的在dao里面用那个get set我不知道怎么弄。你帮我看一下吧就是先不配置biz的。Spring<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="org.hibernate.dialect.Oracle9Dialect" /> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:ora" /> <property name="username" value="system" /> <property name="password" value="sys" /></bean><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource"/> </property> <property name="mappingResources"> <list> <value>com/yh/entity/Petdiary.hbm.xml</value> <value>com/yh/entity/PetInfoId.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property></bean> <bean id="petinfo" class="com.yh.dao.impl.PetInfoDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property></bean></beans> daopackage com.yh.dao.impl;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.yh.dao.PetInfoDao;import com.yh.entity.PetInfoId;public class PetInfoDaoImpl extends HibernateDaoSupport implements PetInfoDao { private PetInfoDao petinfo=null; public PetInfoDao getPetinfo() { return petinfo; } public void setPetinfo(PetInfoDao petinfo) { this.petinfo = petinfo; } //你说的加上面这一段我不会弄。 public PetInfoId HibLoad(Integer id) { PetInfoId pet=(PetInfoId)super.getHibernateTemplate().get(PetInfoId.class, id); return pet; } }以及请帮我看一下我biz这里该怎么写的。(spring里面还没配置biz这一块来)。public PetInfoId HibLoad(Integer id) { PetInfoDao p=new PetInfoDaoImpl(); PetInfoId pet=p.HibLoad(id); return pet; }谢谢了啊。我新手。有点问题还麻烦说详细一点。 SpringXML code<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="org.hibernate.dialect.Oracle9Dialect" /> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:ora" /> <property name="username" value="system" /> <property name="password" value="sys" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource"/> </property> <property name="mappingResources"> <list> <value>com/yh/entity/Petdiary.hbm.xml</value> <value>com/yh/entity/PetInfoId.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="petinfo" class="com.yh.dao.impl.PetInfoDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="petInfoBiz " class="com.yh.dao.impl.PetInfoBizImpl"> <property name="PetInfo" ref="PetInfo"></property></bean></beans>----------------------------------------------------------------------------------------------------daoImplJava codepackage com.yh.dao.impl; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.yh.dao.PetInfoDao; import com.yh.entity.PetInfoId; public class PetInfoDaoImpl extends HibernateDaoSupport implements PetInfoDao { public PetInfoId HibLoad(Integer id) { PetInfoId pet=(PetInfoId)super.getHibernateTemplate().get(PetInfoId.class, id); return pet; } }---------------------------------------------------------------------------------------------------------bizImplJava codeprivate PetInfoDao petinfo=null; //这个属性必须和<bean id="petinfo" class="com.yh.dao.impl.PetInfoDaoImpl"> //名字一样否则,500错误 public PetInfoDao getPetinfo() { return petinfo; } public void setPetinfo(PetInfoDao petinfo) { this.petinfo = petinfo; } public PetInfoId HibLoad(Integer id) { PetInfoId pet=petinfo.HibLoad(id); return pet; }---------------------------------------------------------------------------------------如果有Action的话在Action里加上:private PetInfoBiz petInfoBiz = null,get和set方法。XML配置为:<bean name="自己起" class="对应的Action"> <property name="petInfoBiz " ref="petInfoBiz "></property></bean>注意配置Action的时候,bean用的是Name不再是ID。如果没有Action的话,在JSP或者Servlet里,只写private PetInfoBiz petInfoBiz = new PetInfoBizImpl();不需要在spring的XML配置bean。如果还是搞不定的话,我也没办法给你说明白了。 我biz都还没配置到spring里面去的啊。 那你把在spring的XML里配置的Biz配置去掉,其他的不变 今早上继续弄的时候解决了。谢谢 JavaBurning 了。结贴给你分吧,虽然分不多。 判断s:if test里面的值包含字符串 第一次查不出新消息,第2次就可以查出,这个莫个情况额? JQUERY POST()方法传参后台获取问题 javascript中的数组传递 运行jar文件时显示could not find main class !? 关于hibnate插件的问题! 求助:一个关于Apache的问题,在线等,谢谢!!! 什么样的加密算法能加密又能还原密文 关于J2EE部署,急,谢谢!300分 关于weblogic的JMS java堆栈问题 请大家帮我分析一下这个系统的技术选型。
class="com.yh.dao.impl.PetInfoDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
我这一段没有注入成功吗?
class="com.yh.dao.impl.PetInfoDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
bean id 怎么能大写呢? 你看看你在service里面 实例化是怎么写的?
那个不能够大写的么?
public PetInfoId HibLoad(Integer id) {
PetInfoDao p=new PetInfoDaoImpl();
PetInfoId pet=p.HibLoad(id);
return pet;
}
PetInfoDao p=new PetInfoDaoImpl();
return petInfoDAO;
}
public void setPetInfoDAO(PetInfoDAO petInfoDAO) {
this.petInfoDAO = petInfoDAO;
} public PetInfo load(Integer id) {
PetInfo item = this.petInfoDAO.load(id);
return item;
}
private PetInfoDao PetInfo = null;
然后get和set方法。试一试
这个是加在DAO里面的?我把他像7楼那样架在BIZ里面了不对的么?
<bean id="petInfoBiz "
class="com.yh.dao.impl.PetInfoBizImpl">
<property name="PetInfo" ref="PetInfo"></property>
</bean>
注意 bean里的Id必须和在DaoImpl或者BizImpl里的那个属性名字一样。。
是不是我没有把所有的配置都配完的话就会报错?我现在是只配置到了biz,然后就报错了。是不是得一直配置到action完才能运行的?
private PetInfoDao PetInfo = null;
.....这一段是写在dao里面的话那查询方法还怎么写?
private PetInfoBiz petInfoBiz = null,改为 private PetInfoBiz petInfoBiz = new PetInfoBizImpl();
在这里就不需要 get和set的方法了
就是先不配置biz的。Spring<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="org.hibernate.dialect.Oracle9Dialect" />
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:ora" />
<property name="username" value="system" />
<property name="password" value="sys" />
</bean><bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>com/yh/entity/Petdiary.hbm.xml</value>
<value>com/yh/entity/PetInfoId.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="petinfo"
class="com.yh.dao.impl.PetInfoDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean></beans>
daopackage com.yh.dao.impl;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.yh.dao.PetInfoDao;
import com.yh.entity.PetInfoId;public class PetInfoDaoImpl extends HibernateDaoSupport implements PetInfoDao {
private PetInfoDao petinfo=null;
public PetInfoDao getPetinfo() {
return petinfo;
} public void setPetinfo(PetInfoDao petinfo) {
this.petinfo = petinfo;
} //你说的加上面这一段我不会弄。
public PetInfoId HibLoad(Integer id) {
PetInfoId pet=(PetInfoId)super.getHibernateTemplate().get(PetInfoId.class, id);
return pet;
}
}
以及请帮我看一下我biz这里该怎么写的。(spring里面还没配置biz这一块来)。
public PetInfoId HibLoad(Integer id) {
PetInfoDao p=new PetInfoDaoImpl();
PetInfoId pet=p.HibLoad(id);
return pet;
}
谢谢了啊。我新手。有点问题还麻烦说详细一点。
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.hibernate.dialect.Oracle9Dialect" />
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:ora" />
<property name="username" value="system" />
<property name="password" value="sys" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource"/> </property>
<property name="mappingResources">
<list>
<value>com/yh/entity/Petdiary.hbm.xml</value>
<value>com/yh/entity/PetInfoId.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean>
<bean id="petinfo" class="com.yh.dao.impl.PetInfoDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="petInfoBiz "
class="com.yh.dao.impl.PetInfoBizImpl">
<property name="PetInfo" ref="PetInfo"></property>
</bean>
</beans>
----------------------------------------------------------------------------------------------------
daoImplJava codepackage com.yh.dao.impl;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.yh.dao.PetInfoDao; import com.yh.entity.PetInfoId;
public class PetInfoDaoImpl extends HibernateDaoSupport implements PetInfoDao {
public PetInfoId HibLoad(Integer id) {
PetInfoId pet=(PetInfoId)super.getHibernateTemplate().get(PetInfoId.class, id);
return pet;
}
}
---------------------------------------------------------------------------------------------------------
bizImplJava code
private PetInfoDao petinfo=null;
//这个属性必须和<bean id="petinfo" class="com.yh.dao.impl.PetInfoDaoImpl">
//名字一样否则,500错误
public PetInfoDao getPetinfo() {
return petinfo;
} public void setPetinfo(PetInfoDao petinfo) {
this.petinfo = petinfo;
} public PetInfoId HibLoad(Integer id) {
PetInfoId pet=petinfo.HibLoad(id);
return pet;
}
---------------------------------------------------------------------------------------
如果有Action的话在Action里加上:
private PetInfoBiz petInfoBiz = null,get和set方法。
XML配置为:
<bean name="自己起"
class="对应的Action">
<property name="petInfoBiz " ref="petInfoBiz "></property>
</bean>
注意配置Action的时候,bean用的是Name不再是ID。如果没有Action的话,在JSP或者Servlet里,只写
private PetInfoBiz petInfoBiz = new PetInfoBizImpl();
不需要在spring的XML配置bean。如果还是搞不定的话,我也没办法给你说明白了。
我biz都还没配置到spring里面去的啊。