引言

1.对于AOP编程思想,很多框架都进行了实现,Spring就是其中之一,可以完成面向切面编程。然后,AspectJ也实现了AOP的功能。并且实现的更加简单、方便,并且还支持注解式开发。所以,spring又将AspectJ的对于AOP的实现也引入到了自己的框架中。

2.在Spring中使用AOP开发的时候,一般使用AspectJ的实现方式。

3.AspectJ是一个优秀的面向切面的框架,它扩展了java语言,提供了强大的切面实现,它是Eclipse的一个开源项目。

2.AspectJ的通知类型

AspectJ中常用的通知有五种类型:
1.前置通知@Before
2.后置通知@AfterReturning
3.环绕通知@Arround
4.异常通知@After Throwing
5.最终通知@After

3.AspectJ的切入点表达式



3.准备AspectJ的开发环境

1.加入Maven依赖


```xml<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.bjpowernode</groupId><artifactId>ch06-aop-aspectj</artifactId><version>1.0-SNAPSHOT</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!--spring依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.5.RELEASE</version></dependency><!--aspectj依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.1</version></dependency></dependencies><build><plugins><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build>
</project>

2.引入AspectJ依赖

4.AspectJ基于注解的AOP实现

实现步骤
a.定义业务接口和实现类

public interface SomeService {void doSome(String name,Integer age);
}
//目标类  厂家
public class SomeServiceImpl implements SomeService {@Overridepublic void doSome(String name,Integer age) {//给doSome方法增加一个功能,在doSome()执行之前, 输出方法的执行时间System.out.println("====目标方法doSome()====");}public void doOther(String name,Integer age) {//给doSome方法增加一个功能,在doSome()执行之前, 输出方法的执行时间System.out.println("====目标方法doSome()====");}
}

b.定义切面类


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;import java.util.Date;/***  @Aspect : 是aspectj框架中的注解。*     作用:表示当前类是切面类。*     切面类:是用来给业务方法增加功能的类,在这个类中有切面的功能代码*     位置:在类定义的上面*/
@Aspect
public class MyAspect {/*** 定义方法,方法是实现切面功能的。* 方法的定义要求:* 1.公共方法 public* 2.方法没有返回值* 3.方法名称自定义* 4.方法可以有参数,也可以没有参数。*   如果有参数,参数不是自定义的,有几个特殊参数类型可以使用。*//*** @Before: 前置通知注解*   属性:value ,是切入点表达式,表示切面的功能执行的位置。*   位置:在方法的上面* 特点:*  1.在目标方法之前先执行的*  2.不会改变目标方法的执行结果*  3.不会影响目标方法的执行。*/@Before(value = "" +"execution(" +"public void com.bjpowernode.ba01.SomeServiceImpl.doSome(String,Integer))" +"")public void myBefore(){//就是你目标类要增加的功能代码System.out.println("前置通知, 切面功能:在目标方法之前输出执行时间:"+ new Date());}/*@Before(value = "execution(void com.bjpowernode.ba01.SomeServiceImpl.doSome(String,Integer))")public void myBefore(){//就是你切面要执行的功能代码System.out.println("1=====前置通知, 切面功能:在目标方法之前输出执行时间:"+ new Date());}*//*  @Before(value = "execution(void *..SomeServiceImpl.doSome(String,Integer))")public void myBefore(){//就是你切面要执行的功能代码System.out.println("2=====前置通知, 切面功能:在目标方法之前输出执行时间:"+ new Date());}*//*@Before(value = "execution(* *..SomeServiceImpl.*(..))")public void myBefore(){//就是你切面要执行的功能代码System.out.println("3=====前置通知, 切面功能:在目标方法之前输出执行时间:"+ new Date());}*//*@Before(value = "execution(* do*(..))")public void myBefore2(){//就是你切面要执行的功能代码System.out.println("4=====前置通知, 切面功能:在目标方法之前输出执行时间:"+ new Date());}*//*@Before(value = "execution(* com.bjpowernode.ba01.*ServiceImpl.*(..))")public void myBefore2(){//就是你切面要执行的功能代码System.out.println("2=====前置通知, 切面功能:在目标方法之前输出执行时间:"+ new Date());}*//*** 指定通知方法中的参数 : JoinPoint* JoinPoint:业务方法,要加入切面功能的业务方法*    作用是:可以在通知方法中获取方法执行时的信息, 例如方法名称,方法的实参。*    如果你的切面功能中需要用到方法的信息,就加入JoinPoint.*    这个JoinPoint参数的值是由框架赋予, 必须是第一个位置的参数*/@Before(value = "execution(void *..SomeServiceImpl.doSome(String,Integer))")public void myBefore(JoinPoint jp){  //下面就是切面要执行的功能//获取方法的完整定义System.out.println("方法的签名(定义)="+jp.getSignature());System.out.println("方法的名称="+jp.getSignature().getName());//获取方法的实参Object args [] = jp.getArgs();for (Object arg:args){System.out.println("参数="+arg);}//就是你切面要执行的功能代码System.out.println("2=====前置通知, 切面功能:在目标方法之前输出执行时间:"+ new Date());}
}

c. 声明目标对和目标类对象

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd"><!--把对象交给spring容器,由spring容器统一创建,管理对象--><!--声明目标对象--><bean id="someService" class="com.bjpowernode.ba01.SomeServiceImpl" /><!--声明切面类对象--><bean id="myAspect" class="com.bjpowernode.ba01.MyAspect" /><!--声明自动代理生成器:使用aspectj框架内部的功能,创建目标对象的代理对象。创建代理对象是在内存中实现的, 修改目标对象的内存中的结构。 创建为代理对象所以目标对象就是被修改后的代理对象.aspectj-autoproxy:会把spring容器中的所有的目标对象,一次性都生成代理对象。--><aop:aspectj-autoproxy /><!--有接口也想用cjlib实现动态代理:如果你期望目标类有接口,使用cglib代理proxy-target-class="true":告诉框架,要使用cglib动态代理-->
<!--    <aop:aspectj-autoproxy proxy-target-class="true"/>-->
</beans>

d.测试类中使用目标对象的id(测试前置通知@Before)

import com.bjpowernode.ba01.SomeService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest01 {@Testpublic void test01(){String config="applicationContext.xml";ApplicationContext ctx = new ClassPathXmlApplicationContext(config);//从容器中获取目标对象SomeService proxy = (SomeService) ctx.getBean("someService");//com.sun.proxy.$Proxy8 :jdk动态代理//com.sun.proxy.$Proxy0System.out.println("proxy:"+proxy.getClass().getName());//通过代理的对象执行方法,实现目标方法执行时,增强了功能proxy.doSome("lisi",20);}
}

5.@Before前置通知-参数JoinPoint

6.@AfterReturning后置通知-returning属性

7.@Around环绕通知- ProceedingJoinPoint 参数


8.AfterThrowing异常通知-throwing属性

9.@After最终通知

10.@Pointcut定义切入点

AspectJ对AOP的实现相关推荐

  1. Spirng使用Aspectj实现AOP

    Aspectj实现AOP有两种方式: (1)基于aspectj的xml配置: (2)基于aspectj的注解方式: 一.基于aspectj的xml配置: 1.导入相关的AOP的jar包: 2.创建Sp ...

  2. Spring使用AspectJ开发AOP

    AspectJ 是一个基于 Java 语言的 AOP 框架,它扩展了 Java 语言.Spring 2.0 以后,新增了对 AspectJ 方式的支持,新版本的 Spring 框架,建议使用 Aspe ...

  3. 使用AspectJ开发AOP更加便捷,你不知道嘛

    前文 中,已经讲解了Spring传统的AOP开发,但在实际开发中,我们都是使用AspectJ进行AOP开发. AspectJ 简介 AspectJ 是一个基于Java语言的独立的AOP框架. 在没有A ...

  4. (转)Spring使用AspectJ进行AOP的开发:注解方式

    http://blog.csdn.net/yerenyuan_pku/article/details/69790950 Spring使用AspectJ进行AOP的开发:注解方式 之前我已讲过Sprin ...

  5. Spring @AspectJ 实现AOP 入门例子(转)

    AOP的作用这里就不再作说明了,下面开始讲解一个很简单的入门级例子. 引用一个猴子偷桃,守护者守护果园抓住猴子的小情节. 1.猴子偷桃类(普通类): Java代码   package com.samt ...

  6. Spring基于AspectJ实现AOP操作

    基于AspectJ实现AOP操作 准备工作 在项目工程里面引入 AOP 相关依赖. 如果是maven项目,使用pom.xml代替引入jar包的过程(注意) 学会使用切入点表达式 AOP 操作(Aspe ...

  7. Spring框架基于AspectJ的AOP开发规范和步骤

    AOP和动态代理的关系: AOP术语: 横向关注点: 需要新增的到业务代码中的功能(在目标对象那里叫横切关注点,在切面类中叫通知) 切面类: 封装了增强方法(横向关注点)的类 通知: 切面类中的每一个 ...

  8. Spring AOP编程-传统基于aspectJ切点AOP开发

    1.在配置文件上方增加aop相关配置. 2.在spring的配置文件中定义目标与通知. 3.使用aop:xxx标签来完成切面与切点声明. 4.我们使用aspectj的切面声明方式 需要在导入aspec ...

  9. Spring中基于注解@AspectJ的AOP实现

    @AspectJ 作为通过 Java 5 注释注释的普通的 Java 类,它指的是声明aspects 的一种风格.通过在基于架构的 XML 配置文件中包含以下元素,@AspectJ 支持是可用的. a ...

最新文章

  1. 【转】ubuntu 12.04 下 Vim 插件 YouCompleteMe 的安装
  2. java 文件写入 读取_JAVA文件的两种读取方法和三种写入方法
  3. Websphere: security-constraint in web.xml doesn't
  4. Docker网络相关
  5. 简单三步搭建一对一直播源码系统
  6. 阿里云能耗宝新品发布
  7. 控制面板项 .cpl 文件说明
  8. 看聊天记录都学不会C语言?太菜了吧》(16)我一直以为校花很漂亮,直到我叫了她一声...
  9. linux aemv7,无法在我的Ubuntu machin中安装“xlwings”
  10. 调整eclipse、SpringToolSuite4编辑器的内存大小以及显示
  11. 分布式日志收集系统Apache Flume的设计详细介绍
  12. 广西工学院2000级计算机系,广西工学院管理系信管教研室李明 - 欢迎访问广西科技大学.ppt...
  13. Leetcode每日一题:197.rising-temperature(上升的温度)
  14. Java关键字---this的由来和其三大作用
  15. jsf表单验证_JSF验证示例教程–验证器标签,定制验证器
  16. 基于MATLAB步态算法仿真的六足仿生机器人
  17. 直播预告| 基于神经网络模型的开放领域对话系统研究
  18. 高德地图导航onInitNaviSuccess只调用一次
  19. html打开页面时在img标签加域名,Html中的img标签 加载失败
  20. ESP8266 SOC门磁系统(一)---短信报警功能

热门文章

  1. Centos7更改默认启动模式
  2. 与ISP合作需要了解哪些?
  3. GreenPlum部署时所修改内核参数的含义
  4. 阿里代码扫描插件安装 (IDEA)
  5. 数据脱敏项目中遇见的问题
  6. 虚拟机下安装vmware tools
  7. 从30岁到35岁:为你的生命多积累一些厚度(转)
  8. UISeatchBar
  9. 算法周记(一)直接插入排序
  10. C++ 中的左值(Lvalues)和右值(Rvalues)