1、代理类接口Person.java

 package com.xiaostudy;

 /**
* @desc 被代理类接口
*
* @author xiaostudy
*
*/
public interface Person { public void add();
public void update();
public void delete();
}

2、代理类PersonImple.java

 package com.xiaostudy;

 import org.springframework.stereotype.Component;

 /**
* @desc 被代理类
*
* @author xiaostudy
*
*/
@Component("person")//类注解
public class PersonImple implements Person { /**
* @desc 实现接口方法
*/
public void add() {
System.out.println("add().....");
} @Override
public void update() {
System.out.println("update().....");
// int i = 1/0;
} @Override
public void delete() {
System.out.println("delete().....");
} }

3、通知类MyAspectJ.java

 package com.xiaostudy;

 import org.aspectj.lang.JoinPoint;
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; /**
* @desc 通知类
*
* @author xiaostudy
*
*/
@Component//类注解
@Aspect//AspectJ注解
public class MyAspectJ { //声明公共切入点
@Pointcut("execution(* com.xiaostudy.PersonImple.*(..))")
public void myPointcut() { } //前置通知注解,只有一个参数时,value可以省略不写
@Before("execution(* com.xiaostudy.PersonImple.*(..))")
public void myBefort(JoinPoint joinPoint) {
System.out.println("前置通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName());
} //后置通知注解,当参数大于1时,value必须写
@AfterReturning(value="myPointcut()", returning="ret")
public void myAfterReturning(JoinPoint joinPoint, Object ret) {
System.out.println("后置通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName()
+ ", ret: " + ret);
} //环绕通知注解
@Around("myPointcut()")
public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕通知====前>>>>>>>>>>>");
Object obj = joinPoint.proceed();
System.out.println("环绕通知====后<<<<<<<<<<<");
return obj;
} //异常通知注解
@AfterThrowing(value="myPointcut()", throwing="e")
public void myThrowint(JoinPoint joinPoint, Throwable e) {
System.out.println("异常通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName()
+ ", e: " + e.getMessage());
System.exit(0);
} //最终通知注解
@After("myPointcut()")
public void myAfter(JoinPoint joinPoint) {
System.out.println("最终通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName());
}
}

4、spring配置文件applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描注解类 -->
<context:component-scan base-package="com.xiaostudy"></context:component-scan>
<!-- 确定 AOP注解生效 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

5、测试类Test.java

 package com.xiaostudy;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @desc 测试类
*
* @author xiaostudy
*
*/
public class Test { public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = ac.getBean("person", Person.class);
person.add();
person.update();
person.delete();
} }

最新文章

  1. JavaScript异步编程原理
  2. U盘插入电脑后,提示需要格式化U盘如何解决?
  3. 发生tcp丢包(拥堵、超时)重传
  4. mysql workbench
  5. windbg
  6. windows环境下局域网内无法访问apache站点
  7. 【转】Java魔法堂:String.format详解
  8. (object sender,EventArgs e)是什么?
  9. 【风马一族_windom】 批量修改相同文件类型的后缀
  10. Android开发:向下一个activity传递数据,返回数据给上一个activity
  11. Servlet 过滤器
  12. jQuery源码分析1
  13. 杭州电 1203 I NEED A OFFER!
  14. 新认识:SDF数据库
  15. 201521123117 《Java程序设计》第14周学习总结
  16. 多服务器操作利器 - Polysh
  17. W班-项目选题报告成绩
  18. sql server managerment 给表加说明
  19. python模块的导入的两种方式区别详解
  20. python 操作数据库

热门文章

  1. 【BZOJ4543】[POI2014]Hotel加强版 长链剖分+DP
  2. Leetcode-Permuation Sequence
  3. Powershell Get Domain Mailbox的几种方法
  4. python 三行代码实现快速排序
  5. Windows数据库定时备份
  6. Vue(2)- v-model、局部组件和全局组件、父子组件传值、平行组件传值
  7. oracle 禁用索引
  8. Angular学习笔记—RxJS与Observable(转载)
  9. VGA显示
  10. Djngo Rest Framework