AOP(Aspect-Oriented Programming, 面向切面编程):是一种新的方法论, 是对传统OOP(Object-Oriented Programming, 面向对象编程)的补充。

它其实就是将公共的东西收取出来进行处理,横向重复,纵向抽取。从而使得代码更简洁。

我的博客地址:https://www.cnblogs.com/themysteryofhackers/p/12013351.html

更新时间:2019-12-09

一、创建Maven项目

创建Maven项目的步骤和我上一篇博客的步骤差不多,这里就不再论述了,如果不记得的话,点击这里如何创建Maven项目

创建好Maven项目后,就直接创建包和类。

二、添加项目依赖

因为要用到Spring中的AOP技术,所以要导入Spring的依赖,又因为要用到单元测试,所以也要导入Junit的依赖。

<?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.zzx</groupId> <artifactId>Spring_AOP</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.12.RELEASE</version> </dependency> <!-- spring aop支持 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.12.RELEASE</version> </dependency> <!-- aspectj支持 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency> </dependencies> </project>

三、创建类

我的是在com.zzx.aspect包下创建了一个MyAspect.java类,

package com.zzx.aspect; import org.springframework.stereotype.Component; //这是将对象交给Spring容器管理,并且取名为myAspect @Component("myAspect") public class MyAspect { //这是前置通知的方法 public void before(){ System.out.println("商品进货!"); } //这是后置通知的方法 public void afterReturn(){ System.out.println("售后分红!"); } }

我的是在com.zzx.sales包下创建了一个OppoSales.java和XiaomiSales.java类。

OppoSales.java

package com.zzx.sales; import org.springframework.stereotype.Component; //这是将对象交给Spring容器管理,并且取名为oppo @Component("oppo") public class OppoSales { public void salePhone() { System.out.println("出售OPPO手机!"); } public void salePC() { System.out.println("出售OPPO电脑!"); } }

XiaomiSales.java

package com.zzx.sales; import org.springframework.stereotype.Component; //这是将对象交给Spring容器管理,并且取名为xiaomi @Component("xiaomi") public class XiaomiSales { public void salePhone() { System.out.println("出售小米手机!"); } public void salePC() { System.out.println("出售小米电脑!"); } }

我的是在resources目录下创建了applicationContext.xml配置文件。配置文件中的表达式

execution(* com.zzx.sales..*.salePC(..))就是Aspectj表达式,这个表达式的意思是第一个*号是代表所有返回类型,com.zzx.sales是需要拦截的类的包名,后面的两个点,是代表当前包和它的子孙包,第二*号代表的是类名,salePC是方法名,方法名里面的两个点是代表任何参数,如果你还想深入了解Aspectj表达式的话,就点击Aspectj表达式详解

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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- Spring扫描包 --> <!-- 扫描包,只有扫描的包,Spring的注解才会生效,设置要扫描的那个包,那它的子包也会扫描 --> <context:component-scan base-package="com.zzx"/> <aop:config> <!-- 配置切入点 --> <aop:pointcut id="pointId" expression="execution(* com.zzx.sales..*.salePC(..))"/> <!-- 配置切面,把增强用来方法上面 --> <aop:aspect ref="myAspect"> <!-- 前置通知 method填的是切面对象方法 , pointcut-ref填的是切点id --> <aop:before method="before" pointcut-ref="pointId"></aop:before> <!-- 后置通知 --> <aop:after method="afterReturn" pointcut-ref="pointId"></aop:after> </aop:aspect> </aop:config> </beans>

我的是在text目录下创建了com.zzx.aop包,还创建了一个Test01.java类。

Test01.java

package com.zzx.aop; import com.zzx.sales.OppoSales; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test01 { @Test public void testSpringAOP(){ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); OppoSales oppoSales = (OppoSales) applicationContext.getBean("oppo"); oppoSales.salePC(); System.out.println("=================================="); oppoSales.salePhone(); } }

最后直接运行程序,结果如下:(因为我的Aspectj表达式最后那里指定了方法salePC,所以只有调用salePC方法才会将前置和后置方法切入,如果想要任何方法都要切入,就把salePC该成*即可)

结尾

我是一个Java程序员,一个向往技术的小白,以后我会陆续将自己学习到的Java或者其他的知识会以博客的形式分享出来,希望能对大家有帮助。

喜欢小编的就给我一个关注吧!

如果有哪些问题、有哪些不妥或者侵犯到您的权益的地方,可以联系我,我马上修改。

基于Aspectj表达式配置的Spring AOP的更多相关文章

  1. Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探

    由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...

  2. Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较

    本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring  AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...

  3. spring-AOP框架(基于AspectJ注解配置AOP)

    基于AspectJ注解配置AOP 1.加入jar包: 要在Spring应用中使用AspectJ注解,必须在classpath下包含AspectJ类库:aopalliance.jar.aspectj.w ...

  4. Spring Aop(七)——基于XML配置的Spring Aop

    转发:https://www.iteye.com/blog/elim-2396043 7 基于XML配置的Spring AOP 基于XML配置的Spring AOP需要引入AOP配置的Schema,然 ...

  5. 基于AspectJ的XML方式进行AOP开发

    -------------------siwuxie095                                 基于 AspectJ 的 XML 方式进行 AOP 开发         1 ...

  6. 基于AspectJ的注解方式进行AOP开发

    -------------------siwuxie095                                     基于 AspectJ 的注解方式进行 AOP 开发         ...

  7. 基于配置的Spring AOP

    前面几篇学习了Spring的依赖注入,这篇开始学习另一个核心功能——面向切面编程AOP. 通过本文,你可以了解到: 1 Spring xml规范 2 通过配置文件实现面向切面编程 3 对比与传统AOP ...

  8. 基于XML配置的spring aop增强配置和使用

    在我的另一篇文章中(http://www.cnblogs.com/anivia/p/5687346.html),通过一个例子介绍了基于注解配置spring增强的方式,那么这篇文章,只是简单的说明,如何 ...

  9. 基于@AspectJ注解配置切面与基于XML配置切面

    1. Waiter目标类 package com.smart.aop.advice.pointcut; public class Waiter { public void greetTo(String ...

随机推荐

  1. 初识消息队列--ActiveMq

    消息队列 即MessageQueue,是一种消息中间件,在遇到系统请求量比较大的情况下,导致请求堆积过多无法及时返回,可以通过它进行异步的消息处理,从而缓解系统压力. ActiveMq ActiveM ...

  2. HDU 1542 Atlantis(扫描线算法)

    题意:给出n个矩形的左下角左边和右上角坐标,求这n个矩形的面积并 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 典型的扫描线算法的题目 什么是 ...

  3. vue的一些基础知识点,后续会更新最全的vue知识点

    axios中jq的基础 jq语法 $(this).hide() 隐藏当前的html元素 $(''#test").hide() 隐藏id='test'的元素 添加新的 HTML 内容 我们将学 ...

  4. AcWing 1012. 友好城市

    #include<iostream> #include<algorithm> using namespace std ; typedef pair<int,int> ...

  5. Java 散列集笔记

    散列表 散列表(hash table)为每个对象计算一个整数,称为散列码(hash code). 若需要自定义类,就要负责实现这个类的hashCode方法.注意自己实现的hashCode方法应该与eq ...

  6. Python之旅第二天(第一天补充部分、数据类型、运算逻辑、部分方法的引入、pycharm)

    今天其实是有点小忙的,但是干自己不喜欢事情的结果就是,要睡觉了都不知道自己在忙鸡毛,所以还是不继续想了,脑仁疼.回忆一下今天的学习内容,着实有点少,本大侠还没怎么过瘾呢.废话不多说. while补充两 ...

  7. 剑指offer 面试题. 数据流中的中位数

    题目描述 如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值.如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值.我们 ...

  8. 创建本地yum源仓库

    更新本地yum源 yum仓库服务端配置如下 : 1. 创建yum仓库目录 mkdir -p /data/yum_data/ cd /data/yum_data/ #可以上传rpm包到此目录,此目录下面 ...

  9. 钉钉、钉应用(微应用和E应用)开发介绍

    钉钉,数字化新工作方式,让工作更简单 目前在钉钉的官网可以看到,超过700万家企业组织正在使用钉钉.笔者也相信,这一数字每天都在增加.获得群众的认可,也是理所当然的,体验过钉钉,就能感觉到,钉钉的考勤 ...

  10. find & grep 总 结

    前言 关于本文 总 结 了 find.grep常 规 用 法,正 则 表 达 式,find与 grep合 用 以 及 自 定 义 搜 索 函 数 等 什么是find和grep find 和 grep ...