package com.xk.spring.kp04_aop.aop.s02_annotation;

public interface IStudentService {
public void save(Student stu);
public void update(Student stu);
}
package com.xk.spring.kp04_aop.aop.s02_annotation;

import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements IStudentService {
@Override
public void save(Student stu) {
System.out.println("调用Dao的save方法....");
}
@Override
public void update(Student stu) {
System.out.println("调用Dao的update方法...");
/*@SuppressWarnings("unused")
int i = 10/0;*/
}
}
package com.xk.spring.kp04_aop.aop.s02_annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; //自定义切面
@Aspect
@Component //一般写在工具类上面
public class TransactionManager {
@Pointcut("execution(* com.xk.spring.kp04_aop.aop.s02_annotation.*Service.*(..))")
public void stuService(){}//此方法名为execution表达式的别名,即id="";
/**
* 事物开始
*/
@Before("stuService()")//
public void begin(){
System.out.println("TransactionManager.begin()");
}
/**
* 事物提交
*/
@AfterReturning("stuService()")
public void commit(){
System.out.println("TransactionManager.commit()");
}
/**
* 事物回滚
*/
@AfterThrowing(value = "stuService()",throwing = "e")
public void rollback(Throwable e){
System.out.println("TransactionManager.rollback()");
System.out.println("rollback()");
} /**
* 事物结束
*/
@After("stuService()")
public void finished(){
System.out.println("TransactionManager.close()");
}
@Around("stuService()")
public void all(ProceedingJoinPoint point){
try {
begin();
point.proceed();//此处有连接,必须写,不然执行到此将不再向下执行
commit();
} catch (Throwable e) {
rollback(e);
}finally{
finished();
} }
}
package com.xk.spring.kp04_aop.aop.s02_annotation;

public class Student {
private String name;
private Integer age; public Student() { } public Student(String name, Integer age) {
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} @Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!-- 自动生成代理 -->
<aop:aspectj-autoproxy />
<!-- 指定IOC扫描的包 -->
<context:component-scan
base-package="com.xk.spring.kp04_aop.aop.s02_annotation"/>
</beans>
package com.xk.spring.kp04_aop.aop.s02_annotation;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class AOPAnnoationTest {
@Autowired
IStudentService seriver; @Test
public void testAOPXML() throws Exception {
seriver.save(new Student("张三", 18));
seriver.update(new Student("张4", 18));
}
}

最新文章

  1. 理解java虚拟机内存分配堆,栈和方法区
  2. C# 该行已经属于另一个表
  3. 3.1 ARM汇编编程概述
  4. Async Console Programs 异步控制台程序
  5. java加密解密的学习
  6. 图片和Base64之间的转换
  7. nodejs 常用全局包
  8. TaffyDB:开源JavaScript数据库
  9. OC基础(2)
  10. 省市区(县)三级联动代码(js 数据源)
  11. 算法优化(动态规划):COGS 2009. [USACO Mar09]餐厅清扫
  12. break 与continue的区别
  13. ThinkPHP - 登录流程
  14. 浅谈JavaScript递归
  15. php 两个数组,若键相同,则值合并
  16. Day08 - Ruby比一比:String的+=与concat串接
  17. ul无点标签左移
  18. WebSocket和long poll、ajax轮询的区别,ws协议测试
  19. Oracle使用——Oracle表空间处理
  20. C++指针二(易错模型)

热门文章

  1. nRF52832-PPI部分学习
  2. cocos2dx spine之一 :spine缓存 (c++ &amp; lua)
  3. ado.net常用操作
  4. LeetCode--004--寻找两个有序数组的中位数(java)
  5. 20180518VSTO多簿单表汇总外接程序按钮
  6. kernel_thread简析
  7. 函数和函数模版在一个。cpp中的情况!(除了左移和右移,其他的不要用友元函数!!!)
  8. UI基础三:简单的BOL报表开发
  9. Logstash 基础入门
  10. Hadoop介绍-4.Hadoop中NameNode、DataNode、Secondary、NameNode、JobTracker TaskTracker