Spring -- spring结合aop 进行 tx&aspectj事务管理配置方法
2024-10-20 15:44:50
1. tx 配置方法, 代码示例
javabean及其映射文件省略,和上篇的一样
CustomerDao.java, dao层接口
public interface CustomerDao {
public void insertCustomer(Customer c);
public void updateCustomer(Customer c);
public List<Customer> findCustomerByName(String name); //批量保存
//public void saveCustomers(List<Customer> list);
}
CustomerDaoImpl.java ,dao层实现
public class CustomerDaoImpl implements CustomerDao { private SessionFactory sf ;
public void setSf(SessionFactory sf) {
this.sf = sf;
} public List<Customer> findCustomerByName(String name) {
String hql = "from Customer c where c.name = ?";
Query q = sf.getCurrentSession().createQuery(hql);
q.setString(0, name);
return q.list();
} public void insertCustomer(Customer c) {
sf.getCurrentSession().save(c);
} public void updateCustomer(Customer c) {
sf.getCurrentSession().update(c);
}
}
CustomerService.java ,service层接口
public interface CustomerService {
public void insertCustomer(Customer c);
public void updateCustomer(Customer c);
public List<Customer> findCustomerByName(String name); //批量保存
public void saveCustomers(List<Customer> list);
}
CustomerServiceImpl.java,service层实现
public class CustomerServiceImpl implements CustomerService {
//dao
private CustomerDao dao ; //注入dao
public void setDao(CustomerDao dao) {
this.dao = dao;
} public List<Customer> findCustomerByName(String name) {
return dao.findCustomerByName(name);
} public void insertCustomer(Customer c) {
dao.insertCustomer(c);
} /**
* 批量保存, 编程式事务管理
*/
public void saveCustomers(final List<Customer> list) {
for(Customer c : list){
this.insertCustomer(c);
}
} public void updateCustomer(Customer c) {
dao.updateCustomer(c);
}
}
jdbc.properties
jdbc.driverclass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=root c3p0.pool.size.max=10
c3p0.pool.size.min=2
c3p0.pool.size.ini=3
c3p0.pool.size.increment=2 hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=none
sh.xml,spring配置文件
<?xml version="1.0"?>
<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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<!-- 指定分散配置的文件的位置 -->
<context:property-placeholder location="classpath:cn/itcast/spring/hibernate/tx25/jdbc.properties" />
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverclass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" /> <property name="maxPoolSize" value="${c3p0.pool.size.max}" />
<property name="minPoolSize" value="${c3p0.pool.size.min}" />
<property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
<property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
</bean> <!-- 本地回话工厂bean,spring整合hibernate的核心入口 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 指定hibernate自身的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:cn/itcast/spring/domain</value>
</list>
</property>
</bean> <!-- customerDao -->
<bean id="customerDao" class="cn.itcast.spring.hibernate.tx25.CustomerDaoImpl">
<property name="sf" ref="sessionFactory" />
</bean> <!-- hibernate事务管理器,在service层面上实现事务管理,而且达到平台无关性 -->
<bean id="htm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- customerService -->
<bean id="customerService" class="cn.itcast.spring.hibernate.tx25.CustomerServiceImpl">
<property name="dao" ref="customerDao" />
</bean> <!-- 事务通知 -->
<tx:advice id="txAdvice" transaction-manager="htm">
<!-- 事务属性 -->
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="batch*" propagation="REQUIRED" isolation="DEFAULT"/> <tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/> <tx:method name="*" propagation="SUPPORTS" isolation="DEFAULT" read-only="false"/>
</tx:attributes>
</tx:advice> <!-- aop配置(配置切入点) -->
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* *..*Service.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>
</beans>
App.java 测试代码
public class App { public static void main(String[] args) throws SQLException {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"cn/itcast/spring/hibernate/tx25/sh.xml");
CustomerService cs = (CustomerService) ac.getBean("customerService");
List<Customer> list = new ArrayList<Customer>();
Customer c = null ;
for(int i = 0 ; i < 10 ; i++){
c = new Customer();
if(i == 9){
c.setName(null);
}
else{
c.setName("tom" + i);
}
c.setAge(20 + i);
list.add(c);
}
cs.saveCustomers(list);
} }
2. aspectj 配置方法 代码示例
和(1)tx方式相比只有两个文件有变动
CustomerServiceImpl.java, service实现 以加注解的方式
/**
* 通过注解方法实现事务管理
*/
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)
public class CustomerServiceImpl implements CustomerService {
//dao
private CustomerDao dao ; //注入dao
public void setDao(CustomerDao dao) {
this.dao = dao;
} /**
* 只读
*/
@Transactional(readOnly=true)
public List<Customer> findCustomerByName(String name) {
return dao.findCustomerByName(name);
} public void insertCustomer(Customer c) {
dao.insertCustomer(c);
} /**
* 批量保存, 编程式事务管理
*/
public void saveCustomers(final List<Customer> list) {
for(Customer c : list){
this.insertCustomer(c);
}
} public void updateCustomer(Customer c) {
dao.updateCustomer(c);
}
}
sh.xml 配置文件
<?xml version="1.0"?>
<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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<!-- 指定分散配置的文件的位置 -->
<context:property-placeholder location="classpath:cn/itcast/spring/hibernate/tx25/jdbc.properties" />
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverclass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" /> <property name="maxPoolSize" value="${c3p0.pool.size.max}" />
<property name="minPoolSize" value="${c3p0.pool.size.min}" />
<property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
<property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
</bean> <!-- 本地回话工厂bean,spring整合hibernate的核心入口 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 指定hibernate自身的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:cn/itcast/spring/domain</value>
</list>
</property>
</bean> <!-- customerDao -->
<bean id="customerDao" class="cn.itcast.spring.hibernate.aspectj.CustomerDaoImpl">
<property name="sf" ref="sessionFactory" />
</bean> <!-- hibernate事务管理器,在service层面上实现事务管理,而且达到平台无关性 -->
<bean id="htm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- customerService -->
<bean id="customerService" class="cn.itcast.spring.hibernate.aspectj.CustomerServiceImpl">
<property name="dao" ref="customerDao" />
</bean> <!-- 使用注解驱动 -->
<tx:annotation-driven transaction-manager="htm"/>
</beans>
最新文章
- 基于Ruby的watir-webdriver自动化测试方案与实施(五)
- .net下开发ActiveX控件
- 【原】灵活运用sessionStorage或者localStorage
- MVC-列表页操作按钮调用脚本
- c语言中继承和多态的简单实现
- ZigBee物理层协议规范
- 网页在Safari快速滚动和回弹的原理: -webkit-overflow-scrolling : touch;的实现
- 如何在Visio 2007中画接口和实现类的关系图
- PHP 静态缓存
- spring2.0 mybatis JDBC配置
- 使用ILSpy软件反编译.Net应用程序的方法及注意事项
- gym101808 E
- 查看Linux内置命令和外部命令
- GIAC深圳站 | 2018年不可错过的全球互联网架构大会!
- ora-12899解决方法
- [Winform]Media Player组件全屏播放的设置
- Spring boot下添加filter
- linux centos7最小化安装桥接模式网络设置、xshell、xftf
- Cobbler自动化安装部署系统
- java 继承 String类
热门文章
- eslint Rules
- 170306、wamp中的Apache开启gzip压缩提高网站的响应速度
- Powershell About File System
- 实践中需要了解的cpu特性
- MyBatis generator 生成生成dao model mappper
- numpy.random.random &; numpy.ndarray.astype &; numpy.arange
- eclipse或Myeclipse中web项目没有run on server时怎么办?
- yum速查
- Python(迭代、三元表达式、列表生成、生成器、迭代器)
- 面向对象封装 classmethod和staticmethod方法