Spring学习(四)

Spring学习(四)
19.Spring的AOP对Hibernate的支持:事务管理

Spring包中提供了一个切面类【HibernateTransactionManager】用于完成事务的管理,我们需要在Spring的配置文件中通过AOP配置,指定事务管理类对哪些类和哪些方法进行事务管理。如果某个数据库操作方法被Spring进行了事务管理,那么当我们在调用这个方法进行操作之前,Spring就会打开一个session并开启事务,在这个数据库操作方法中我们只需要从sessionFacory对象中获取session(无需再次打开)进行操作就可以,当操作完成以后,Spring事务管理类会自动的进行事务的提交/回滚操作。Spring事务管理的配置:需要导入aop,tx标签

<!--配置事务管理类(切面类) -->
<beanid="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"></property>
</bean>
<!--配置事务管理策略(导入tx标签) -->
<tx:adviceid="txm"transaction-manager="txManager">
<tx:attributes>
<tx:methodname="insert*"propagation="REQUIRED"/>
<tx:methodname="delete*"propagation="REQUIRED"/>
<tx:methodname="update*"propagation="REQUIRED"/>
<tx:methodname="get*"read-only="true"/>
<tx:methodname="list*"read-only="true"/>
</tx:attributes>
</tx:advice>
<!--定义连接点,将策略运用到对应的连接点上(导入aop标签) -->
<aop:config>
<aop:pointcutexpression="execution(public * com.hibernate.dao.*.*(..))"id="crud"/>
<aop:advisoradvice-ref="txm"pointcut-ref="crud"/>
</aop:config>

示例:applicationContext.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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/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">
<beanid="user"class="com.hibernate.pojos.User"/>
<!--借助于Spring容器创建SessionFactory对象 -->
<!-- classpath:表示src目录 -->
<beanid="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="configLocation"value="classpath:hibernate.cfg.xml"></property>
</bean>
<!--配置事务管理类(切面类) -->
<beanid="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"></property>
</bean>
<!--配置事务管理策略(导入tx标签) -->
<tx:adviceid="txm"transaction-manager="txManager">
<tx:attributes>
<tx:methodname="insert*"propagation="REQUIRED"/>
<tx:methodname="delete*"propagation="REQUIRED"/>
<tx:methodname="update*"propagation="REQUIRED"/>
<tx:methodname="get*"read-only="true"/>
<tx:methodname="list*"read-only="true"/>
</tx:attributes>
</tx:advice>
<!--定义连接点,将策略运用到对应的连接点上(导入aop标签) -->
<aop:config>
<aop:pointcutexpression="execution(public * com.hibernate.dao.*.*(..))"
id="crud"/>
<aop:advisoradvice-ref="txm"pointcut-ref="crud"/>
</aop:config>
<!--将依赖Spring IoC容器创建SessionFactory对象赋值给 DAO中的sessionFactory属性 -->
<beanid="udao"class="com.hibernate.dao.UserDAO">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>
</beans>

UserDAO.java

package com.hibernate.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.hibernate.pojos.User;
publicclass UserDAO {
private SessionFactory sessionFactory;
publicvoid insert(User user) {
sessionFactory.getCurrentSession().save(user);
}
publicvoid delete(User user) {
sessionFactory.getCurrentSession().delete(user);
}
publicvoid update(User user) {
sessionFactory.getCurrentSession().update(user);
}
public User get(int id) {
Session session =sessionFactory.openSession();
return (User) session.get(User.class, id);
}
public SessionFactory getSessionFactory() {
returnsessionFactory;
}
publicvoid setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}


 
20.Spring和Struts2的整合
搭建struts2和spring环境
1, 导入Struts2的jar包
struts2-core-2.x.x.jar
xwork-core-2.x.x.jar
ognl-x.x.x.jar
freemarker-x.x.x.jar
commons-logging-1.x.x.jar
commons-fileupload-1.2.x.jar
commons-io-x.x.x
commons-lang-x.x
javassist-x.x.x.GA
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值