012. Spring Aspect Oriented Programming(AOP)
2017-06-23 11:47
309 查看
1、创建Java项目:File -> New -> Java Project
2、引入必要jar包,项目结构如下
3、创建StudentService接口StudentService.java
4、创建StudentServiceImp实现类StudentServiceImp.java
5、创建StudentServiceAspect切面类StudentServiceAspect.java
6、创建spring配置文件applicationContext.xml
7、创建Spring测试类SpringUnit.java
8、测试结果
2、引入必要jar包,项目结构如下
3、创建StudentService接口StudentService.java
package com.spring.service; public interface StudentService { public void saveStudent(String student); }
4、创建StudentServiceImp实现类StudentServiceImp.java
package com.spring.service.imp; import com.spring.service.StudentService; public class StudentServiceImp implements StudentService { @Override public void saveStudent(String student) { System.out.println("invoke saveStudent(String student) Save student: " + student); // System.out.println(1/0); // 测试异常通知 } }
5、创建StudentServiceAspect切面类StudentServiceAspect.java
package com.spring.advice; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; public class StudentServiceAspect { public void doBefore(JoinPoint jp ) { System.out.println("前置通知开始"); System.out.println("Class name: " + jp.getTarget().getClass().getName()); System.out.println("Function name: " + jp.getSignature().getName()); System.out.println("Save student begin: " + jp.getArgs()[0]); System.out.println("前置通知结束"); } public void doAfter(JoinPoint jp) { System.out.println("后置通知开始"); System.out.println("Class name: " + jp.getTarget().getClass().getName()); System.out.println("Function name: " + jp.getSignature().getName()); System.out.println("Save student end: " + jp.getArgs()[0]); System.out.println("后置通知结束"); } public Object doAround(ProceedingJoinPoint pjp) { System.out.println("环绕通知开始"); Object retVal = null; // 函数返回值 try { retVal = pjp.proceed(); // 调用函数 } catch (Throwable e) { e.printStackTrace(); } System.out.println("函数执行结果: " + retVal); System.out.println("环绕通知结束"); return retVal; } public void doAfterReturning(JoinPoint jp) { System.out.println("返回通知"); } public void doAfterThrowing(JoinPoint jp, Throwable e){ System.out.println("异常通知: " + e.getMessage()); } }
6、创建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" 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"> <bean id="studentServiceAspect" class="com.spring.advice.StudentServiceAspect"></bean> <bean id="studentService" class="com.spring.service.imp.StudentServiceImp"></bean> <aop:config> <aop:aspect id="studentServiceAspect" ref="studentServiceAspect"> <aop:pointcut expression="execution(* com.spring.service.*.*(..))" id="advice" /> <aop:before method="doBefore" pointcut-ref="advice"/> <aop:after method="doAfter" pointcut-ref="advice"/> <aop:around method="doAround" pointcut-ref="advice"/> <aop:after-returning method="doAfterReturning" pointcut-ref="advice"/> <aop:after-throwing method="doAfterThrowing" pointcut-ref="advice" throwing="e"/> </aop:aspect> </aop:config> </beans>
7、创建Spring测试类SpringUnit.java
package com.spring.junit; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.service.StudentService; public class SpringUnit { @Test public void test() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); StudentService studentService = (StudentService) ctx.getBean("studentService"); studentService.saveStudent("吴腾辉"); ctx.close(); } }
8、测试结果
... 省略Spring日志信息 ... 前置通知开始 Class name: com.spring.service.imp.StudentServiceImp Function name: saveStudent Save student begin: 吴腾辉 前置通知结束 环绕通知开始 invoke saveStudent(String student) Save student: 吴腾辉 返回通知 函数执行结果: null 环绕通知结束 后置通知开始 Class name: com.spring.service.imp.StudentServiceImp Function name: saveStudent Save student end: 吴腾辉 后置通知结束 ... 省略Spring日志信息 ...
相关文章推荐
- Aspect Oriented Programming - AOP
- AOP--Aspect Oriented Programming
- AOP(Aspect Oriented Programming),面向切面编程
- AOP(Aspect Oriented Programming)原理
- Spring AOP(aspect oriented programming) 转载
- AOP(Aspect Oriented Programming) 面向方面编程
- aop(Aspect Oriented Programming)面向切面编程
- AOP Aspect Oriented Programming
- AOP(Aspect Oriented Programming)面向切面编程及应用实例
- Spring(5)AOP-Aspect Oriented Programming
- Spring AOP(Aspect Oriented Programming) Tutorials
- Aspect Oriented Programming(AOP)面向切面编程
- AOP(Aspect Oriented Programming,面向方面编程)
- AOP为Aspect Oriented Programming的缩写,意为:面向切面编程
- AOP, aspect oriented programming
- 什么是Aspect Oriented Programming(AOP)面向切面编程?
- Java 面向切面编程(Aspect Oriented Programming,AOP)
- AOP: Aspect-Oriented Programming Enables Better Code Encapsulation and Reuse(Microsoft MSDN)
- Aspect Oriented Programming (AOP)
- AOP(Aspect Oriented Programming)是什么?