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

主机屋网站北京朝林建设集团网站

主机屋网站,北京朝林建设集团网站,百度竞价点击软件,百度灰色关键词代发SpringAOP是什么#xff1f;为什么要有SpringAOP#xff1f; 原文#xff1a;SpringAOP是什么#xff1f;为什么要有SpringAOP#xff1f; 一、有SpringAOP之前 简单的开发场景#xff0c;只需要写简单的业务逻辑#xff0c;比如CRUD 但是在执行具体的逻辑之前…SpringAOP是什么为什么要有SpringAOP 原文SpringAOP是什么为什么要有SpringAOP 一、有SpringAOP之前 简单的开发场景只需要写简单的业务逻辑比如CRUD 但是在执行具体的逻辑之前需要进行权限校验或者在具体的逻辑之后需要日志记录呢 这样就增加了很多代码而且增加的这些代码都是差不多的 既然如此那我们抽出来吧在这个类里面写一个私有的方法。 代码少了很多但是如果其他的类也需要用到权限校验和日志记录呢 难道也要在其他类里面写私有方法吗这也太麻烦了。 为了解决这个问题有两种方法。 第一种将这个私有方法抽出来不就好了我直接写两个工具类一个是权限校验一个是日志记录谁用谁调用。 第二种我直接搞一个父类谁用我就让它继承这个父类这样就能直接调用父类的方法。 但是不论是第一种还是第二种其实都会侵入业务类的方法逻辑那么有没有一种方法能在没有对业务核心代码侵入性的前提下给业务核心代码添加额外的功能呢 这时候AOP就出来了也就是所谓的面向切面编程。 首先来看一下AOP的概念 切入点 想额外添加功能的方法 切面 权限检查、日志记录等这些增强逻辑的方法在的类 通知 在目标方法运行的什么时机来执行某个增强逻辑 目标方法前还是目标方法后 切入点表达式 规定增强逻辑需要去增强什么方法 二、有SpringAOP之后 使用方法 maven依赖 dependencies!--导入Spring Context核心依赖--dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.3.23/version/dependency!--Spring AOP--dependencygroupIdorg.springframework/groupIdartifactIdspring-aspects/artifactIdversion5.3.1/version/dependency!--Lombok--dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.30/version/dependency!--Junit单元测试--dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.13.2/versionscopetest/scope/dependency/dependencies切入点 package com.moon.aop.bean;/*** Author moon* Date 2023/10/15 15:55* Description 切入点*/ public class UserService {public void addUser() {System.out.println(添加用户...);}public void queryUser() {System.out.println(查询用户...);}public void editUser() {System.out.println(编辑用户...);}public void deleteUser() {System.out.println(删除用户...);} } 切面 package com.moon.aop.bean;/* * 底层依赖Aspect的注解【但是原理不同】 * */ import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut;/*** Author moon* Date 2023/10/15 15:58* Description 切面里面放的增强逻辑*/Aspect public class UserAspect {/** 这就是传说中上文所谓的切入点表达式* */Pointcut(value execution(* com.moon.aop.bean.UserService.*(..)))public void point() {}/** 这就是增强逻辑* Before、After就是所谓的通知* */Before(value point())public void beforeRun() {System.out.println(在目标方法执行前开始执行);}After(value point())public void afterRun() {System.out.println(在目标方法执行后开始执行);} } 配置类用来初始化和加载Spring的IOC容器 package com.moon.aop.bean;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy;/*** Author moon* Date 2023/10/15 16:07* Description 用该类来初始化和加载IOC容器*/Configuration public class SpringConfig {Beanpublic UserAspect userAspect() {return new UserAspect();}Beanpublic UserService userService() {return new UserService();} } Junit单元测试 import com.moon.aop.bean.SpringConfig; import com.moon.aop.bean.UserService; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext;/*** Author moon* Date 2023/10/15 16:32* Description*/ public class AOPTest {Testpublic void test01() {/** 通过配置类初始化IOC容器* */AnnotationConfigApplicationContext context new AnnotationConfigApplicationContext(SpringConfig.class);/** 获取目标对象Bean* */UserService userService context.getBean(UserService.class);userService.addUser();} } AOP并没有生效 此时userService.getClass().getName()的值为 那是因为Spring并没有开启AOP需要我们手动开启 package com.moon.aop.bean;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy;/*** Author moon* Date 2023/10/15 16:07* Description 用该类来初始化和加载IOC容器*/Configuration /* * 手动开启AOP功能 * */ EnableAspectJAutoProxy public class SpringConfig {Beanpublic UserAspect userAspect() {return new UserAspect();}Beanpublic UserService userService() {return new UserService();} } 成功 此时userService.getClass().getName()的值为
http://www.huolong8.cn/news/358231/

相关文章:

  • 泰安市网站建设免费下载小程序
  • 什么是网站建设需求企业站模板大全
  • 电子商务网站建设与管理第二版答案什么公司做网站最好
  • 建设织梦网站模板做外贸英文网站
  • 公司名被注册网站网站产品介绍页面的布局方案
  • 怎么将自己做的网站上线郑州一建南阳分公司
  • 潍坊制作网站公司佛山网站建设机构
  • 微商手机网站制作关键词林俊杰mp3在线听
  • 怎么制作个人门户网站违禁网站用什么浏览器
  • 郑州企业微网站建设一般使用的分辨率是多少
  • 青岛做网站建设多少钱网站设计制作策划书
  • 最好网页游戏网站推广网站有哪些方式
  • 高质量的南京网站建设企业招聘网站
  • 桂林论坛网网站电话hk域名哪里注册
  • 桃子网站logo企业网站域名注册
  • html5的网站设计与实现是做什么云南建设人力资源网站
  • 网站flash素材用wordpress做答题网站
  • 如何看别人网站用什么做的设计素材网站那个好
  • wordpress多站残疾人信息无障碍网站建设
  • 网站制作 南京安徽住房和城乡建设厅网站
  • 辽宁网站建设招标广州手机网站建设公司
  • 查找南宁网站开发公司福州专业网站开发很专业
  • 天津本地网站网站没备案能访问吗
  • 网站类软文福州网站建设推进
  • 网页设计与网站建设分析刚刚发布了天津的最新消息
  • 网站域名的作用是什么意思备案博客域名做视频网站会怎么样
  • 求一个自己做的网站链接怎样联系自己建设网站
  • 网站风格变化沈阳seo合作
  • 可以做试卷并批改的网站215做网站
  • 网站开发毕业设计收获与体会网站公司建设网站收费模块