搭建网站的架构,南昌有做网站的吗,wordpress文件上传类型,情侣做记录网站源码文章目录前言一、AOP的底层实现原理二、AOP的两种开发模式1.使用xml配置文件1.1 添加AOP依赖1.2 创建UserService1.3创建UserServiceImpl1.4创建通知类1.5 创建applicationContext.xml#xff08;添加aop约束#xff09;1.6 测试2.使用注解开发2.1 创建bean.xml文件配置注解方…
文章目录前言一、AOP的底层实现原理二、AOP的两种开发模式1.使用xml配置文件1.1 添加AOP依赖1.2 创建UserService1.3创建UserServiceImpl1.4创建通知类1.5 创建applicationContext.xml添加aop约束1.6 测试2.使用注解开发2.1 创建bean.xml文件配置注解方式2.2 在通知类上使用相关注解2.3 测试总结前言
spring的核心是IOC控制反转和AOP面向切面编程。AOP面向切面编程是通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续是软件开发中的一个热点也是Spring框架中的一个重要内容是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离从而使得业务逻辑各部分之间的耦合度降低提高程序的可重用性同时提高了开发的效率。 一、AOP的底层实现原理
此处再解释详情请看我这篇文章 静态代理和动态代理https://blog.csdn.net/l_zl2021/article/details/127095878
二、AOP的两种开发模式
关于AOP的案例看我这边篇文章本文只是记录两种AOP开发方式 AOP初识https://blog.csdn.net/l_zl2021/article/details/127113425
1.使用xml配置文件
1.1 添加AOP依赖
dependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.3.20/version/dependencydependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactIdversion1.9.9.1/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.13.2/versionscopecompile/scope/dependency/dependencies1.2 创建UserService
package com.wmj.service;//目标对象target
public interface UserService {//未增强的方法叫做连接点JoinPoint//已增强的方法叫做切入点PointCutpublic void add();public void delete();
}1.3创建UserServiceImpl
代码如下示例
package com.wmj.service.impl;import com.wmj.service.UserService;public class UserServiceImpl implements UserService {Overridepublic void add() {System.out.println(添加用户..);//int i 1/0;}Overridepublic void delete() {System.out.println(删除用户..);}
}1.4创建通知类 前置通知(before)目标方法运行之前调用 后置通知(after-returning)在目标方法运行之后调用 (如果出现异常不会调用) 环绕通知(around)在目标方法之前和之后都调用(ProceedingJoinPoint对象 -- 调用proceed方法) 异常拦截通知(after-throwing)如果出现异常,就会调用 最终通知(after)在目标方法运行之后调用 (无论是否出现 异常都会调用) package com.wmj.advice;import org.aspectj.lang.ProceedingJoinPoint;//通知类增强的代码方法Advice
public class MyAdvice {public void before(){System.out.println(前置通知目标对象调用方法前执行);}public void after(){System.out.println(后置通知最终通知目标对象调用方法后执行无论是否发生异常都执行);}public void after_returning(){System.out.println(后置通知目标对象调用方法后执行发生异常不执行);}public void after_throwing(){System.out.println(异常通知发生异常执行);}public void around(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println(环绕通知目标对象调用方法之前);joinPoint.proceed();System.out.println(环绕通知目标对象调用方法之后);}}1.5 创建applicationContext.xml添加aop约束
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:aophttp://www.springframework.org/schema/aop xsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd !-- bean definitions here --bean iduserService classcom.wmj.service.impl.UserServiceImpl/bean!-- 通知 --bean idmyAdvice classcom.wmj.advice.MyAdvice/bean!-- aop --!-- 默认使用JDK动态代理 --!-- proxy-target-classtrue 使用cglib --aop:config proxy-target-classtrue!-- 配置切入点 切入点表达式的写法execution(表达式)public void com.abyg.service.UserServiceImpl.save() void com.wmj.service.UserServiceImpl.save() 其他修饰符无返回值的save空参方法* com.wmj.service.UserServiceImpl.save() 有或者无返回值的save空参方法* com.wmj.service.UserServiceImpl.*() 有或者无返回值的所有空参方法* com.wmj.service.*ServiceImpl.*(..) 有或者无返回值的所有有参或者空参方法* com.wmj.service..*ServiceImpl.*(..) 一般不用service包下的子包和孙包以ServiceImpl结尾的类中的方法
--!-- 切入点 --
!-- aop:pointcut idpc expressionexecution(public void com.wmj.service.impl.UserServiceImpl.add())/--aop:pointcut idpc expressionexecution(* com.wmj.service.impl.*ServiceImpl.*(..))/!-- 切面 --aop:aspect refmyAdvice!-- 配置前置通知对应的方法 --aop:before methodbefore pointcut-refpc/aop:before!-- 配置后置通知最终通知对应的方法 --aop:after methodafter pointcut-refpc/aop:after!-- 配置后置通知对应的方法发生异常不执行 --aop:after-returning methodafter_returning pointcut-refpc/aop:after-returning!-- 配置异常通知对应的方法发生异常执行 --aop:after-throwing methodafter_throwing pointcut-refpc/aop:after-throwing!-- 配置环绕通知对应的方法 --aop:around methodaround pointcut-refpc/aop:around/aop:aspect/aop:config/beans1.6 测试
package com.wmj.test;import com.wmj.service.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringTest {Testpublic void testUserService(){ClassPathXmlApplicationContext applicationContext new ClassPathXmlApplicationContext(applicationContext.xml);UserService userService (UserService)applicationContext.getBean(userService);userService.add();userService.delete();}}2.使用注解开发
2.1 创建bean.xml文件配置注解方式
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:aophttp://www.springframework.org/schema/aop xsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd !-- bean definitions here --!-- 准备工作: 导入aop(约束)命名空间 --!-- 1.配置目标对象 --bean iduserService classcom.wmj.service.impl.UserServiceImpl/bean!-- 2.配置通知对象 --bean idmyAdvice classcom.wmj.advice.MyAdvice/bean!-- 3.开启使用注解完成织入 --aop:aspectj-autoproxy/aop:aspectj-autoproxy/beans2.2 在通知类上使用相关注解
Aspect
//表示该类是一个通知类
//通知类增强的代码方法Advice
public class MyAdvice {//自己设置一个切点管理重复代码Pointcut(execution(* com.wmj.service.impl.*ServiceImpl.*(..)))public void pc(){}//前置通知//指定该方法是前置通知,并制定切入点Before(MyAdvice.pc())public void before(){System.out.println(前置通知目标对象调用方法前执行);}//最终通知After(execution(* com.wmj.service.impl.*ServiceImpl.*(..)))public void after(){System.out.println(后置通知最终通知目标对象调用方法后执行无论是否发生异常都执行);}//后置通知AfterReturning(execution(* com.wmj.service.impl.*ServiceImpl.*(..)))public void after_returning(){System.out.println(后置通知目标对象调用方法后执行发生异常不执行);}//异常通知AfterThrowing(execution(* com.wmj.service.impl.*ServiceImpl.*(..)))public void after_throwing(){System.out.println(异常通知发生异常执行);}//环绕通知Around(execution(* com.wmj.service.impl.*ServiceImpl.*(..)))public void around(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println(环绕通知目标对象调用方法之前);joinPoint.proceed();System.out.println(环绕通知目标对象调用方法之后);}}2.3 测试
Test
public void testUserService(){ClassPathXmlApplicationContext applicationContext new ClassPathXmlApplicationContext(bean.xml);UserService userService (UserService)applicationContext.getBean(userService);userService.add();userService.delete();
}总结
本文记录了两种开发AOP编程的方式