springAOP

这是spring的另一个主要的核心功能,用来提供声明式事务:允许用户自定义切面。基本原理是动态代理,代理的是接口不是一个具体的类。

1.基本概念

切面(aspect):就是一个用来拓展功能的类。
通知(advice):拓展功能的类中的方法。
目标(target):被通知对象
代理(proxy):向目标对象应用通知之后创建的对象。
切入点(pointcut):切面通知执行地点的定义。
连接点(jointpoint):与切入点匹配的执行点

2.使用AOP需要导入的包

<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.4</version>
</dependency>

3.AOP实现方式一(springAPI方式)

3.1service层接口以及其实现类

接口:

public interface UserService {public void add();public void delete();public void update();public void select();
}

实现类:

public class UserServiceImpl implements UserService {@Overridepublic void add() {System.out.println("增加");}@Overridepublic void delete() {System.out.println("删除");}@Overridepublic void update() {System.out.println("修改");}@Overridepublic void select() {System.out.println("查询");}
}

3.2编写日志增强类

后置类:

public class AfterLog implements AfterReturningAdvice {@Overridepublic void afterReturning(Object returnValue, Method method, Object[] objects, Object o1) throws Throwable {System.out.println("执行了" + method.getName() + "返回结果为:" + returnValue);}
}

前置类:

public class BeforeLog implements MethodBeforeAdvice {//method:要执行的目标对象的方法//args:参数//target:目标对象@Overridepublic void before(Method method, Object[] args, Object target) throws Throwable {System.out.println(target.getClass().getName() + "的" + method.getName() + "被执行了");}
}

3.3编写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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd"><!--    注册bean--><bean id="userService" class="com.joker.service.UserServiceImpl"></bean><bean id="beforelog" class="com.joker.log.BeforeLog"></bean><bean id="afterlog" class="com.joker.log.AfterLog"></bean><!--    使用原生api接口--><!--    配置aop:需要导入aop的约束-->
<aop:config><!--        切入点:expression表示在哪理执行,这是一个表达式--><aop:pointcut id="pointcut" expression="execution(* com.joker.service.UserServiceImpl.*(..))"/><!--        执行环绕增加--><aop:advisor advice-ref="beforelog" pointcut-ref="pointcut"></aop:advisor><aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"></aop:advisor>
</aop:config>
</beans>

3.4测试类

public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//动态代理的是接口所以这里必须是接口,不能象以前一样改为实现类UserService userService = (UserService) context.getBean("userService");userService.add();}
}

至此我们已经完成aop。在测试中,使用getbean方法的时候传入的是一个接口而不是一个具体的类。

4.AOP实现方式二(自定义类实现)

4.1定义自定义类(切面)

实现类和前面一样

public class DiyPointCut {public void before() {System.out.println("自定义before");}public void after() {System.out.println("自定义after");}
}

4.2配置文件

<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd"><!--    注册bean--><bean id="userService" class="com.joker.service.UserServiceImpl"></bean><bean id="beforelog" class="com.joker.log.BeforeLog"></bean><bean id="afterlog" class="com.joker.log.AfterLog"></bean>
<!--    方式二:自定义类(没有第一种方法强大)--><bean id="diy" class="com.joker.diy.DiyPointCut"></bean><aop:config><!--            定义切面--><aop:aspect ref="diy"><!--            下面是切入点--><!--            expression表达式--><aop:pointcut id="point" expression="execution(* com.joker.service.UserServiceImpl.*(..))"></aop:pointcut><aop:before method="before" pointcut-ref="point"></aop:before><aop:after method="after" pointcut-ref="point"></aop:after></aop:aspect></aop:config>
</beans>

4.3测试类

public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//动态代理的是接口所以这里必须是接口,不能象以前一样改为实现类UserService userService = (UserService) context.getBean("userService");userService.add();}
}

5.AOP实现方式三(注解实现)

5.1编写自定义类

@Aspect//标注这个类是一个切面
public class AnnotationPointCut {@Before("execution(* com.joker.service.UserServiceImpl.*(..))")public void before() {System.out.println("注解执行前");}@After("execution(* com.joker.service.UserServiceImpl.*(..))")//参数用来定义切入点public void after() {System.out.println("注解执行后");}@Around("execution(* com.joker.service.UserServiceImpl.*(..))")//在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点public void around(ProceedingJoinPoint jp) throws Throwable {System.out.println("环绕钱");//执行方法Object proceed = jp.proceed();System.out.println("环绕后");}
}

5.2配置文件

<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd"><!--    注册bean--><!--    注册bean--><bean id="userService" class="com.joker.service.UserServiceImpl"></bean><bean id="annotationPointCut" class="com.joker.diy.AnnotationPointCut"></bean><!--    开启注解支持--><aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

5.3测试类

public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//动态代理的是接口所以这里必须是接口,不能象以前一样改为实现类UserService userService = (UserService) context.getBean("userService");userService.add();}
}

spring(六)AOP相关推荐

  1. Spring(六):AOP

    今天来学习spring的另一个重要部分,AOP面向切面编程. 目录 1.代理模式 1.1 简介 1.2 术语 2. AOP概念及相关术语 2.1 AOP概述 2.2 术语 2.3 作用 3.基于注解A ...

  2. [Spring+SpringMVC+Mybatis]框架学习笔记(四):Spring实现AOP

    上一章:[Spring+SpringMVC+Mybatis]框架学习笔记(三):Spring实现JDBC 下一章:[Spring+SpringMVC+Mybatis]框架学习笔记(五):SpringA ...

  3. Spring 之AOP AspectJ切入点语法详解(最全了,不需要再去其他地找了)---zhangkaitao

    Spring 之AOP AspectJ切入点语法详解(最全了,不需要再去其他地找了) http://jinnianshilongnian.iteye.com/blog/1415606    --zha ...

  4. Spring的AOP介绍

    文章目录 前言 一.AOP是什么? 二. AOP 的作用及其优势 三.AOP 的底层实现 四.AOP 的动态代理技术 4.1.JDK 的动态代理 4.2.cglib 的动态代理 五.AOP 相关概念 ...

  5. Spring.Net Aop

    Spring.Net 有四种通知: IMethodBeforeAdvice,IAfterReturningAdvice,IMethodInterceptor,IThrowsAdvice BeforeA ...

  6. [Spring 深度解析]第4章 Spring之AOP

    第4章 ◄Spring之AOP► 在上一章节中,我们大致了解了Spring核心容器,了解了IOC思想在Spring中的具体应用Bean容器以及Bean的配置与使用,这一章我们将开始学习AOP在Spri ...

  7. Spring对AOP的支持

     Spring对AOP的支持<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" ...

  8. Spring 容器AOP的实现原理——动态代理

    本文来自极客学院 Spring 容器AOP的实现原理--动态代理 之前写了一篇关于IOC的博客--<Spring容器IOC解析及简单实现>,今天再来聊聊AOP.大家都知道Spring的两大 ...

  9. Spring的AOP使用xml配置

    需要使用spring的包,大家自己全部导入进去即可.省4........ 用户管理接口 package com.rx.spring; public interface UserManager { pu ...

  10. 【SSM框架系列】Spring 的 AOP(面向切面编程)

    什么是 AOP AOP 为 Aspect Oriented Programming 的缩写,意思为面向切面编程,是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. AOP 是 OOP ...

最新文章

  1. 远程mysql出现ERROR 1130 (HY000): Host '172.17.42.1' is not allowed to connect to this MySQL server...
  2. 百度搜索结果 转换_如何让图片出现在百度搜索结果里出现?
  3. Oracle LAST_DAY(d)
  4. [鸟哥linux视频教程整理]04_02_Linux 权限及权限管理
  5. 51. Python 数据处理(2)
  6. 光纤收发器按照网管怎么分类
  7. 【BZOJ1831】[AHOI2008]逆序对(动态规划)
  8. python七巧板三角形_用七巧板拼出14种三角形,这才是图形认知的神器!
  9. 请使用webdav_介绍下phpdav的使用功能价值
  10. 用Spring Cloud Alibaba开发微服务为什么越来越香?
  11. Python中文手册
  12. 区块链开发入门教程【加精】
  13. almost unreal歌词翻译_Almost Lover歌词
  14. 机器学习基础补习04---凸优化
  15. 网络文学网站的盈利模式分析
  16. Uber新CEO:公司最早将于2019年IPO上市
  17. 楼宇自控BACnet/IP协议网关功能特点
  18. git missing change-id解决办法
  19. Typora+PicGo+阿里云OSS实现云笔记
  20. Python写ROS话题

热门文章

  1. 一些团队管理心得总结
  2. 如何查看vue版本号以及vue/cli脚手架版本号
  3. 微信小程序日历打卡组件wx-calendar
  4. 自己研发的Android3D引擎开发的3d壁纸,欢迎大家拍砖~~
  5. FFmpeg源码分析:avcodec_send_packet()与avcodec_receive_frame()音视频解码
  6. JavaScript实现贪吃蛇小游戏(网页单机版)
  7. Nginx源码分析1--------编写Nginx扩展模块
  8. 【PHP Fatal error: Class ‘Redis’ not found in 错误】windows下为PHP安装redis扩展操作redis
  9. Unity 实战【360VR 看房】
  10. java 获取所有实现类_Java动态获取实现某个接口下所有的实现类对象集合