第一节:AOP 简介

AOP 简介:百度百科;

面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是Spring框架中的一个重要内容。利用AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

主要的功能是:日志记录,性能统计,安全控制,事务处理,异常处理等等。

--------

这里我先开头讲一个例子代码程序:

T.java:

 package com.wishwzp.test;

 import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.service.StudentService; public class T { private ApplicationContext ac; @Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
} @Test
public void test1() {
StudentService studentService=(StudentService)ac.getBean("studentService");
studentService.addStudent("张三");
} }

beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="studentService" class="com.wishwzp.service.impl.StudentServiceImpl"></bean> </beans>

StudentServiceImpl.java:

 package com.wishwzp.service.impl;

 import com.wishwzp.service.StudentService;

 public class StudentServiceImpl implements StudentService{

     @Override
public void addStudent(String name) {
System.out.println("开始添加学生"+name);
System.out.println("添加学生"+name);
System.out.println("完成学生"+name+"的添加");
} }

StudentService.java:

 package com.wishwzp.service;

 public interface StudentService {

     public void addStudent(String name);
}

运行结果显示:

开始添加学生张三
添加学生张三
完成学生张三的添加

--------------

第二节:Spring AOP 实例

1,前置通知;

2,后置通知;

3,环绕通知;

4,返回通知;

5,异常通知;

1,前置通知;

T.java:

 package com.wishwzp.test;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.service.StudentService; public class T { public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
StudentService studentService=(StudentService)ac.getBean("studentService");
studentService.addStudent("张三"); }
}

beans.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,StudentServiceAspect -->
<bean id="studentServiceAspect" class="com.wishwzp.advice.StudentServiceAspect"></bean> <!-- 注入bean,StudentServiceImpl -->
<bean id="studentService" class="com.wishwzp.service.impl.StudentServiceImpl"></bean> <!-- 配置AOP -->
<aop:config>
<aop:aspect id="studentServiceAspect" ref="studentServiceAspect">
<aop:pointcut expression="execution(* com.wishwzp.service.*.*(..))" id="businessService"/>
<aop:before method="doBefore" pointcut-ref="businessService"/>
</aop:aspect>
</aop:config>
</beans>

StudentServiceAspect.java:

 package com.wishwzp.advice;

 import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; public class StudentServiceAspect { public void doBefore(JoinPoint jp){
System.out.println("类名:"+jp.getTarget().getClass().getName());
System.out.println("方法名:"+jp.getSignature().getName());
System.out.println("开始添加学生:"+jp.getArgs()[0]);
} }

StudentServiceImpl.java:

 package com.wishwzp.service.impl;

 import com.wishwzp.service.StudentService;

 public class StudentServiceImpl implements StudentService{

     @Override
public void addStudent(String name) {
// System.out.println("开始添加学生"+name);
System.out.println("添加学生"+name);
// System.out.println("完成学生"+name+"的添加");
} }

StudentService.java:

 package com.wishwzp.service;

 public interface StudentService {

     public void addStudent(String name);
}

运行结果显示:

类名:com.wishwzp.service.impl.StudentServiceImpl
方法名:addStudent
开始添加学生:张三
添加学生张三

2,后置通知;

T.java:

 package com.wishwzp.test;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.service.StudentService; public class T { public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
StudentService studentService=(StudentService)ac.getBean("studentService");
studentService.addStudent("张三"); }
}

beans.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,StudentServiceAspect -->
<bean id="studentServiceAspect" class="com.wishwzp.advice.StudentServiceAspect"></bean> <!-- 注入bean,StudentServiceImpl -->
<bean id="studentService" class="com.wishwzp.service.impl.StudentServiceImpl"></bean> <!-- 配置AOP -->
<aop:config>
<aop:aspect id="studentServiceAspect" ref="studentServiceAspect">
<!-- 配置执行切点路径和id -->
<aop:pointcut expression="execution(* com.wishwzp.service.*.*(..))" id="businessService"/>
<!-- 前置通知 -->
<aop:before method="doBefore" pointcut-ref="businessService"/>
<!-- 后置通知 -->
<aop:after method="doAfter" pointcut-ref="businessService"/>
</aop:aspect>
</aop:config>
</beans>

StudentServiceAspect.java:

 package com.wishwzp.advice;

 import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; public class StudentServiceAspect { //前置通知
public void doBefore(JoinPoint jp){
System.out.println("类名:"+jp.getTarget().getClass().getName());
System.out.println("方法名:"+jp.getSignature().getName());
System.out.println("开始添加学生:"+jp.getArgs()[0]);
} //后置通知
public void doAfter(JoinPoint jp){
System.out.println("类名:"+jp.getTarget().getClass().getName());
System.out.println("方法名:"+jp.getSignature().getName());
System.out.println("学生添加完成:"+jp.getArgs()[0]);
}
}

StudentServiceImpl.java:

 package com.wishwzp.service.impl;

 import com.wishwzp.service.StudentService;

 public class StudentServiceImpl implements StudentService{

     @Override
public void addStudent(String name) {
// System.out.println("开始添加学生"+name);
System.out.println("添加学生"+name);
// System.out.println("完成学生"+name+"的添加");
} }

StudentService.java:

 package com.wishwzp.service;

 public interface StudentService {

     public void addStudent(String name);
}

运行结果显示:

类名:com.wishwzp.service.impl.StudentServiceImpl
方法名:addStudent
开始添加学生:张三
添加学生张三
类名:com.wishwzp.service.impl.StudentServiceImpl
方法名:addStudent
学生添加完成:张三

3,环绕通知;

T.java:

 package com.wishwzp.test;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.service.StudentService; public class T { public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
StudentService studentService=(StudentService)ac.getBean("studentService");
studentService.addStudent("张三"); }
}

beans.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,StudentServiceAspect -->
<bean id="studentServiceAspect" class="com.wishwzp.advice.StudentServiceAspect"></bean> <!-- 注入bean,StudentServiceImpl -->
<bean id="studentService" class="com.wishwzp.service.impl.StudentServiceImpl"></bean> <!-- 配置AOP -->
<aop:config>
<aop:aspect id="studentServiceAspect" ref="studentServiceAspect">
<!-- 配置执行切点路径和id -->
<aop:pointcut expression="execution(* com.wishwzp.service.*.*(..))" id="businessService"/>
<!-- 前置通知 -->
<aop:before method="doBefore" pointcut-ref="businessService"/>
<!-- 后置通知 -->
<aop:after method="doAfter" pointcut-ref="businessService"/>
<!-- 环绕通知 -->
<aop:around method="doAround" pointcut-ref="businessService"/>
</aop:aspect>
</aop:config>
</beans>

StudentServiceAspect.java:

 package com.wishwzp.advice;

 import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; public class StudentServiceAspect { //前置通知
public void doBefore(JoinPoint jp){
System.out.println("类名:"+jp.getTarget().getClass().getName());
System.out.println("方法名:"+jp.getSignature().getName());
System.out.println("开始添加学生:"+jp.getArgs()[0]);
} //后置通知
public void doAfter(JoinPoint jp){
System.out.println("类名:"+jp.getTarget().getClass().getName());
System.out.println("方法名:"+jp.getSignature().getName());
System.out.println("学生添加完成:"+jp.getArgs()[0]);
} //环绕通知,这里添加了返回值
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("添加学生前");
Object retVal=pjp.proceed();
System.out.println(retVal);
System.out.println("添加学生后");
return retVal;
}
}

StudentServiceImpl.java:

 package com.wishwzp.service.impl;

 import com.wishwzp.service.StudentService;

 public class StudentServiceImpl implements StudentService{

     @Override
public String addStudent(String name) {
// System.out.println("开始添加学生"+name);
System.out.println("添加学生"+name);
// System.out.println("完成学生"+name+"的添加");
return name;
} }

StudentService.java:

 package com.wishwzp.service;

 public interface StudentService {

     public String addStudent(String name);
}

运行结果显示:

类名:com.wishwzp.service.impl.StudentServiceImpl
方法名:addStudent
开始添加学生:张三
添加学生前
添加学生张三
张三
添加学生后
类名:com.wishwzp.service.impl.StudentServiceImpl
方法名:addStudent
学生添加完成:张三

4,返回通知;

T.java:

 package com.wishwzp.test;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.service.StudentService; public class T { public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
StudentService studentService=(StudentService)ac.getBean("studentService");
studentService.addStudent("张三"); }
}

beans.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,StudentServiceAspect -->
<bean id="studentServiceAspect" class="com.wishwzp.advice.StudentServiceAspect"></bean> <!-- 注入bean,StudentServiceImpl -->
<bean id="studentService" class="com.wishwzp.service.impl.StudentServiceImpl"></bean> <!-- 配置AOP -->
<aop:config>
<aop:aspect id="studentServiceAspect" ref="studentServiceAspect">
<!-- 配置执行切点路径和id -->
<aop:pointcut expression="execution(* com.wishwzp.service.*.*(..))" id="businessService"/>
<!-- 前置通知 -->
<aop:before method="doBefore" pointcut-ref="businessService"/>
<!-- 后置通知 -->
<aop:after method="doAfter" pointcut-ref="businessService"/>
<!-- 环绕通知 -->
<aop:around method="doAround" pointcut-ref="businessService"/>
<!-- 返回通知 -->
<aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/>
</aop:aspect>
</aop:config>
</beans>

StudentServiceAspect.java:

 package com.wishwzp.advice;

 import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; public class StudentServiceAspect { //前置通知
public void doBefore(JoinPoint jp){
System.out.println("类名:"+jp.getTarget().getClass().getName());
System.out.println("方法名:"+jp.getSignature().getName());
System.out.println("开始添加学生:"+jp.getArgs()[0]);
} //后置通知
public void doAfter(JoinPoint jp){
System.out.println("类名:"+jp.getTarget().getClass().getName());
System.out.println("方法名:"+jp.getSignature().getName());
System.out.println("学生添加完成:"+jp.getArgs()[0]);
} //环绕通知,这里添加了返回值
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("添加学生前");
Object retVal=pjp.proceed();
System.out.println(retVal);
System.out.println("添加学生后");
return retVal;
} //返回通知
public void doAfterReturning(JoinPoint jp){
System.out.println("返回通知");
}
}

StudentServiceImpl.java:

 package com.wishwzp.service.impl;

 import com.wishwzp.service.StudentService;

 public class StudentServiceImpl implements StudentService{

     @Override
public String addStudent(String name) {
// System.out.println("开始添加学生"+name);
System.out.println("添加学生"+name);
// System.out.println("完成学生"+name+"的添加");
return name;
} }

StudentService.java:

 package com.wishwzp.service;

 public interface StudentService {

     public String addStudent(String name);
}

运行结果显示:

类名:com.wishwzp.service.impl.StudentServiceImpl
方法名:addStudent
开始添加学生:张三
添加学生前
添加学生张三
返回通知
张三
添加学生后
类名:com.wishwzp.service.impl.StudentServiceImpl
方法名:addStudent
学生添加完成:张三

5,异常通知;

T.java:

 package com.wishwzp.test;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wishwzp.service.StudentService; public class T { public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
StudentService studentService=(StudentService)ac.getBean("studentService");
studentService.addStudent("张三"); }
}

beans.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,StudentServiceAspect -->
<bean id="studentServiceAspect" class="com.wishwzp.advice.StudentServiceAspect"></bean> <!-- 注入bean,StudentServiceImpl -->
<bean id="studentService" class="com.wishwzp.service.impl.StudentServiceImpl"></bean> <!-- 配置AOP -->
<aop:config>
<aop:aspect id="studentServiceAspect" ref="studentServiceAspect">
<!-- 配置执行切点路径和id -->
<aop:pointcut expression="execution(* com.wishwzp.service.*.*(..))" id="businessService"/>
<!-- 前置通知 -->
<aop:before method="doBefore" pointcut-ref="businessService"/>
<!-- 后置通知 -->
<aop:after method="doAfter" pointcut-ref="businessService"/>
<!-- 环绕通知 -->
<aop:around method="doAround" pointcut-ref="businessService"/>
<!-- 返回通知 -->
<aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/>
<!-- 异常通知 -->
<aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/>
</aop:aspect>
</aop:config>
</beans>

StudentServiceAspect.java:

 package com.wishwzp.advice;

 import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; public class StudentServiceAspect { //前置通知
public void doBefore(JoinPoint jp){
System.out.println("类名:"+jp.getTarget().getClass().getName());
System.out.println("方法名:"+jp.getSignature().getName());
System.out.println("开始添加学生:"+jp.getArgs()[0]);
} //后置通知
public void doAfter(JoinPoint jp){
System.out.println("类名:"+jp.getTarget().getClass().getName());
System.out.println("方法名:"+jp.getSignature().getName());
System.out.println("学生添加完成:"+jp.getArgs()[0]);
} //环绕通知,这里添加了返回值
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("添加学生前");
Object retVal=pjp.proceed();
System.out.println(retVal);
System.out.println("添加学生后");
return retVal;
} //返回通知
public void doAfterReturning(JoinPoint jp){
System.out.println("返回通知");
} //异常通知
public void doAfterThrowing(JoinPoint jp,Throwable ex){
System.out.println("异常通知");
System.out.println("异常信息:"+ex.getMessage());
}
}

StudentServiceImpl.java:

 package com.wishwzp.service.impl;

 import com.wishwzp.service.StudentService;

 public class StudentServiceImpl implements StudentService{

     @Override
public String addStudent(String name) {
// System.out.println("开始添加学生"+name);
System.out.println("添加学生"+name);
System.out.println(1/0);
// System.out.println("完成学生"+name+"的添加");
return name;
} }

StudentService.java:

 package com.wishwzp.service;

 public interface StudentService {

     public String addStudent(String name);
}

运行结果显示:

(三)Spring 之AOP 详解的更多相关文章

  1. 深入浅出学习Spring框架(三):AOP 详解

    AOP的英文解释——AOPAspect Oriented Programming面向切面编程.主要目的是通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. 在反 ...

  2. Spring之AOP详解

    文章大纲 一.AOP介绍二.Spring的AOP实战三.AOP常用标签四.项目源码及参考资料下载五.参考文章   一.AOP介绍 1. 什么是AOP 在软件业,AOP为Aspect Oriented ...

  3. 3、Spring的AOP详解和案例

    AOP(Aspect Oriented Programming),即面向切面编程. 1.OOP回顾 在介绍AOP之前先来回顾一下大家都比较熟悉的OOP(Object Oriented Programm ...

  4. Spring、AOP详解

    如何配置AOP查看:Spring.Hello AOP 1.对于拦截规则@Pointcut的介绍: @Pointcut("execution (* cn.raffaello.service.. ...

  5. spring的aop详解

    一.aop术语 1.连接点joinpoint: 程序执行的某个特定位置:如类开始初始化之前.类初始化之后.类某个方法调用前.调用后等.Spring仅支持方法的连接点,即仅能在方法调用前.方法调用后以及 ...

  6. 【转载】Spring AOP详解 、 JDK动态代理、CGLib动态代理

    Spring AOP详解 . JDK动态代理.CGLib动态代理  原文地址:https://www.cnblogs.com/kukudelaomao/p/5897893.html AOP是Aspec ...

  7. spring事务详解(三)源码详解

    系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.引子 ...

  8. Spring AOP详解及简单应用

    Spring AOP详解   一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址: ...

  9. 转:Spring AOP详解

    转:Spring AOP详解 一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址: ...

随机推荐

  1. 【HDU4471】Homework(矩阵快速幂)

    [HDU4471]Homework(矩阵快速幂) 题面 Vjudge 给定一个数列的前\(m\)项,给定一个和前\(t\)项相关的递推式. 有\(q\)个位置的递推式单独给出,求数列第\(n\)项. ...

  2. 【Aizu2292】Common Palindromes(回文树)

    [Aizu2292]Common Palindromes(回文树) 题面 Vjudge 神TMD日语 翻译: 给定两个字符串\(S,T\),询问\((i,j,k,l)\)这样的四元组个数 满足\(S[ ...

  3. 输入三个数a,b,n,输出a和b不大于n的公倍数的个数

    题:输入三个数a,b,n,输出a和b不大于n的公倍数的所有个数. 这题的思想是先求得a和b的最大公约数,然后用a和b的积除以最大公约数,得到最小公倍数,再持续加上最小公倍数,直到超过n,记下n的个数. ...

  4. SQL注入9种绕过WAF方法

    SQL注入9种绕过WAF方法 0x01前言 WAF区别于常规 防火墙 是因为WAF能够过滤特定Web应用程序的内容,而常规防火墙则充当服务器之间的防御门.通过检查HTTP的流量,它可以防御Web应用安 ...

  5. opencv透视变换GetPerspectiveTransform的总结

    对于透视变换,必须为map_matrix分配一个3x3数组,除了3x3矩阵和三个控点变为四个控点外,透视变化在其他方面与仿射变换完全类似.具体可以参考:点击打开链接 主要用到两个函数WarpPersp ...

  6. 3.6 scikit-learn:Python中的机器学习

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&am ...

  7. 给上传文件的input控件“美容”

    在工作中经常会遇到form表单这种东西.然而表单的其他input控件样式还是很好改变的.但是,唯独input类型是file的文件上传控件可能就没那么好打扮的漂亮. demo: html代码 <b ...

  8. 2015/11/3用Python写游戏,pygame入门(3):字体模块、事件显示和错误处理

    游戏里面一般是肯定会出现文字显示的,即使是俄罗斯方块也应该显示分数.那么我们应该怎样来显示文字呢,今天一起学习一下pygame的font模块. 使用字体模块 pygame可以直接调用系统字体,也可以调 ...

  9. logstash 收集 IIS 日志实践

    IIS日志示例: 2017-02-20 00:55:40 127.0.0.1 GET /MkWebAPI/swagger/ui/index - 80 - 127.0.0.1 Mozilla/5.0+( ...

  10. 最常用的8款 PHP 调试工具,你用过吗?

    Web 开发并不是一项轻松的任务,有超级多服务端脚本语言提供给开发者,但是当前 PHP 因为具有额外的一些强大的功能而越来越流行.PHP 是最强大的服务端脚本语言之一,同时也是 Web 开发者和设计者 ...