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

广州公司注册代理机构哪家好seo教程 百度网盘

广州公司注册代理机构哪家好,seo教程 百度网盘,电子政务门户网站建设的教训,牡丹江信息网0453招聘信息网01.AOP技术是对于面向对象编程(OOP)的补充。是按照OCP原则进行的编写,(ocp是修改模块权限不行,扩充可以) 02.写一个例子: 创建一个新的java项目,在main主启动类中,写如下代码。 package com.co…

01.AOP技术是对于面向对象编程(OOP)的补充。是按照OCP原则进行的编写,(ocp是修改模块权限不行,扩充可以)
02.写一个例子:
创建一个新的java项目,在main主启动类中,写如下代码。

package com.company;interface mainService{void send();
}
class DefaultServiceImpl implements mainService
{@Overridepublic void send() {System.out.println("hello");}
}
public class main {public static void main(String[] args){DefaultServiceImpl defaultService=new DefaultServiceImpl();defaultService.send();}
}

一个接口,一个接口实现类,一个main主方法。

03.如果要实现显示接口实现类中的send方法运行的时间,一般的就在实现类中的send方法前后添加system.currenttimeMills

 @Overridepublic void send() {System.out.println("start:"+System.currentTimeMillis());System.out.println("hello");System.out.println("end:"+System.currentTimeMillis());}

04.如果在项目的发布后,或者以后接口实现类以后代码多,如何去修改项目呢?实现显示运行时间呢?
方法一:继续写一个子类去继承接口实现类。

class DefaultServiceImpl implements mainService
{@Overridepublic void send() {// System.out.println("start:"+System.currentTimeMillis());System.out.println("hello");//  System.out.println("end:"+System.currentTimeMillis());}
}class logDefaultImpl extends DefaultServiceImpl
{@Overridepublic void send() {System.out.println("start:"+System.currentTimeMillis());System.out.println("hello");System.out.println("end:"+System.currentTimeMillis());}
}
package com.company;interface mainService{void send();
}
class DefaultServiceImpl implements mainService
{@Overridepublic void send() {// System.out.println("start:"+System.currentTimeMillis());System.out.println("hello");//  System.out.println("end:"+System.currentTimeMillis());}
}class logDefaultImpl extends DefaultServiceImpl
{@Overridepublic void send() {System.out.println("start:"+System.currentTimeMillis());System.out.println("hello");System.out.println("end:"+System.currentTimeMillis());}
}
public class main {public static void main(String[] args){mainService logDefaultImpl=new logDefaultImpl();logDefaultImpl.send();}
}

方法二:如果接口实现类被final修饰的话,不能用子类来继承,可以写一个集合来实现运行时间的功能

final class DefaultServiceImpl implements mainService
{@Overridepublic void send() {System.out.println("hello");}
}

创建一个新的类,也同样实现接口mainService:
在这个类中声明一个接口的变量:

class logDefaultImpl implements mainService
{private mainService mainservice;public logDefaultImpl(mainService mainservice){this.mainservice=mainservice;}@Overridepublic void send() {System.out.println("start:"+System.currentTimeMillis());mainservice.send();System.out.println("end:"+System.currentTimeMillis());}
}

在main主方法中:

public class main {public static void main(String[] args){mainService DefaultServiceImpl=new DefaultServiceImpl();DefaultServiceImpl.send();mainService log=new logDefaultImpl(new DefaultServiceImpl());log.send();}
}

通过构造函数,将final修饰的接口实现类传入到新的类中,结合方法,来实现显示运行时间的功能。

05.AOP技术的底层实现。
AOP对象是通过代理对象来实现的,代理对象有两种,一种是通过JDK来实现的,一种是通过CGlib来实现的。
在这里插入图片描述
jdk的代理就好像是使用一个新的类去继承接口,再来包含目标接口实现类
cglib是写一个子类去继承目标接口实现类。

06.AOP的术语
1切面:就是写了相关扩展功能的类
2.通知:就是切面中的相关方法
3.连接点:就是需要扩展的方法
4.切入点:就是连接点所在的类,有的时候也可能是一整个包。
在这里插入图片描述
07.在springboot中去实现AOP技术
先在maven项目中导入相关的依赖。
在这里插入图片描述

 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId>     </dependency>

在去写切面类,这里需要用注解@Aspect标识这个类是切面类,用@Component来将类交给spring容器进行管理。还需要使用到log4j来进行日志管理@Slf4j。

package com.cy.common;import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Slf4j//日志
@Aspect//标识切面类
@Component//交给spring容器管理
public class sysLogAspect {@Pointcut("bean(sysUserServiceImpl)")//@Pointcut标识连接点(多个切入点的集合)public void logPointCut(){} @Around("logPointCut()")//这个是环绕通知,属性是切入点public Object around(ProceedingJoinPoint joinPoint) throws Throwable {//还是计算运行时间//并且执行方法Long startTime=System.currentTimeMillis();Object object= joinPoint.proceed();//调用本切面的其他方法或者其他切面的通知和目标Long endTime=System.currentTimeMillis();log.info("总时长是:",endTime-startTime);return object;}}

@Pointcut标识连接点(多个切入点的集合),这里用来写的是连接点bean标识spring容器中的类,括号中的是类名,一般是接口的实现类impl。

这个切面的意义在于sysUserServiceImpl这个接口实现类的每一个方法都扩展了功能,记录运行时间。

07.在springboot项目导入AOP依赖后,项目实现路径发送了改变。
springboot版本2.x后,默认AOP代理是Cglib
运行:
在这里插入图片描述

AOP通知有五种:
在这里插入图片描述

package com.cy.common;import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;@Slf4j
@Aspect
@Component
public class sysTimeAspect {@Pointcut("bean(sysUserServiceImpl)")public void doTime(){}@Around("doTime()")public Object aroud(ProceedingJoinPoint joinPoint) throws Throwable {Long startTime=System.currentTimeMillis();Object object= joinPoint.proceed();Long endTime=System.currentTimeMillis();log.info("总时长是:",endTime-startTime);System.out.println("通知around");return object;}//前置@Before("doTime()")public void before()  {System.out.println("通知before");}//后置@After("doTime()")public void after()  {System.out.println("通知before");}//正常@AfterReturning("doTime()")public void AfterReturn()  {System.out.println("通知AfterReturning");}//异常@AfterThrowing("doTime()")public void AfterThrow()  {System.out.println("通知AfterThrowing");}
}

使用通知@AfterThrowing,在切面中去写一个异常通知,就是目标接口类方法运行时候有异常,切面类处理。

@Slf4j
@Aspect
@Component
public class sysExceptionAspect {@AfterThrowing(pointcut = "bean(sysUserServiceImpl)",throwing = "e")//pointcut是连接点,throwing是抛出的异常public void doHandlerException(JoinPoint jp//这个是切入点,Throwable e){MethodSignature ms= (MethodSignature)jp.getSignature();log.error("{},exception msg is {}",ms.getName(),e.getMessage());}}

getsignature返回的类型是signature。
在这里插入图片描述

如果想要所有的接口实现类的运行方法报错时候有这个切面的类的AfterThrowing来处理异常,可以在bean中去写bean(*ServiceImpl)

http://www.ds6.com.cn/news/117977.html

相关文章:

  • html5单页面网站上海百度推广电话客服
  • 网站建立步骤怎样制作网页新手自学入门
  • 中国国家城乡建设委员会网站不屏蔽的国外搜索引擎
  • 网络推广竞价开户一个企业seo网站的优化流程
  • 浙江中钦建设有限公司网站今天热搜前十名
  • 做的网站怎么上传艾滋病多久能检查出来
  • 大连网站制作的公司哪家好荆门网络推广
  • Python做网站难不难网站维护需要学什么
  • 手机做的兼职网站丁的老头seo博客
  • wordpress企业站爱主题百度推广关键词越多越好吗
  • 哪个网站可以做担保交易常德政府网站
  • 制作企业网站首页怎么做推广小程序拿佣金
  • 怎么把网站提交给搜索引擎百度一下首页百度
  • 智能建站是什么沈阳网站推广优化
  • 云南建设学院的网站东莞百度seo新网站快速排名
  • 如何做淘宝联盟网站的推广营销策略有哪些内容
  • 中国建设企业银行官网站企业网站制作需要多少钱
  • wordpress jetpack 慢太原网站制作优化seo
  • php网站打开一片空白培训课程安排
  • 用jsp做网站信息推广平台
  • 重生做明星那个网站下载怎样在百度上注册自己的店铺
  • 运动器材网站建设西点培训
  • 网站维护的基本内容有哪些北京网络优化推广公司
  • 重庆网站推广网络推广惠州seo公司
  • 网页设计与制作教程这本书seo是什么地方
  • 做动态图的网站企业管理培训
  • 农业公司怎样建立网站个人永久免费自助建站
  • 个人网站的建设bt磁力猪
  • 庞各庄网站开发公司公司官网怎么做
  • 自己做视频会员网站宁波网络推广平台