基于Aspectj表达式配置的Spring AOP
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的更多相关文章
- Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探
由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...
- Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较
本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...
- spring-AOP框架(基于AspectJ注解配置AOP)
基于AspectJ注解配置AOP 1.加入jar包: 要在Spring应用中使用AspectJ注解,必须在classpath下包含AspectJ类库:aopalliance.jar.aspectj.w ...
- Spring Aop(七)——基于XML配置的Spring Aop
转发:https://www.iteye.com/blog/elim-2396043 7 基于XML配置的Spring AOP 基于XML配置的Spring AOP需要引入AOP配置的Schema,然 ...
- 基于AspectJ的XML方式进行AOP开发
-------------------siwuxie095 基于 AspectJ 的 XML 方式进行 AOP 开发 1 ...
- 基于AspectJ的注解方式进行AOP开发
-------------------siwuxie095 基于 AspectJ 的注解方式进行 AOP 开发 ...
- 基于配置的Spring AOP
前面几篇学习了Spring的依赖注入,这篇开始学习另一个核心功能——面向切面编程AOP. 通过本文,你可以了解到: 1 Spring xml规范 2 通过配置文件实现面向切面编程 3 对比与传统AOP ...
- 基于XML配置的spring aop增强配置和使用
在我的另一篇文章中(http://www.cnblogs.com/anivia/p/5687346.html),通过一个例子介绍了基于注解配置spring增强的方式,那么这篇文章,只是简单的说明,如何 ...
- 基于@AspectJ注解配置切面与基于XML配置切面
1. Waiter目标类 package com.smart.aop.advice.pointcut; public class Waiter { public void greetTo(String ...
随机推荐
- 2018中国大学生程序设计竞赛 - 网络选拔赛---Find Integer!--hdu6441
问题传送门:https://vjudge.net/contest/320779#problem/D 介绍一个名词:奇偶数列法则 Key part: #include<iostream> # ...
- 重载(Overload)和重写(Override)的区别是什么?
首先java程序的运行分为编译和运行两部分. 所以重载和重写在这一点就有很明显的区别,因为重写方法的方法名和参数个数类型都一样,所以在java虚拟机的编译阶段是识别不出重写的方法的不同,在运行期间才可 ...
- sql 按天及上午下午分组
),r.CTime,), then '下午' end as k, count(*) from Record as r left join Channel as c on r.ChannelId=c.I ...
- Eclipse项目转到MyEclipse中出错
原因如下. JDK的编译版本和JRE的运行版本不一致导致了这个问题. 在MyEclipse中,对项目进行Build path 逐一设置即可. 还有关于类型转换的问题,由于JDK版本的不一致,下载下来的 ...
- pandas模块详解
Pandas模块 1.什么是pandas pandas是基于numpy构建的,用来做数据分析的 2.pandas能干什么 具备对其功能的数据结构DataFrame,Series 集成时间序列功能 提供 ...
- Linux oracle中文乱码的问题解决
乱码问题的根源是字符集的修改 1.查看linux的默认语言 2.查看客户端的语言编码设置 配置文件中的配置: cat ~/.bash_profile 注意修改配置信息: export PATHexp ...
- python3.0练习100题——001
自学python3中,现在开始每天在python2.71 100例中做一道题,用python3实现,并写下一些思考-加油(ง •̀灬•́)ง 题目网站(http://www.runoob.com/py ...
- codeforces div2_604 E. Beautiful Mirrors(期望+费马小定理)
题目链接:https://codeforces.com/contest/1265/problem/E 题意:有n面镜子,你现从第一面镜子开始询问,每次问镜子"今天我是否美丽",每天 ...
- el-popover 点击input框出现table表,可点击选中,可拼音检索完回车选中
<template> <card> <el-popover placement="right" width="400" trigg ...
- 53最大子序和.py
题目:给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 来源:https://leetcode-cn.com/problems/maximum-s ...


