直接上配置吧
事物级的spring配置:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 事务配置 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<!-- 配置哪些方法将要被切入 -->
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 配置切入点:指哪些包下的类将要被切面所切入 第一个星号:任意返回值 第二个星号:dao包下面的子子包 第三个星号:任意类-->
<aop:pointcut id="allmethod" expression="execution(* com.csyingwu.dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allmethod" />
</aop:config>
dao层spring配置,此处只贴出了一个dao的配置,因为做测试的也是这个Dao:
<bean id="userDao" class="dao.UserDAO">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
下面是java代码(测试用):
public class UserDAO extends HibernateDaoSupport{
public static void main(String[] d){
HibernateTemplate a = this.getHibernateTemplate();
System.out.println(a);
SessionFactory b = a.getSessionFactory();
System.out.println(b);
Session c = this.getSession();
System.out.println(c);
}
}
运行结果:
这里我就不贴图了,报错为空指针,a的值取出来就为null, 不知道是哪个环节出了错
最后我再把web.xml的代码贴出来:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!-- spring与structs 整合的配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- structs2配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 用户注册 -->
<servlet>
<servlet-name>RegisterAction</servlet-name>
<servlet-class>action.RegisterAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterAction</servlet-name>
<url-pattern>/register.action</url-pattern>
</servlet-mapping>
<!--用户登陆 -->
<servlet>
<servlet-name>LoginAction</servlet-name>
<servlet-class>action.RegisterAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginAction</servlet-name>
<url-pattern>/login.action</url-pattern>
</servlet-mapping>
<!-- 中文乱码过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
------解决方案--------------------
dao那里注入的应该是sessionfactory吧,不是hibernateTimplete,楼主测下
------解决方案--------------------
可以试一下,1楼说的有一定道理。你在dao层中注入了hibernatetemplete,但是没有注入sessionFactory,尽管hibernateTemplete中有sessionFactory。试下单独注入sessionFactory。
------解决方案--------------------