当前位置: 首页 > news >正文

【JAVAEE框架】浅谈 AOP 及代码实现

 哈喽~大家好呀,上篇讲到 Spring 框架的两大核心思想 AOP 与 IOP的原理,现在来看看具体代码实现。

 🥇个人主页:个人主页​​​​​             

🥈 系列专栏:【云原生系列】

🥉与这篇相关的文章:            

【JAVAEE框架】Spring 项目构建流程

【JAVAEE框架】Spring 项目构建流程_程序猿追的博客-CSDN博客

【JAVAEE框架】Mybatis常用操作(CRUD)

【JAVAEE框架】Mybatis常用操作(CRUD)_程序猿追的博客-CSDN博客

Java测试、反射、注解

Java测试、反射、注解_程序猿追的博客-CSDN博客

目录

一、前言

1、实现需要的知识点

2、定义切入点

二、原生代码实现

 三、注解实现

1、注解

2、Spring AOP配置元素

3、注解实现


一、前言

上篇讲到 AOP 它是基于代理模式下进行的,这篇来讲讲代码是如何实现。

1、实现需要的知识点

AspectJ 中常用的通知有四种类型:

(1)前置通知

(2)后置通知

(3)环绕通知

(4)最终通知

(5)定义切入点

2、定义切入点

切入点:简单的说,就是连接点的查询条件

示例

<aop:config>
<aop:pointcut id="pointcut"
            expression="execution(public void addNewUser(entity.User))"/>
</aop:config>

表达式匹配规则举例

public * addNewUser(entity.User): “*”表示匹配所有类型的返回值。
public void *(entity.User): “*”表示匹配所有方法名。
public void addNewUser(..): “..”表示匹配所有参数个数和类型。
* com.service.*.*(..):匹配com.service包下所有类的所有方法。
* com.service..*.*(..):匹配com.service包及其子包下所有类的所有方法

二、原生代码实现

pom导入 aop 需要的依赖

        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.5</version>
        </dependency>

创建 userService.java

用来做方法测试类

public class UserService {

    public Integer addUser(int n) {
        Integer r = 0;

        r = 100 / n;
        System.out.println("addUser 运行了");
        System.out.println(r);

        return r;
    }

    public int deleteUser(String id) {
        System.out.println("删除:" + id);
        return 1;
    }

}

编写 UserAspect.java

    // 前置通知
    // JoinPoint: 连接点,改对象包含目标方法的相关信息
    public void before(JoinPoint jp){
        System.out.println(jp.getTarget() + "的" + jp.getSignature().getName() + "方法执行了,参数: " + Arrays.toString(jp.getArgs()));
    }

    // 后置通知
    public void after(JoinPoint jp){
        System.out.println(jp.getTarget() + "的" + jp.getSignature().getName() + "之后方法执行了,参数: " + Arrays.toString(jp.getArgs()));
    }

    // 异常通知
    public void errorNation(JoinPoint jp, Throwable e){
        System.out.println(jp.getTarget() + "的" + jp.getSignature().getName() + "方法出错了,错误是: " + e.getMessage());
    }

    // 返回通知

    public void ReturnNation(JoinPoint jp, Object ret){
        System.out.println(jp.getTarget() + "的" + jp.getSignature().getName() + "方法的返回结果是: " + ret);
    }

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"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
	   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	   http://www.springframework.org/schema/aop
	   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
	">

	<bean id="UserAspect" class="com.itxzw.user.UserAspect"/>

	<bean id="UserService" class="com.itxzw.user.UserService"/>

	<aop:config>

		<aop:pointcut id="pointcut" expression="execution(public * com.itxzw.user.UserService.*(..))"/>

		<aop:aspect ref="UserAspect">
			<aop:before method="before" pointcut-ref="pointcut"/>
			<aop:after method="after" pointcut-ref="pointcut"/>
			<aop:after-throwing method="errorNation" throwing="e" pointcut-ref="pointcut"/>
			<aop:after-returning method="ReturnNation" returning="ret" pointcut-ref="pointcut"/>

<!--			<aop:around method="roundNation" pointcut-ref="pointcut"/>-->
		</aop:aspect>

	</aop:config>

</beans>

 编写测试类

    @Test
    public void Test01(){

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        UserService userService = (UserService) context.getBean("UserService");

        userService.addUser(10);

        System.out.println(userService);

    }

效果

底层原理就是采用了代理模式

 三、注解实现

1、注解

前置通知@Before:前置增强处理,在目标方法前织入增强处理

后置通知@AfterReturning:后置增强处理,在目标方法正常执行(不出现异常)后织入增强处理

环绕通知@Around:环绕增强处理,在目标方法的前后都可以织入增强处理

最终通知@After:最终增强处理,不论方法是否抛出异常,都会在目标方法最后织入增强处理

异常通知@AfterThrowing:异常增强处理,在目标方法抛出异常后织入增强处理

定义切入点@Pointcut

2、Spring AOP配置元素

 3、注解实现

注解方式将Bean的定义信息和Bean实现类结合在一起,Spring提供的注解有
@Component:实现Bean组件的定义
@Repository    :用于标注DAO类
@Service    :用于标注业务类
@Controller    :用于标注控制器类

@Repository("userDao") 
public class UserDaoImpl implements UserDao {
    …
}
与在XML配置文件中编写
<bean id="userDao" 
class="dao.impl.UserDaoImpl" /> 
等效
 

使用@Autowired注解实现Bean的自动装配,默认按类型匹配,可以使用@Qualifier指定Bean的名称

@Service("userService") 
public class UserServiceImpl implements UserService { 
    @Autowired
    @Qualifier("userDao")
    private UserDao dao; 
    …… 
}

可以对类的成员变量进行标注
@Service("userService") 
public class UserServiceImpl implements UserService { 
    private UserDao dao;
    @Autowired
    public void setDao((@Qualifier("userDao") UserDao dao) {
       this.dao = dao;
    } 
  …… 
}

也可以对方法的入参进行标注

使用注解信息启动Spring容器

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="......
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    <!-- 扫描包中注解标注的类 -->
    <context:component-scan base-package="service,dao" />
</beans>

指定需要扫描的基类包,多个包可用逗号隔开

示例

UserAspect.java

@Component
@Aspect
public class UserAspect {

    // 定义切入点
    @Pointcut("execution(public * com.itxzw..*.*(..))")
    public void pointcut(){}

    // 前置通知
    // JoinPoint: 连接点,改对象包含目标方法的相关信息
    @Before("pointcut()")
    public void before(JoinPoint jp){
        System.out.println(jp.getTarget() + "的" + jp.getSignature().getName() + "方法执行了,参数: " + Arrays.toString(jp.getArgs()));
    }

    // 后置通知
    @After("pointcut()")
    public void after(JoinPoint jp){
        System.out.println(jp.getTarget() + "的" + jp.getSignature().getName() + "之后方法执行了,参数: " + Arrays.toString(jp.getArgs()));
    }

    // 异常通知
    @AfterThrowing(pointcut = "pointcut()", throwing = "e")
    public void errorNation(JoinPoint jp, Throwable e){
        System.out.println(jp.getTarget() + "的" + jp.getSignature().getName() + "方法出错了,错误是: " + e.getMessage());
    }

    // 返回通知
    @AfterReturning(pointcut = "pointcut()", returning = "ret")
    public void ReturnNation(JoinPoint jp, Object ret){
        System.out.println(jp.getTarget() + "的" + jp.getSignature().getName() + "方法的返回结果是: " + ret);
    }

    // 环绕通知
    @Around("pointcut()")
    public void roundNation(ProceedingJoinPoint jp){
        Object t = jp.getTarget();
        String name = jp.getSignature().getName();
        String args = Arrays.toString(jp.getArgs());
        Object rel;

        try {
            System.out.println(t + "的" + name + "方法执行了,参数是:" + args);

            rel = jp.proceed(jp.getArgs());

            System.out.println(t + "的" + name + "方法结果是:" + rel);
        }catch (Throwable e){
            System.out.println(t + "的" + name + "方法出错了:" + e.getMessage());
        }finally {
            System.out.println(t + "的" + name + "方法结束了");
        }
    }

}

UserService.java

@Component(value = "userService")
public class UserService {

    public Integer addUser(int n) {
        Integer r = 0;


        r = 100 / n;
        System.out.println("addUser 运行了");
        System.out.println(r);

        return r;
    }

    public int deleteUser(String id) {
        System.out.println("删除:" + id);
        return 1;
    }

}

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:context="http://www.springframework.org/schema/context"
	   xmlns:aop="http://www.springframework.org/schema/aop"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
	   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	   http://www.springframework.org/schema/aop
	   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
	   http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.2.xsd">


<!--xmlns:context="http://www.springframework.org/schema/context"-->

<!--	<bean id="address" class="com.itxzw.user.model.Address">-->

<!--	</bean>-->

<!--	开启扫描-->
	<context:component-scan base-package="com.itxzw.user" />

<!--	开启Aspect生成代理对象-->
	<aop:aspectj-autoproxy proxy-target-class="false"/>

</beans>

测试

    @Test
    public void test02(){

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        UserService userService = (UserService) context.getBean("userService");

        userService.addUser(10);

    }

效果

不积跬步无以至千里,趁年轻,使劲拼,给未来的自己一个交代!向着明天更好的自己前进吧!

相关文章:

  • React获取DOM和获取组件实例的方式
  • [Spark、hadoop]spark Streaming的核心DStream
  • 【Vue】父子组件通信
  • API接口开发其实特简单,Python Flask Web 框架教程来了
  • SpringMVC03之拦截器和JSR303
  • 索引失效案例
  • 机器学习笔记 - Albumentations库实现的图像增强功能一览
  • 软考-存储系统
  • 大学生网页设计制作作业实例代码 (全网最全,建议收藏) HTML+CSS+JS
  • GAN Step By Step -- Step4 CGAN
  • HackerRank 算法刷题笔记(一),基于Go语言
  • 【Linux篇】第十三篇——信号(概念+信号的产生+阻塞信号+捕捉信号)
  • Android Jetpack系列之MVI架构
  • 十分钟之内实现stack和queue?容器适配器是什么?priority_queue不是队列?
  • 基于Keras实战项目-猫狗熊猫分类大战
  • 【笔记】你不知道的JS读书笔记——Promise
  • eclipse的离线汉化
  • Java 11 发布计划来了,已确定 3个 新特性!!
  • Js基础知识(四) - js运行原理与机制
  • Python 基础起步 (十) 什么叫函数?
  • React中的“虫洞”——Context
  • sublime配置文件
  • 初识MongoDB分片
  • 二维平面内的碰撞检测【一】
  • 分享一个自己写的基于canvas的原生js图片爆炸插件
  • 关于springcloud Gateway中的限流
  • 海量大数据大屏分析展示一步到位:DataWorks数据服务+MaxCompute Lightning对接DataV最佳实践...
  • 基于Mobx的多页面小程序的全局共享状态管理实践
  • 聚簇索引和非聚簇索引
  • 开源中国专访:Chameleon原理首发,其它跨多端统一框架都是假的?
  • 聊聊directory traversal attack
  • 算法-插入排序
  • 掌握面试——弹出框的实现(一道题中包含布局/js设计模式)
  • 中国人寿如何基于容器搭建金融PaaS云平台
  • ionic异常记录
  • 树莓派用上kodexplorer也能玩成私有网盘
  • ​ArcGIS Pro 如何批量删除字段
  • ​Java并发新构件之Exchanger
  • ​低代码平台的核心价值与优势
  • ​软考-高级-系统架构设计师教程(清华第2版)【第9章 软件可靠性基础知识(P320~344)-思维导图】​
  • #我与Java虚拟机的故事#连载18:JAVA成长之路
  • (173)FPGA约束:单周期时序分析或默认时序分析
  • (cos^2 X)的定积分,求积分 ∫sin^2(x) dx
  • (带教程)商业版SEO关键词按天计费系统:关键词排名优化、代理服务、手机自适应及搭建教程
  • (一)基于IDEA的JAVA基础12
  • (中等) HDU 4370 0 or 1,建模+Dijkstra。
  • (转) Face-Resources
  • (转载)(官方)UE4--图像编程----着色器开发
  • .net 8 发布了,试下微软最近强推的MAUI
  • .NET CLR Hosting 简介
  • .NET/C# 使用反射调用含 ref 或 out 参数的方法
  • .NetCore实践篇:分布式监控Zipkin持久化之殇
  • [ABC294Ex] K-Coloring
  • [ACTF2020 新生赛]Include
  • [Asp.net MVC]Asp.net MVC5系列——Razor语法