@aspect类
/**
* @Date 2019/9/9 2:27
* by mocar
*/
@Component
@Aspect
public class TransactionManager {
@Autowired
private ConnectionUtils connectionUtils;
@Pointcut("execution(* com.itcast.service.impl.*.*(..))")
private void pt1(){}
@Before("pt1()")
public void beginTransaction(){
try {
connectionUtils.getThreadConnection().setAutoCommit(false);//开启事务
} catch (SQLException e) {
e.printStackTrace();
}
}
@AfterReturning("pt1()")
public void commit(){
try {
connectionUtils.getThreadConnection().commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
@AfterThrowing()
public void rollback(){
try {
connectionUtils.getThreadConnection().rollback();
} catch (Exception e) {
e.printStackTrace();
}
}
@After("pt1()")
public void release(){
try {
//从dataSource获取的connection释放,但是线程中还绑定着这个无效connection连接,也需要释放线程绑定的连接
connectionUtils.getThreadConnection().close();
connectionUtils.removeThreadConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
//@Around("pt1()")
public Object around(ProceedingJoinPoint pjp){
Object rsValues=null;
Object[] args = pjp.getArgs();
try {
beginTransaction();
rsValues=pjp.proceed(args);
commit();
} catch (Throwable e) {
//throwable.printStackTrace();
rollback();
throw new RuntimeException(e);
}finally {
release();
}
return rsValues;
}
}
测试:
spring整合junit
/**
* @Date 2019/9/8 3:34
* by mocar
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:bean.xml")
public class AccountServiceTest {
@Autowired
private AccountService as=null;
@Test
public void testTarget(){
as.transfer("aaa","bbb",100f);
}
}
报错信息:
Caused by:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountDao' defined in file [D:\idea_project\spring\springaoptx_aonnotation\target\classes\com\itcast\dao\impl\AccountDaoImpl.class]: BeanPostProcessor before instantiation of bean failed; nested exception is
java.lang.IllegalArgumentException: @org.aspectj.lang.annotation.AfterThrowing(throwing=, pointcut=, value=, argNames=) is not a valid AspectJ annotation
报的:is not a valid AspectJ annotation
查看了配置,没有问题啊。。。
只知道这四个注解执行顺序有问题
然后决定试试环绕通知手动编码生成动态代理
/**
* @Date 2019/9/9 2:27
* by mocar
*/
@Component
@Aspect
public class TransactionManager {
@Autowired
private ConnectionUtils connectionUtils;
@Pointcut("execution(* com.itcast.service.impl.*.*(..))")
private void pt1(){}
//@Before("pt1()")
public void beginTransaction(){
try {
connectionUtils.getThreadConnection().setAutoCommit(false);//开启事务
} catch (SQLException e) {
e.printStackTrace();
}
}
//@AfterReturning("pt1()")
public void commit(){
try {
connectionUtils.getThreadConnection().commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
//@AfterThrowing()
public void rollback(){
try {
connectionUtils.getThreadConnection().rollback();
} catch (Exception e) {
e.printStackTrace();
}
}
//@After("pt1()")
public void release(){
try {
//从dataSource获取的connection释放,但是线程中还绑定着这个无效connection连接,也需要释放线程绑定的连接
connectionUtils.getThreadConnection().close();
connectionUtils.removeThreadConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Around("pt1()")
public Object around(ProceedingJoinPoint pjp){
Object rsValues=null;
Object[] args = pjp.getArgs();
try {
beginTransaction();
rsValues=pjp.proceed(args);
commit();
} catch (Throwable e) {
//throwable.printStackTrace();
rollback();
throw new RuntimeException(e);
}finally {
release();
}
return rsValues;
}
}
居然通过了!但是不知道 @AfterThrowing我哪里配置有问题