AOP

切面就像一把菜刀,将Java处理业务流程进行分割,在分割处添加特定的业务处理。主要应用于声明事务、安全和缓存。在本文中,主要介绍两种切面的实现方法--Java配置和XML配置。

Java配置

  • 创建Java类

    创建一个Music的Java类,用于声明切点
  1. @Component
  2. public class Music {
  3. public void perform(int num){
  4. System.out.println("music");
  5. }
  6. }
  • 创建切面

    创建Aop Java类,并声明为切面。声明为切面使用注解@Aspect,同时,切面必须是一个Bean。同时,声明一个切点,避免创建通知的时候重复使用过长的表达式。
  1. @Component
  2. @Aspect
  3. public class Aop {
  4. @Pointcut("execution(** com.tidas.spring.fourth.aopjavaconfig.Music.*(..))")
  5. public void performer(){}
  6. @Before("performer()")
  7. public void beforee(){
  8. System.out.println("before");
  9. }
  10. @After("performer()")
  11. public void afterr(){
  12. System.out.println("after");
  13. }
  14. @AfterReturning("performer()")
  15. public void afterReturning(){
  16. System.out.println("afterreturning");
  17. }
  18. @AfterThrowing("performer()")
  19. public void throwingg(){
  20. System.out.println("throwing");
  21. }
  • 创建Java配置类

    创建JavaConfiguration 类,创建Bean工厂,在这里需要使用@EnableAspectAutoProxy注解启动Spring切面功能
  1. @Configuration
  2. @EnableAspectJAutoProxy
  3. @ComponentScan
  4. public class JavaConfiguration {
  5. }
  • 创建测试类

    创建Main测试类
  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(classes=JavaConfiguration.class)
  3. public class Main {
  4. @Autowired
  5. private Music music;
  6. @Test
  7. public void test(){
  8. music.perform(3);
  9. }
  10. }
  • 为通知传递参数

    希望将声明为切点方法中的参数传递到通知当中其,则需要在声明切点的使用指明args参数,在Java中使用&& 在xml中使用and,下面是创建为通知传递参数的切面:
  1. /*@Pointcut("execution(** com.tidas.spring.fourth.aopjavaconfig.Music.perform(int)) && args(number)")
  2. public void performer(int number){}
  3. @Before("performer(number)")
  4. public void beforee(int number){
  5. System.out.println("before" + number);
  6. }
  7. @After("performer(number)")
  8. public void afterr(int number){
  9. System.out.println("after" + number+2);
  10. }
  11. @AfterReturning("performer(number)")
  12. public void afterReturning(int number){
  13. System.out.println("afterreturning" + number+1);
  14. }
  15. @AfterThrowing("performer(number)")
  16. public void throwingg(int number){
  17. System.out.println("throwing" + number +3);
  18. }
  • 另外,还可以使用@Around穿件环绕通知,被声明为环绕通知的方法需要包含参数ProceedingJoinPoint,通过ProceedingJoinPoint.proceed()来区分前置 通知和后置通知,通过try...catch来获取异常通知
  1. @Component
  2. @Aspect
  3. public class AopSecond {
  4. @Pointcut("execution(** com.tidas.spring.fourth.aopjavaconfig.Music.perform(..))")
  5. public void performer(){}
  6. @Around("performer()")
  7. public void per(ProceedingJoinPoint jb){
  8. try{
  9. System.out.println("beforeer");
  10. jb.proceed();
  11. System.out.println("afterr");
  12. }catch(Throwable eThrowable){
  13. System.out.println("exception");
  14. }
  15. }
  16. }

XML中配置切面

  • 创建Java类
  1. @Component
  2. public class AopXml {
  3. public void performA(){
  4. System.out.println("performA");
  5. }
  6. public void performB(){
  7. System.out.println("performB");
  8. }
  9. }
  • 创建切面类
  1. @Component
  2. public class AopConfug {
  3. public void beforee(){
  4. System.out.println("before");
  5. }
  6. public void afterr(){
  7. System.out.println("after");
  8. }
  9. public void aroundd(ProceedingJoinPoint pb) throws Throwable{
  10. System.out.println("beforeeee");
  11. pb.proceed();
  12. System.out.println("afterrrr");
  13. }
  14. }
  • 创建xml配置文件,其中需要添加xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ,来支持component-scan,同时,还需要配置<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>支持自动注入
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  9. <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
  10. <context:component-scan base-package="com.tidas.spring.fourth.aopxml"/>
  11. <!-- 无参数 -->
  12. <aop:config>
  13. <aop:aspect ref="aopConfug">
  14. <aop:pointcut expression="execution(** com.tidas.spring.fourth.aopxml.AopXml.*(..))" id="aopxmll"/>
  15. <!-- 执行AopXml中的任何方法,都会通知切面 -->
  16. <aop:before pointcut-ref = "aopxmll" method="beforee"/>
  17. <aop:after pointcut-ref = "aopxmll" method="afterr"/>
  18. <aop:around pointcut-ref = "aopxmll" method="aroundd"/>
  19. <!-- 对于环绕通知,其实在xml中声明和其它通知声明一样,没有参数,而参数还是在具体的方法中就好 -->
  20. </aop:aspect>
  21. </aop:config>
  22. </beans>
  • 为通知传递参数

    创建带参数的通知
  1. //有参数
  2. /*public void beforee(int number){
  3. System.out.println("number:" + number);
  4. }*/

创建带参数的方法

  1. //有参数
  2. public void perform(int number){
  3. System.out.println("perform2");
  4. }

切面和切点的配置,在xml中使用and连接args参数

  1. <aop:config>
  2. <aop:aspect ref="aopConfug">
  3. <aop:pointcut expression="execution(** com.tidas.spring.fourth.aopxml.AopXml.*(int)) and args(number)" id="aopxmll"/>
  4. <aop:before pointcut-ref = "aopxmll" method="beforee"/>
  5. </aop:aspect>
  6. </aop:config>

通过切面为Java引入新的功能

Spring--AOP(面向切面)编程的更多相关文章

  1. 详细解读 Spring AOP 面向切面编程(二)

    本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...

  2. 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~

    简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...

  3. spring AOP面向切面编程学习笔记

    一.面向切面编程简介: 在调用某些类的方法时,要在方法执行前或后进行预处理或后处理:预处理或后处理的操作被封装在另一个类中.如图中,UserService类在执行addUser()或updateUse ...

  4. 【Spring系列】Spring AOP面向切面编程

    前言 接上一篇文章,在上午中使用了切面做防重复控制,本文着重介绍切面AOP. 在开发中,有一些功能行为是通用的,比如.日志管理.安全和事务,它们有一个共同点就是分布于应用中的多处,这种功能被称为横切关 ...

  5. 从源码入手,一文带你读懂Spring AOP面向切面编程

    之前<零基础带你看Spring源码--IOC控制反转>详细讲了Spring容器的初始化和加载的原理,后面<你真的完全了解Java动态代理吗?看这篇就够了>介绍了下JDK的动态代 ...

  6. Spring AOP面向切面编程详解

    前言 AOP即面向切面编程,是一种编程思想,OOP的延续.在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等.在阅读本文前希望您已经对Spring有一定的了解 注:在能对代码进行添 ...

  7. Spring AOP 面向切面编程相关注解

    Aspect Oriented Programming 面向切面编程   在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业.   ...

  8. Spring AOP 面向切面编程入门

    什么是AOP AOP(Aspect Oriented Programming),即面向切面编程.众所周知,OOP(面向对象编程)通过的是继承.封装和多态等概念来建立一种对象层次结构,用于模拟公共行为的 ...

  9. 详细解读 Spring AOP 面向切面编程(一)

    又是一个周末, 今天我要和大家分享的是 AOP(Aspect-Oriented Programming)这个东西,名字与 OOP 仅差一个字母,其实它是对 OOP 编程方式的一种补充,并非是取而代之. ...

  10. Spring Aop面向切面编程&&自动注入

    1.面向切面编程 在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面的过程叫做面向切面编程 2.常用概念 原有功能:切点,pointcut 前置通知:在切点之前执行的功能,befor ...

随机推荐

  1. 基于 HTML5 WebGL 的 3D 服务器与客户端的通信

    这个例子的初衷是模拟服务器与客户端的通信,我把整个需求简化变成了今天的这个例子.3D 机房方面的模拟一般都是需要鹰眼来辅助的,这样找产品以及整个空间的概括会比较明确,在这个例子中我也加了,这篇文章就算 ...

  2. 那些容易遗忘的web前端问题

    背景: 年底将至,本人这只才出门的前端菜鸟,终于有空闲的时间来整理一下最近投简历时出现的问题.有的是经常使用但是没有仔细留意造成的:有的是个人认为根本没人使用而忽略的.为了下次不出现这种错误,进行一下 ...

  3. 如何用VSCode愉快的写Python

    在学习Python的过程中,一直没有找到比较趁手的第三方编辑器,用的最多的还是Python自带的编辑器.由于本人用惯了宇宙第一IDE(Visual Studio),所以当Visual Studio C ...

  4. Java 管程解决生产者消费者问题

    同样是实验存档.//.. 依然以生产者消费者问题作为背景. 管程(=“资源管理程序”)将资源和对资源的操作封装起来,资源使用者通过接口操作资源就 ok,不用去考虑进程同步的问题. 管程: packag ...

  5. 模板引擎(smarty)知识点总结五

    ---------重点知识:循环------------ /*   smarty 循环之for循环 */ /*    基本的语法         {for $i=$start to $end step ...

  6. ES6 let和const命令(2)

    为什么要使用块级作用域 在ES5中只有全局作用域和函数作用域,没有块级作用域,因此带来了这些麻烦 内层变量可能会覆盖外层变量 var tmp = new Date(); console.log(tmp ...

  7. MVCC的一些理解

    link 一.MVCC简介 MVCC (Multiversion Concurrency Control),即多版本并发控制技术,它使得大部分支持行锁的事务引擎,不再单纯的使用行锁来进行数据库的并发控 ...

  8. 欢迎大家走进我的园子 ( ^___^ )y 本博客文章目录整理

    "记录"是见证成长:"成长"则意味着蜕变:“变",创造无限可能! ------致自己 文章越来越多,不容易查看,特整理了一个目录,方便快速查找 坚持的是分享,搬运的是知识,图的是大家的进步,欢迎更多的 ...

  9. JAVA模板方法

    package project01; abstract class MyRuntime{ public final void runtime(){ long starttime =System.cur ...

  10. K:java序列化与反序列化—transient关键字的使用

      首先,应该明白的是transient是java中的一个关键字,音标为 英: [ˈtrænziənt].   在了解transient关键字之前,应该先弄明白序列化和反序列化.所谓的序列化,通俗点的 ...