包头 网站建设网络服务器机柜

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

包头 网站建设,网络服务器机柜,建设企业网站企业网上银行,oa系统平台✅作者简介#xff1a;2022年博客新星 第八。热爱国学的Java后端开发者#xff0c;修心和技术同步精进。 #x1f34e;个人主页#xff1a;Java Fans的博客 #x1f34a;个人信条#xff1a;不迁怒#xff0c;不贰过。小知识#xff0c;大智慧。 #x1f49e;当前专栏… ✅作者简介2022年博客新星 第八。热爱国学的Java后端开发者修心和技术同步精进。 个人主页Java Fans的博客 个人信条不迁怒不贰过。小知识大智慧。 当前专栏SSM 框架从入门到精通 ✨特色专栏国学周更-心性养成之路 本文内容一文吃透 Spring 中的 AOP 编程 文章目录AOP 概述AOP 实现分类AOP 术语基于 Aspectj 实现 AOP 操作第一版基于xmlaop:config配置文件第二版基于xmlaop:aspect配置文件第三版基于注解实现通知AOP 概述 AOP 为 Aspect Oriented Programming 的缩写是面向切面编程通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP 是 OOP 的延续是软件开发中的一个热点也是 Spring 框架中的一个重要内容是函数式编程的一种衍生范型 AOP 可以分离业务代码和关注点代码重复代码在执行业务代码时动态的注入关注点代码。切面就是关注点代码形成的类。Spring AOP 中的动态代理主要有两种方式JDK 动态代理和 CGLIB 动态代理。JDK 动态代理通过反射来接收被代理的类并且要求被代理的类必须实现一个接口 AOP 实现分类 AOP 要达到的效果是保证开发者不修改源代码的前提下去为系统中的业务组件添加某种通用功能按照 AOP 框架修改源代码的时机可以将其分为两类 静态 AOP 实现 AOP 框架在编译阶段对程序源代码进行修改生成了静态的 AOP 代理类生成的 *.class 文件已经被改掉了需要使用特定的编译器比如 AspectJ。动态 AOP 实现 AOP 框架在运行阶段对动态生成代理对象在内存中以 JDK 动态代理或 CGlib 动态地生成 AOP 代理类如 SpringAOP AOP 术语 连接点(JointPoint)与切入点匹配的执行点在程序整个执行流程中可以织入切面的位置方法的执行前后异常抛出的位置 切点(PointCut)在程序执行流程中真正织入切面的方法。 切面(ASPECT)切点通知就是切面 通知(Advice)切面必须要完成的工作也叫增强。即它是类中的一个方法方法中编写织入的代码。 前置通知 后置通知 环绕通知 异常通知 最终通知 目标对象(Target)被织入通知的对象 代理对象(Proxy)目标对象被织入通知之后创建的新对象
通知的类型 Spring 方面可以使用下面提到的五种通知工作 通知描述前置通知在一个方法执行之前执行通知。最终通知在一个方法执行之后不考虑其结果执行通知。后置通知在一个方法执行之后只有在方法成功完成时才能执行通知。异常通知在一个方法执行之后只有在方法退出抛出异常时才能执行通知。环绕通知在一个方法调用之前和之后执行通知。 基于 Aspectj 实现 AOP 操作 基于 Aspectj 实现 AOP 操作经历了下面三个版本的变化注解版是我们最常用的。 切入点表达式 作用声明对哪个类中的哪个方法进行增强 语法 execution[访问权限修饰符] 返回值 [ 类的全路径名 ] 方法名 (参数列表)[异常] 访问权限修饰符 可选项不写就是四个权限都包含 写public就表示只包括公开的方法 返回值类型 必填项 * 标识返回值任意 全限定类名 可选项两个点 … 表示当前包以及子包下的所有类省略表示所有类 方法名 必填项 * 表示所有的方法 set*表示所有的set方法 形参列表 必填项 表示没有参数的方法 …)参数类型和参数个数随意的方法 *只有一个参数的方法 String 第一个参数类型随意第二个参数String类型 异常信息 可选项 省略时标识任何异常信息 第一版基于xmlaop:config配置文件 使用 Spring AOP 接口方式实现 AOP, 可以通过自定义通知来供 Spring AOP 识别对应实现的接口是: 前置通知MethodBeforeAdvice返回通知AfterReturningAdvice异常通知ThrowsAdvice环绕通知MethodInterceptor 实现步骤 1、定义业务接口 /** 使用接口方式实现AOP, 默认通过JDK的动态代理来实现. 非接口方式, 使用的是cglib实现动态代理/package cn.kgc.spring05.entity;public interface Teacher {String teachOnLine(String course);String teachOffLine(Integer course); }2、定义实现类 package cn.kgc.spring05.entity;public class TeacherA implements Teacher{Overridepublic String teachOnLine(String course) {System.out.println(TeacherA开始course课程线上教学);if(course.equals(java)){throw new RuntimeException(入门到放弃);}return course课程线上教学;}Overridepublic String teachOffLine(Integer course) {System.out.println(TeacherA开始course课程线下教学);return course课程线下教学;} } 3、实现接口定义通知类 前置通知类 package cn.kgc.spring05.advice;import org.springframework.aop.MethodBeforeAdvice;import java.lang.reflect.Method;//前置通知 public class MyMethodBeforeAdvice implements MethodBeforeAdvice {Overridepublic void before(Method method, Object[] objects, Object o) throws Throwable {System.out.println(————spring aop 前置通知————);} }后置通知类 package cn.kgc.spring05.advice;import org.springframework.aop.AfterReturningAdvice;import java.lang.reflect.Method;public class MyAfterReturnAdvice implements AfterReturningAdvice {Overridepublic void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {System.out.println(————spring aop 后置通知————);} }4、XML 配置方式 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd!–托管通知–bean idafter classcn.kgc.spring05.advice.MyAfterReturnAdvice/beanbean idbefore classcn.kgc.spring05.advice.MyMethodBeforeAdvice/beanbean idteacherA classcn.kgc.spring05.entity.TeacherA/bean!–AOP的配置–aop:config!–切点表达式–aop:pointcut idpt expressionexecution( (..))/aop:advisor advice-refbefore pointcut-refpt/aop:advisoraop:advisor advice-refafter pointcut-refpt/aop:advisor/aop:config /beans5、测试 package cn.kgc.spring05;import cn.kgc.spring05.entity.Teacher; import junit.framework.TestCase; import junit.framework.TestSuite; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/** Unit test for simple App./ RunWith(SpringJUnit4ClassRunner.class) ContextConfiguration(classpath:spring-config.xml) public class AppTest {AutowiredTeacher teacher;Testpublic void teachOnLine() {System.out.println(teacher.getClass());String s teacher.teachOnLine(java);System.out.println(s s);} }6、运行结果 第二版基于xmlaop:aspect配置文件 基于 xmlaop:config 配置文件的方式增加几个通知就会创建几个通知类那我们能否将这些通知类写在一个类中呢下面就让我来带你们找到解决之法 配置 AspectJ 标签解读表 实现步骤 1、定义业务接口 /** 使用接口方式实现AOP, 默认通过JDK的动态代理来实现. 非接口方式, 使用的是cglib实现动态代理/package cn.kgc.spring05.entity;public interface Teacher {String teachOnLine(String course);String teachOffLine(Integer course); }2、定义实现类 package cn.kgc.spring05.entity;public class TeacherA implements Teacher{Overridepublic String teachOnLine(String course) {System.out.println(TeacherA开始course课程线上教学);if(course.equals(java)){throw new RuntimeException(入门到放弃);}return course课程线上教学;}Overridepublic String teachOffLine(Integer course) {System.out.println(TeacherA开始course课程线下教学);return course课程线下教学;} } 3、实现接口定义通知类 package cn.kgc.spring05.advice;public class AllAdvice {public void before(){System.out.println(————前置通知————–);}public void afterReturning(){System.out.println(————后置通知————–);}public void afterThrowing(){System.out.println(————异常通知————–);}public void after(){System.out.println(————最终通知————–);}public void around(){System.out.println(————环绕通知————–);} }4、XML 配置方式 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd!–托管通知–bean idall classcn.kgc.spring05.advice.AllAdvice/beanbean idteacherA classcn.kgc.spring05.entity.TeacherA/bean!–AOP的配置–aop:config!–切点表达式–aop:pointcut idpt expressionexecution( (String))/aop:aspect refallaop:before methodbefore pointcut-refpt/aop:beforeaop:after-returning methodafterReturning pointcut-refpt/aop:after-returningaop:after-throwing methodafterThrowing pointcut-refpt/aop:after-throwingaop:after methodafter pointcut-refpt/aop:after !– aop:around methodaround pointcut-refpt/aop:around–/aop:aspect/aop:config /beans5、测试 package cn.kgc.spring05.advice;import cn.kgc.spring05.entity.Teacher; import junit.framework.TestCase; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;RunWith(SpringJUnit4ClassRunner.class) ContextConfiguration(classpath:spring-config2.xml) public class AllAdviceTest{AutowiredTeacher teacher;Testpublic void test01() {System.out.println(teacher.getClass());String s teacher.teachOnLine(java);System.out.println(s s);} }6、运行结果 第三版基于注解实现通知 常用 “通知” 注解如下   Aspect 注解将此类定义为切面。 Before 注解用于将目标方法配置为前置增强前置通知。 AfterReturning 注解用于将目标方法配置为后置增强后置通知。 Around 定义环绕增强环绕通知 AfterThrowing 配置异常通知 After 也是后置通知与 AfterReturning 很相似区别在于 AfterReturning 在方法执行完毕后进行返回可以有返回值。After 没有返回值。 实现步骤 1、定义业务接口 /** 使用接口方式实现AOP, 默认通过JDK的动态代理来实现. 非接口方式, 使用的是cglib实现动态代理/package cn.kgc.spring05.entity;public interface Teacher {String teachOnLine(String course);String teachOffLine(Integer course); }2、定义注解 package cn.kgc.spring05.advice;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;Target(ElementType.METHOD) Retention(RetentionPolicy.RUNTIME) public interface AnnoAdvice { }3、定义实现类 package cn.kgc.spring05.entity;import cn.kgc.spring05.advice.AnnoAdvice; import org.springframework.stereotype.Component;Component public class TeacherA implements Teacher{OverrideAnnoAdvicepublic String teachOnLine(String course) {System.out.println(TeacherA开始course课程线上教学);if(course.equals(java)){throw new RuntimeException(入门到放弃);}return course课程线上教学;}OverrideAnnoAdvicepublic String teachOffLine(Integer course) {System.out.println(TeacherA开始course课程线下教学);return course课程线下教学;} } 4、实现接口定义切面类 首先在类上面添加 Aspect 注解将该类转化为切面类再在类中的各个方法上面使用各自的 “通知” 注解即可实现。 package cn.kgc.spring05.advice;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.; import org.springframework.stereotype.Component;Component Aspect public class AllAdvice {Pointcut(annotation(AnnoAdvice))public void point(){}Before(point())public void before(){System.out.println(————前置通知————–);}AfterReturning(point())public void afterReturning(){System.out.println(————后置通知————–);}AfterThrowing(point())public void afterThrowing(){System.out.println(————异常通知————–);}After(point())public void after(){System.out.println(————最终通知————–);}Around(point())public Object aroundAdvice(ProceedingJoinPoint joinPoint){Object proceed null;try {System.out.println(———-spring aop 环绕 前通知———–);proceed joinPoint.proceed();System.out.println(———-spring aop 环绕 后通知———–);} catch (Throwable throwable) {throwable.printStackTrace();System.out.println(———-spring aop 环绕 异常通知———–);}finally {System.out.println(———-spring aop 环绕 最终通知———–);}return proceed;} } 5、XML 配置方式 开启包扫描和aspectj自动代理 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:aophttp://www.springframework.org/schema/aopxmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd!–开启包扫描–context:component-scan base-packagecn.kgc.spring05/context:component-scan!–开启aspectj自动代理–aop:aspectj-autoproxy/aop:aspectj-autoproxy /beans6、测试 package cn.kgc.spring05.advice;import cn.kgc.spring05.entity.Teacher; import junit.framework.TestCase; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;RunWith(SpringJUnit4ClassRunner.class) ContextConfiguration(classpath:spring-config3.xml) public class AllAdviceTest{AutowiredTeacher teacher;Testpublic void test01() {System.out.println(teacher.getClass());String s teacher.teachOnLine(html);System.out.println(s s);} }7、运行效果 码文不易本篇文章就介绍到这里如果想要学习更多Java系列知识点击关注博主博主带你零基础学习Java知识。与此同时对于日常生活有困扰的朋友欢迎阅读我的第四栏目《国学周更—心性养成之路》学习技术的同时我们也注重了心性的养成。