最近做了个ssh项目遇到一个奇怪问题:在strtus的action里用注入进去的service获取的getHibernateTemplate()始终为null,而service本身方法里一切正常。
load方法如下:
applicationContext.xml配置文件
貌似getHibernateTemplate只能在service类里用,此类继承HibernateDaoSupport。
//au可以正常获取
//AdmUser au = (AdmUser) yxth.load(AdmUser.class, new Long(1000));
try {//不知道为啥是null?
if (yxth.getHibernateTemplate() == null) {
System.out.println("46777");
}
;
} catch (Exception rt) {
}
load方法如下:
public AdmUser load(Class<AdmUser> class1, Long s) {
log.debug("load " + DaoUtil.getEntityName(class1)
+ " instance with id: " + s);
try {
return this.getHibernateTemplate().get(class1, s);
} catch (ObjectNotFoundException ex) {
log.error("Not such Object with id:" + s, ex);
return null;
} catch (RuntimeException re) {
log.error("load failed!", re);
throw re;
}
}
applicationContext.xml配置文件
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="yxth36actionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- <bean id="fuchanSF"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation" value="classpath:yxfuchan.cfg.xml">
</property>
</bean>-->
<!-- <bean id="fuchanactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="fuchanSF" />
</property>
</bean>-->
<tx:advice id="yxth36TxAdvice" transaction-manager="yxth36actionManager">
<tx:attributes>
<tx:method name="find*" read-only="true" />
<tx:method name="get*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
<!--
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"
/>
-->
</tx:attributes>
</tx:advice>
<!-- <tx:advice id="fuchanTxAdvice" transaction-manager="fuchanactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true" />
<tx:method name="get*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>-->
<!-- <bean id="serviceAutoProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="proxyTargetClass" value="true" />
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>yxth36TxAdvice</value>
<value>fuchanTxAdvice</value>
</list>
</property>
</bean>-->
<aop:config proxy-target-class="true">
<aop:pointcut id="yxth36ServiceOperation" expression="execution(* uniora.thorp.service.*.*(..))" />
<!--
<aop:pointcut id="fuchanV6ServiceOperation" expression="execution(*
uniora.thorp.fuchan.*.*(..))" />
-->
<aop:advisor advice-ref="yxth36TxAdvice" pointcut-ref="yxth36ServiceOperation" />
<!--
<aop:advisor advice-ref="fuchanTxAdvice"
pointcut-ref="fuchanV6ServiceOperation" />
-->
</aop:config>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!--
<bean id="hibernateTemplate2"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory"> <ref local="fuchanSF" /> </property>
</bean>
-->
<bean id="yxth36Service" class="uniora.thorp.service.Yxth36Service">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
<!--
<bean id="fuchanV6Service" class="uniora.thorp.db.FuchanV6Service">
<property name="hibernateTemplate" ref="hibernateTemplate2" /> </bean>
-->
貌似getHibernateTemplate只能在service类里用,此类继承HibernateDaoSupport。