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

本周新闻热点10条如何优化关键词排名快速首页

本周新闻热点10条,如何优化关键词排名快速首页,中国建设银行网站u盾修改密码,网站建设和网站优化的区别AOP的三种实现方式 AOP是Spring中继IOC#xff08;面向切面编程#xff09;后又一十分重要的概念。AOP#xff0c;即面向切面编程。使用AOP可以实现在不改变原有的业务逻辑的代码的情况下#xff0c;在系统上增加一些特殊的功能#xff01;即符合面向对象分析的OOP设计原…AOP的三种实现方式 AOP是Spring中继IOC面向切面编程后又一十分重要的概念。AOP即面向切面编程。使用AOP可以实现在不改变原有的业务逻辑的代码的情况下在系统上增加一些特殊的功能即符合面向对象分析的OOP设计原则对扩展是开放的对修改是封闭的。 而AOP的底层原理是动态代理模式而动态代理的底层都是反射反射使得Java语言有了一定的动态性。 在讲解SpringAOP之前我们先引入一个需求详情如图所示 分析如下原来公司的业务逻辑只有增删改查方法现在公司要求在原有业务方法的基础上增加验证参数前置日志后置日志等功能并且要求符合OOP开闭原则原则即不改变原有的业务逻辑代码实现。 在这里我采用SpringAOP实现接下来就一一介绍AOP的三种实现方法。 这里同样是采用Maven构建项目Maven 是一个十分重要的项目管理工具可以对 Java 项目进行构建、依赖管理。 三种AOP实现方式的第一步都是导入依赖首先就需要在pom.xml导入依赖具体如下所示 dependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-aop/artifactIdversion5.2.5.RELEASE/version/dependencydependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactIdversion1.8.13/version/dependency/dependencies导入依赖后还需要搭建环境即编写公司业务接口Service和接口实现类ServiceImpl。 接口编写如下所示 package com.xing.service;public interface Service {public void add();//增加用户public void delete();//删除用户public void update();//修改用户public void query();//查询用户 } 接口实现类如下所示 package com.xing.service; public class ServiceImpl implements Service{Overridepublic void add() {System.out.println(增加了一个用户);}Overridepublic void delete() {System.out.println(删除了一个用户);}Overridepublic void update() {System.out.println(更新了一个用户);}Overridepublic void query() {System.out.println(查询了一个用户);} } 完成前两步后将采用不同的方式实现AOP切面编程。 方式一 使用SpringAPI接口 在SpringAOP中通过Advice定义横切逻辑Spring中支持5种类型的Advice具体如下图所示 具体的每一个接口的使用在代码中介绍。 创建一个日志软件包log在log中创建BeforeLog类并实现MethodBeforeAdvice接口和AfterLog类并实现AfterReturningAdvice接口具体的实现如下所示 BeforeLog.java package com.xing.log;import org.springframework.aop.MethodBeforeAdvice;import java.lang.reflect.Method;public class BeforeLog implements MethodBeforeAdvice {Overridepublic void before(Method method, Object[] args, Object target) throws Throwable {System.out.println(target.getClass().getName()的method.getName()被执行了);} }AfterLog.java package com.xing.log;import org.springframework.aop.AfterAdvice; import org.springframework.aop.AfterReturningAdvice;import java.lang.reflect.Method;public class AfterLog implements AfterReturningAdvice {Overridepublic void afterReturning(Object returnValue, Method method, Object[] objects, Object o1) throws Throwable {System.out.println(执行了method.getName()方法返回值为:returnValue);} }现在已经完成了两个很单纯的日志增强类一个是前置一个是后置完成后还并未实现AOP接下来我们要把这些类都注册到Spring中。 即在resources文件中创建ApplicationContext.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/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd!-- 注册bean--bean idservice classcom.xing.service.ServiceImpl/bean idlog classcom.xing.log.BeforeLog/bean idafterLog classcom.xing.log.AfterLog/ /beans注册service,log和afterLog后需要注册AOP以继续完成日志功能。接下来在ApplicationContext.xml继续注册即可具体如下所示 !-- 使用原生的Api接口-- !-- 配置Aop:需要导入Aop的约束--aop:config !-- 切入点expression:表达式execution(要执行的位置* * * * *) --aop:pointcut idpointcut expressionexecution(* com.xing.service.ServiceImpl.*(..))/ !-- 执行环绕增加--aop:advisor advice-reflog pointcut-refpointcut/aop:advisor advice-refafterLog pointcut-refpointcut//aop:config到此为止基于SpringAPI接口的实现AOP方式已完成测试代码由于三种AOP实现方式都一模一样所以留到最后展示。 方式二 自定义类实现 第一种方式是API接口第二种方式可以简单一点刚才的接口如果接口记不住或者接口找不到就没办法实现那么使用自定义类来实现。 即在log文件中创建DiyPointCut类在里面定义before前置日志方法和after后置日志方法具体的实现如下所示 package com.xing.log;public class DiyPointCut {public void before(){System.out.println(方法执行前);}public void after(){System.out.println(方法执行后);} }书写完自定义类后需要在ApplicationContext.xml中注册AOP具体的注册如下所示 !-- 方式二-- bean iddiy classcom.xing.log.DiyPointCut/aop:configaop:aspect refdiyaop:pointcut idpointcut expressionexecution(* com.xing.service.ServiceImpl.*(..))/aop:before methodbefore pointcut-refpointcut/aop:after methodafter pointcut-refpointcut//aop:aspect/aop:config简单分析一下aop:aspect ref“diy”是定义自定义类为切面aop:before method“before” pointcut-ref“pointcut”/是定义diy里面的before方法为前置日志aop:after method“after” pointcut-ref“pointcut”/定义diy里面的after方法为后置日志。 注册完成后测试即可查看效果 方式三 注解实现AOP 前面讲解了两种方式现在来介绍第三种方式这种方式采用注册实现注解实现其实就是用注解来替代之前的xml配置。 即可以用Aspect来标注类表示该类为一个切面用Before标注类中方法表示该方法为前置方法注解中的参数即为切入点的位置。 接下来在Log类中创建AnnotationPointCut用注解实现AOP具体如下所示 package com.xing.log;import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; //使用注解实现AOP Aspect//注解标注这个类是一个切面 public class AnnotationPointcut {Before(execution(* com.xing.service.ServiceImpl.*(..)))public void before(){System.out.println(方法执行前);}After(execution(* com.xing.service.ServiceImpl.*(..)))public void after(){System.out.println(方法执行后);} } 接下来需要在ApplicationContext.xml中注册AnnotationPointCut类以及开启注解支持具体如下所示 bean idannotationPointcut classcom.xing.log.AnnotationPointcut/aop:aspectj-autoproxy/三种方式都介绍完了最后来展示测试类即创建MyTest测试类具体如下所示 import com.xing.service.Service; import com.xing.service.ServiceImpl; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(applicationContext.xml);//动态代理代理的是接口Service service context.getBean(service, Service.class);service.add();} } 以上就是AOP的三种实现方式
http://www.hkea.cn/news/14320939/

相关文章:

  • 北京造价员变更在哪个网站做文字网页游戏
  • wordpress侧边栏位置杭州企业网站seo
  • 怎么在网站做推广和宣传比较好的设计网站推荐
  • 免费网站建设自助建站电脑系统优化软件
  • 北京旅游网站排名wordpress salient
  • 如何做考试网站隧道建设期刊网站进不去
  • 万州网站建设果园路wordpress 重定向函数
  • 佛山网站优化有网页的设计与应用的论文
  • 求网站2021在线观看营销品牌网站建设
  • 张店网站建设价格自己做的公司网站百度搜不到
  • 手机怎么免费建设网站软件开发流程示意图
  • 古玩网站源码西昌网站制作
  • 深圳微信分销网站建设兰州市科协网站
  • 海北北京网站建设WordPress主题虚拟资源
  • 网站和网业的关系一键优化
  • 企业网站建设网网站收款接口
  • 网站改版 文案怎么制作网站准考证在网上打印
  • 网站关键词搜索排名怎么做律师网站建设案例
  • 未满18岁能申请网站备案吗wordpress支持pdf
  • html个人网站策划书深圳怎么做网络推广软仿
  • 扶沟县建设局网站科技酒店
  • 嘉兴网络建站模板云南楚雄彝族自治州
  • 山东学生做自我评价的网站有没有做字的网站
  • linux 什么做网站好网页搜索框下记录删不掉
  • 浙江建筑网站西宁专业网站建设公司
  • 如何建立购物网站南宁网站建设 超博网络
  • php 企业网站模板 想1北京搬家公司哪家服务最好
  • 企业如何做网站建站榆林市建设局网站
  • 网站推广营销策划国家企业信息公示网查询官网网址
  • 一个完整网站制作的实例郑州网站建设企业