AOP是什么?

面向切面编程,把那些与业务无关,却为业务模块所共同调用的逻辑封装成一个可重的模块,即切面
使用"横切"技术,AOP把软件系统分为两个部分:核心关注点和横切关注点。业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点

为什么使用AOP?

如果说面向对象编程是关注将需求功能划分为不同的并且相对独立,封装良好的类,并让它们有着属于自己的行为,依靠继承和多态等来定义彼此的关系的话;那么面向切面编程则是希望能够将通用需求功能从不相关的类当中分离出来,能够使得很多类共享一个行为,一旦发生变化,不必修改很多类,而只需要修改这个行为即可。

如下,有两个方法分别为mul和div,这两个方法的内容代码大多数是重复的,则我们可以抽离一个新的方法

package com.zzj.calculatar.service;
import org.springframework.stereotype.Service; @Service
public class CalculatorService implements ICalculatorService{ @Override
public int mul(int a, int b) {
System.out.println("The mul method begins.");
System.out.println("The mul method args:["+a+","+b+"]");
return a*b;
} @Override
public int div(int a, int b) {
System.out.println("The div method begins.");
System.out.println("The div method args:["+a+","+b+"]");
return a/b;
} }

新的方法如下:

@Service
public class CalculatorService implements ICalculatorService{ @Override
public int mul(int a, int b) {
t("mul", a, b);
return a*b;
} @Override
public int div(int a, int b) {
t("div", a, b);
return a/b;
} private void t(String methodName,int a,int b){
System.out.println("The"+methodName+"method begins.");
System.out.println("The"+methodName+"method args:["+a+","+b+"]");
} }

AOP的切面化就体现在此,具体的AOP实现方式有:注解和XML配置

package com.zzj.calculatar.service;
import org.springframework.stereotype.Service; @Service
public class CalculatorService implements ICalculatorService{ @Override
public int mul(int a, int b) {
return a*b;
} @Override
public int div(int a, int b) {
return a/b;
} }

注解方式实现AOP:

package com.zzj.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; @Order(1)//切片执行顺序,默认为字典顺序
@Aspect
@Component
public class CalculatorAspect { //提取表达式
@Pointcut("execution(public int com.zzj.calculatar.service.CalculatorService.*(..))")
public void pointCut(){ } @Around(value="pointCut()")
public Object around(ProceedingJoinPoint joinPoint){
Object result = null;
Object target = joinPoint.getTarget();//目标对象
String methodName = joinPoint.getSignature().getName();
Object[] params = joinPoint.getArgs(); try{
try{
//前置增强
System.out.println(target.getClass().getName()+": The "+methodName+" method begins.");
System.out.println(target.getClass().getName()+": Parameters of the "+methodName+"method: ["+params[0]+","+params[1]+"]");
//执行目标对象内的方法
result = joinPoint.proceed();
}finally{
//后置增强
System.out.println(target.getClass().getName()+":The "+methodName+" method ends.");
}
//返回增强
System.out.println(target.getClass().getName()+":Result of the "+methodName+" method:"+result);
}catch (Throwable e) {
System.out.println(target.getClass().getName()+":Exception of the method "+methodName+": "+e);
}
return result;
} }
<!--XML代码-->
<context:component-scan base-package="com.zzj"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

XML配置方式实现AOP(不需要注解,将注解放在XML中):

package com.zzj.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
public class CalculatorAspect {
public Object around(ProceedingJoinPoint joinPoint){
Object result = null;
Object target = joinPoint.getTarget();//目标对象
String methodName = joinPoint.getSignature().getName();
Object[] params = joinPoint.getArgs(); try{
try{
//前置增强
System.out.println(target.getClass().getName()+": The "+methodName+" method begins.");
System.out.println(target.getClass().getName()+": Parameters of the "+methodName+"method: ["+params[0]+","+params[1]+"]");
//执行目标对象内的方法
result = joinPoint.proceed();
}finally{
//后置增强
System.out.println(target.getClass().getName()+":The "+methodName+" method ends.");
}
//返回增强
System.out.println(target.getClass().getName()+":Result of the "+methodName+" method:"+result);
}catch (Throwable e) {
System.out.println(target.getClass().getName()+":Exception of the method "+methodName+": "+e);
}
return result;
}
}
<!--扫描-->
<context:component-scan base-package="com.zzj"></context:component-scan>
<!--获取aspect类-->
<bean id="calculatorAspect" class="com.zzj.aspect.CalculatorAspect"></bean> <aop:config>
<!--提取表达式-->
<aop:pointcut expression="execution(public int com.zzj.calculatar.service.CalculatorService.*(..))" id="pointCut"/>
<!--环绕增强-->
<aop:aspect ref="calculatorAspect" order="1">
<aop:around method="around" pointcut-ref="pointCut"/>
</aop:aspect>
</aop:config>

测试类:

package com.zzj.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zzj.calculatar.service.ICalculatorService; public class Test { public static void main(String[] args){ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
ICalculatorService calculatorService = applicationContext.getBean(ICalculatorService.class);
System.out.println(calculatorService.getClass());//当有代理类时,获取的代理对象
System.out.println(calculatorService.div(1,1));
applicationContext.close(); } }

测试结果:

了解AOP以及实现方式的更多相关文章

  1. spring aop 使用注解方式总结

    spring aop的注解方式:和xml的配置方式略有区别,详细如下: 1.首先还是建立需要的切面类:切面类里面定义好切点配置,以及所有的需要实现的通知方法. /** * */ package com ...

  2. springboot aop 自定义注解方式实现完善日志记录(完整源码)

    版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 一:功能简介 本文主要记录如何使用aop切面的方式来实现日志记录功能. 主要记录的信息有: 操作人,方法名,参数,运行时间,操作类型 ...

  3. springboot aop 自定义注解方式实现一套完善的日志记录(完整源码)

    https://www.cnblogs.com/wenjunwei/p/9639909.html https://blog.csdn.net/tyrant_800/article/details/78 ...

  4. Spring AOP 不同配置方式产生的冲突问题

    Spring AOP的原理是 JDK 动态代理和CGLIB字节码增强技术,前者需要被代理类实现相应接口,也只有接口中的方法可以被JDK动态代理技术所处理:后者实际上是生成一个子类,来覆盖被代理类,那么 ...

  5. 5.3 Spring5源码--Spring AOP使用接口方式实现

    Spring 提供了很多的实现AOP的方式:Spring 接口方式,schema配置方式和注解. 本文重点介绍Spring使用接口方式实现AOP. 使用接口方式实现AOP以了解为目的. 更好地理解动态 ...

  6. Spring学习总结(三)——Spring实现AOP的多种方式

    AOP(Aspect Oriented Programming)面向切面编程,通过预编译方式和运行期动态代理实现程序功能的横向多模块统一控制的一种技术.AOP是OOP的补充,是Spring框架中的一个 ...

  7. spring aop 使用xml方式的简单总结

    spring aop的 xml的配置方式的简单实现: 1.编写自己的切面类:配置各个通知类型 /** * */ package com.lilin.maven.service.aop; import ...

  8. ssm+redis整合(通过aop自定义注解方式)

    此方案借助aop自定义注解来创建redis缓存机制. 1.创建自定义注解类 package com.tp.soft.common.util; import java.lang.annotation.E ...

  9. 浅谈spring中AOP以及spring中AOP的注解方式

    AOP(Aspect Oriented Programming):AOP的专业术语是"面向切面编程" 什么是面向切面编程,我的理解就是:在不修改源代码的情况下增强功能.好了,下面在 ...

  10. spring AOP自定义注解方式实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

随机推荐

  1. java代码开启关闭线程(nginx)

    源码: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; pub ...

  2. 【摘录自MDN】客户端和服务器

    客户端和服务器 连接到互联网的计算机被称作客户端和服务器.下面是一个简单描述它们如何交互的图表: 客户端是典型的Web用户入网设备(比如,你连接了Wi-Fi的电脑,或接入移动网络的手机)和设备上可联网 ...

  3. MySQL 之数据库初识

    一 数据库概述 数据库即存放数据的仓库,只不过这个仓库是在计算机存储设备上,而且数据是按一定的格式存放的.过去人们将数据存放在文件柜里,现在数据量庞大,已经不再适用. 数据库是长期存放在计算机内.有组 ...

  4. IELTS Writing Task 1: two-chart answer

    Thursday, January 09, 2020 The chart below shows the value of one country's exports in various categ ...

  5. MessageBox函数

    <Windows程序设计>(第五版)(美Charles Petzold著) https://docs.microsoft.com/zh-cn/windows/desktop/apiinde ...

  6. 从七牛服务下载PDF文件

    /** * 从七牛下载PDF文件 * @param request * @param response * @param exhiId * @throws MalformedURLException ...

  7. CSP-J2019 纪念品

    Description: Solution: 第一天买入,第二天卖出,在干些别的,再把第二天刚卖出的再买回来,就相当于是啥也没干.也就是说手中的物品本身要算在手中的钱中.这也就是为什么 dp 的状态可 ...

  8. JSTL中获取URL参数

    使用JSTL时,URL会被隐含的对象param包裹起来,使用param.变量名,直接获取值 <body>hello:${param.name}</body> 依据此逻辑,在使用 ...

  9. 关于JDK+Tomcat+eclipse+MyEclipse的配置方法

    说一下关于JDK+Tomcat +eclipse+MyEclipse的配置方法: 1.jdk的配置 我用的是jdk1.6版本,与jdk1.5方法相同.执行完安装程序后就要在进行一下配置,步骤如下:右键 ...

  10. CSS文本居中显示

    因为一直为元素居中问题而困扰,所以决定把自己遇到和看到的方法记录下来,以便以后查看 如果要让inline或inline-block元素居中显示,则父元素css中包含text-align:center; ...