您的位置:首页 > 编程语言 > Java开发

Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法

2017-12-29 16:49 1151 查看
XML配置文件代码如下:

<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- c3p0连接池得到dataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sw_database"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>

<bean id="accountDao" class="com.swift.AccountDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>

<bean id="accountService" class="com.swift.AccountService">
<property name="accountDao" ref="accountDao"></property>
</bean>

</beans>


事务的注解方法依然需要事务管理器DataSourceTransactionManager,这个管理器当然需要数据源DataSource来确认指向哪个数据库,即注入dataSource。

然后开启事务注解的扫描<tx:annotation-driven transaction-manager="transactionManager"/>

程序中在需要增强的类上用@Transactional标记就可以了,自动调用对该类所有方法进行增强的事务,不用再像前边的配置文件方法,自己定义

省去了下面步骤

<!--  配置事务增强 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<!-- 做事务操作 -->
<tx:attributes>
<!-- 事务操作的方法匹配规则 -->
<tx:method name="money*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>


也省去了切入点、切面的aop操作

<!-- 配置切面 -->
<aop:config>
<!-- 切入点 -->
<aop:pointcut expression="execution(* com.swift.AccountService.*(..))" id="pointcut1"/>
<!-- 切面 -->
<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
</aop:config>


 

其他的类dao类和Service类没有改变

代码如下:

package com.swift;

import org.springframework.jdbc.core.JdbcTemplate;

public class AccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

//少钱的方法
public void lessMoney(String from,double number) {
String sql="update account set money=money-? where username=?";
jdbcTemplate.update(sql, number,from);

}
//多钱的方法
public void moreMoney(String to,double number) {
String sql="update account set money=money+? where username=?";
jdbcTemplate.update(sql, number,to);
}
}


Service类

package com.swift;

import org.springframework.transaction.annotation.Transactional;

@Transactional
public class AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}

public void moneyTransfer(String from,String to,double number) {
accountDao.lessMoney(from,number);
int i=10/0;
accountDao.moreMoney(to,number);
}
}


配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法

下边是注解方法注入属性

package com.swift;

import javax.annotation.Resource;

public class Service {
@Resource(name="studentDao")
private StudentDao studentDao;
@Resource(name="courseDao")
private CourseDao courseDao;
public String fun() {
return "This is Service's fun()........."+this.studentDao.fun()+this.courseDao.fun();
}
}


aop操作复习

<bean id="book" class="com.swift.aop.Book"></bean>
<bean id="adviceBook" class="com.swift.aop.AdviceBook"></bean>

<aop:config>
<!-- 切入点表达式第一个*表示public private等权限后接空格,第二个*表示任意方法(..)表示参数 -->
<aop:pointcut expression="execution(* com.swift.aop.Book.*())" id="pointcut1"/>

<!-- 哪个切面(用来增强切入点的类) 名称是什么用ref表示 -->
<aop:aspect ref="adviceBook">
<!-- 增强的具体方法,增强哪个切入点 -->
<aop:before method="before" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>


只要调用book对象中的任意方法就可以得到增强后的效果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐