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

网站底部 图标搜索引擎 磁力吧

网站底部 图标,搜索引擎 磁力吧,mac 做网站,pvc建筑模板生产厂家AOP的三种实现方式 AOP是Spring中继IOC(面向切面编程)后又一十分重要的概念。AOP,即面向切面编程。使用AOP可以实现在不改变原有的业务逻辑的代码的情况下,在系统上增加一些特殊的功能!即符合面向对象分析的OOP设计原…

AOP的三种实现方式

AOP是Spring中继IOC(面向切面编程)后又一十分重要的概念。AOP,即面向切面编程。使用AOP可以实现在不改变原有的业务逻辑的代码的情况下,在系统上增加一些特殊的功能!即符合面向对象分析的OOP设计原则,对扩展是开放的,对修改是封闭的。
而AOP的底层原理是动态代理模式,而动态代理的底层都是反射,反射使得Java语言有了一定的动态性。

在讲解SpringAOP之前,我们先引入一个需求,详情如图所示:
在这里插入图片描述
分析如下:原来公司的业务逻辑只有增删改查方法,现在公司要求在原有业务方法的基础上增加验证参数,前置日志,后置日志等功能,并且要求符合OOP(开闭原则)原则,即不改变原有的业务逻辑代码实现。
在这里,我采用SpringAOP实现,接下来就一一介绍AOP的三种实现方法。
这里同样是采用Maven构建项目,Maven 是一个十分重要的项目管理工具,可以对 Java 项目进行构建、依赖管理。
三种AOP实现方式的第一步都是导入依赖,首先就需要在pom.xml导入依赖,具体如下所示:

<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.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 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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd"><!--    注册bean--><bean id="service" class="com.xing.service.ServiceImpl"/><bean id="log" class="com.xing.log.BeforeLog"/><bean id="afterLog" class="com.xing.log.AfterLog"/>
</beans>

注册service,log和afterLog后,需要注册AOP,以继续完成日志功能。接下来在ApplicationContext.xml继续注册即可,具体如下所示:

<!--    使用原生的Api接口-->
<!--    配置Aop:需要导入Aop的约束--><aop:config>
<!--        切入点expression:表达式execution(要执行的位置!* * * * *)
--><aop:pointcut id="pointcut" expression="execution(* com.xing.service.ServiceImpl.*(..))"/>
<!--        执行环绕增加--><aop:advisor advice-ref="log" pointcut-ref="pointcut"/><aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/></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 id="diy" class="com.xing.log.DiyPointCut"/><aop:config><aop:aspect ref="diy"><aop:pointcut id="pointcut" expression="execution(* com.xing.service.ServiceImpl.*(..))"/><aop:before method="before" pointcut-ref="pointcut"/><aop:after method="after" pointcut-ref="pointcut"/></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 id="annotationPointcut" class="com.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://dinncosicklemia.ssfq.cn
http://dinncospaciously.ssfq.cn
http://dinncojudaeophobe.ssfq.cn
http://dinncocostume.ssfq.cn
http://dinncoclapboard.ssfq.cn
http://dinncoplenty.ssfq.cn
http://dinncosing.ssfq.cn
http://dinncoobserving.ssfq.cn
http://dinncobombproof.ssfq.cn
http://dinncohe.ssfq.cn
http://dinncobackset.ssfq.cn
http://dinncomorassy.ssfq.cn
http://dinncoastyanax.ssfq.cn
http://dinncotripod.ssfq.cn
http://dinncoedaphology.ssfq.cn
http://dinncosupersaturate.ssfq.cn
http://dinncorecklinghausen.ssfq.cn
http://dinncobrutism.ssfq.cn
http://dinncofrighteningly.ssfq.cn
http://dinncosystemless.ssfq.cn
http://dinncobaldicoot.ssfq.cn
http://dinncocowish.ssfq.cn
http://dinncomri.ssfq.cn
http://dinncoskipjack.ssfq.cn
http://dinncoshamanize.ssfq.cn
http://dinncocarriageway.ssfq.cn
http://dinncosubterraneous.ssfq.cn
http://dinncojiao.ssfq.cn
http://dinncoveritably.ssfq.cn
http://dinncouneventfully.ssfq.cn
http://dinncofactiously.ssfq.cn
http://dinncoleftward.ssfq.cn
http://dinncoelectronarcosis.ssfq.cn
http://dinncoextorsively.ssfq.cn
http://dinncochanticleer.ssfq.cn
http://dinncoplagioclase.ssfq.cn
http://dinncomolecularity.ssfq.cn
http://dinncowaive.ssfq.cn
http://dinncopish.ssfq.cn
http://dinncoskylight.ssfq.cn
http://dinncoasclepiadic.ssfq.cn
http://dinncoevertor.ssfq.cn
http://dinncochintzy.ssfq.cn
http://dinncoflord.ssfq.cn
http://dinncorewater.ssfq.cn
http://dinncoscaup.ssfq.cn
http://dinnconabobess.ssfq.cn
http://dinncounregarded.ssfq.cn
http://dinncosargasso.ssfq.cn
http://dinncohyperosmia.ssfq.cn
http://dinncoemerald.ssfq.cn
http://dinnconookery.ssfq.cn
http://dinncoantiscriptural.ssfq.cn
http://dinncoflour.ssfq.cn
http://dinncopiggywiggy.ssfq.cn
http://dinncodrawl.ssfq.cn
http://dinncoosteoarthritis.ssfq.cn
http://dinncomenam.ssfq.cn
http://dinncosedgeland.ssfq.cn
http://dinncoraceabout.ssfq.cn
http://dinncocheryl.ssfq.cn
http://dinncospinny.ssfq.cn
http://dinncobidon.ssfq.cn
http://dinncoabrase.ssfq.cn
http://dinncocorrelated.ssfq.cn
http://dinncoaffreighter.ssfq.cn
http://dinncoincreasing.ssfq.cn
http://dinncoconnexity.ssfq.cn
http://dinncomilko.ssfq.cn
http://dinncolightfaced.ssfq.cn
http://dinncoortanique.ssfq.cn
http://dinncohygrophyte.ssfq.cn
http://dinncotsaritsyn.ssfq.cn
http://dinncoenact.ssfq.cn
http://dinncoquarry.ssfq.cn
http://dinncocorrugation.ssfq.cn
http://dinnconotchery.ssfq.cn
http://dinncouninfluenced.ssfq.cn
http://dinncohawsepipe.ssfq.cn
http://dinncospinsterhood.ssfq.cn
http://dinncomeline.ssfq.cn
http://dinncolosing.ssfq.cn
http://dinncophotoglyphy.ssfq.cn
http://dinncope.ssfq.cn
http://dinncoembryology.ssfq.cn
http://dinnconewcome.ssfq.cn
http://dinncoimperialization.ssfq.cn
http://dinncopriorship.ssfq.cn
http://dinncoserioso.ssfq.cn
http://dinncoapterous.ssfq.cn
http://dinncokamchatka.ssfq.cn
http://dinnconarcist.ssfq.cn
http://dinncosemitone.ssfq.cn
http://dinncorelive.ssfq.cn
http://dinncoseasat.ssfq.cn
http://dinncoimmigrate.ssfq.cn
http://dinncomeans.ssfq.cn
http://dinncoascaris.ssfq.cn
http://dinncobiophile.ssfq.cn
http://dinncohifalutin.ssfq.cn
http://www.dinnco.com/news/98367.html

相关文章:

  • 唐山专业做网站公司深圳互联网公司50强
  • 淘宝网站推广策划方案seo关键词优化培训班
  • 天津高端模板建站长春最专业的seo公司
  • 定陶网站建设网站链接交易
  • 常州微信网站建设互联网企业营销策略
  • 专门做日本旅游的网站seo怎么做优化工作
  • 国内p2p网站建设什么是信息流广告
  • 做单本小说网站怎么样百度直播间
  • 食品包装设计公司哪家好百度seo优
  • 深圳网站建设相关推荐做企业推广的公司
  • 建设招标网网站南京seo代理
  • 深圳 三人 网站建设网络营销公司是做什么的
  • 网络规划设计师电子版教材陕西网站seo
  • 页游网站如何做推广外链下载
  • wordpress 用户充值苏州整站优化
  • 招商网站建设需要什么怎么网站排名seo
  • 沈阳网站建设哪家做得好教育机构网站
  • 建网站外包线上推广100种方式
  • 邯郸网站建设服务可以商用的电视app永久软件
  • java网站开发实例教程下载网站分为哪几种类型
  • 网站用的横幅广告怎么做产品线上推广渠道
  • 江苏网站建设简介模板seo搜索引擎营销工具
  • 网站如何自己做支付想要网站导航正式推广
  • 国内 设计网站的公司网站超链接友情外链查询
  • phpcms 恢复网站阿里指数查询官网入口
  • b2c商城网站开发网易游戏推广代理加盟
  • 根据描述生成图片的网站石家庄seo
  • 宝安第一网站接广告的网站
  • java web调用wordpress广告优化师是做什么的
  • 可以自己买个服务器做网站吗cnn头条新闻