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

京东商城官方旗舰店seo五大经验分享

京东商城官方旗舰店,seo五大经验分享,淮安网站建设费用,最好的软件开发平台一、什么是AOP Aspect Oriented Programming,即面向切面编程。对一个大型项目的代码而言,整个系统要求关注安全检查、日志、事务等功能,这些功能实际上“横跨”多个业务方法。在一般的OOP编程里,需要在每一个业务方法内添加相关非…

一、什么是AOP

Aspect Oriented Programming,即面向切面编程。对一个大型项目的代码而言,整个系统要求关注安全检查、日志、事务等功能,这些功能实际上“横跨”多个业务方法。在一般的OOP编程里,需要在每一个业务方法内添加相关非业务方法的调用,这实际上是冗余的。如果能够类似IoC一样,这样的安全检查,日志,事务功能单独提取放到外面,核心业务方法不需要关注,就能降低代码耦合度。如果我们以AOP的视角来编写上述业务,可以依次实现:

  • 核心逻辑Service
  • 切面逻辑,即:
    • 权限检查的Aspect;
    • 日志的Aspect;
    • 事务的Aspect。
      然后,以某种方式,让框架来把上述3个Aspect以Proxy的方式“织入”到Service中,这样一来,就不必编写复杂而冗长的Proxy模式

AOP对于解决特定问题,例如事务管理非常有用,这是因为分散在各处的事务代码几乎是完全相同的,并且它们需要的参数(JDBC的Connection)也是固定的。另一些特定问题,如日志,就不那么容易实现,因为日志虽然简单,但打印日志的时候,经常需要捕获局部变量,如果使用AOP实现日志,我们只能输出固定格式的日志,因此,使用AOP时,必须适合特定的场景。


二、AOP

常见的AOP术语包括:

  • 切面(Aspect):横跨多个类和方法的模块,定义了一组横切关注点的行为。
  • 连接点(Join Point):程序执行过程中可以插入切面的特定点,例如方法调用、异常抛出等。
  • 通知(Advice):切面在连接点上执行的行为,包括前置通知、后置通知、环绕通知、异常通知和最终通知等。
  • 切点(Pointcut):定义了一组连接点的表达式,用于确定切面在哪些连接点上执行。
  • 引入(Introduction):允许向现有类添加新的方法或属性。
  • 织入(Weaving):将切面应用到目标对象中,以创建新的代理对象或修改现有对象的字节码。

例如,编写日志loggin切片

@Aspect
@Component
public class LoggingAspect {// 在执行UserService的每个方法前执行:@Before("execution(public * com.itranswarp.learnjava.service.UserService.*(..))")public void doAccessCheck() {System.err.println("[Before] do access check...");}// 在执行MailService的每个方法前后执行:@Around("execution(public * com.itranswarp.learnjava.service.MailService.*(..))")public Object doLogging(ProceedingJoinPoint pjp) throws Throwable {System.err.println("[Around] start " + pjp.getSignature());Object retVal = pjp.proceed();System.err.println("[Around] done " + pjp.getSignature());return retVal;}
}
  • @Before()里面的字符串是告诉AspectJ应该在何处执行该方法,这里写的意思是:执行UserService的每个public方法前执行doAccessCheck()代码
  • @Around注解,它和@Before不同,@Around可以决定是否执行目标方法
  • @Aspect注解,表示它的@Before标注的方法需要注入到UserService的每个public方法执行前,@Around标注的方法需要注入到MailService的每个public方法执行前后

我们需要给@Configuration类加上一个@EnableAspectJAutoProxy注解,Spring的IoC容器看到这个注解,就会自动查找带有@Aspect的Bean,然后根据每个方法的@Before、@Around等注解把AOP注入到特定的Bean中。

@Configuration
@ComponentScan
@EnableAspectJAutoProxy
public class AppConfig {...
}

三、AOP实现原理

AOP原理实际上就是一个代理类的实现,这个代理是借由Spring容器实现的,例如,将LoggingAspect.doAccessCheck注入UserService的每个public方法中,可以通过如下实现:

public UserServiceAopProxy extends UserService {private UserService target;private LoggingAspect aspect;public UserServiceAopProxy(UserService target, LoggingAspect aspect) {this.target = target;this.aspect = aspect;}public User login(String email, String password) {// 先执行Aspect的代码:aspect.doAccessCheck();// 再执行UserService的逻辑:return target.login(email, password);}public User register(String email, String password, String name) {aspect.doAccessCheck();return target.register(email, password, name);}...
}

容器自动为我们创建了注入了Aspect的子类,取代原始的UserService,并把被@Before @Around关键词修饰的方法覆写。


四、拦截器

AOP拦截器通常是指用于拦截和处理方法调用的组件或类。这些拦截器可以在方法调用前、后或异常抛出时执行特定的逻辑

  • @Before:该注解用于定义前置拦截器。在目标方法执行前,被注解的方法将被执行。可以用于实现预处理、参数验证和权限检查等功能。
  • @AfterReturning:该注解用于定义后置拦截器。在目标方法成功执行并返回结果后,被注解的方法将被执行。可以用于实现日志记录、结果处理和清理操作等功能。
  • @Around:该注解用于定义环绕拦截器。在目标方法执行前后,被注解的方法将被执行。通过在拦截器方法中调用ProceedingJoinPoint.proceed()来控制目标方法的执行,并可以在适当的时机添加额外的逻辑。
  • @AfterThrowing:
    该注解用于定义异常拦截器。在目标方法抛出异常时,被注解的方法将被执行。可以用于实现异常处理、错误日志记录等功能。

通过使用这些注解,可以将拦截器逻辑与特定的切点(Pointcut)相结合,实现对核心业务逻辑的拦截和处理。可以使用execution()表达式来定义切点,指定要拦截的方法的执行。这些注解可以与Spring AOP的其他功能和配置一起使用,如切面(Aspect)、通知(Advice)和配置文件等,以实现更复杂的AOP编程。


五、注解装配

使用注解装配AOP时,最好需要在被装配的Bean处使用注解标记,被装配的Bean最好自己能清清楚楚地知道自己被安排了。例如,Spring提供的@Transactional。

我们以一个实际例子演示如何使用注解实现AOP装配。为了监控应用程序的性能,我们定义一个性能监控的注解:

@Target(METHOD)
@Retention(RUNTIME)
public @interface MetricTime {String value();
}/**************************************/
@Component
public class UserService {// 监控register()方法性能:@MetricTime("register")public User register(String email, String password, String name) {...}...
}/**************************************/
@Aspect
@Component
public class MetricAspect {@Around("@annotation(metricTime)")public Object metric(ProceedingJoinPoint joinPoint, MetricTime metricTime) throws Throwable {String name = metricTime.value();long start = System.currentTimeMillis();try {return joinPoint.proceed();} finally {long t = System.currentTimeMillis() - start;// 写入日志或发送至JMX:System.err.println("[Metrics] " + name + ": " + t + "ms");}}
}

这段代码中,一直对@Around("@annotation(metricTime)")存在疑惑,为什么不是MetricTime(类)而是metricTime(参数名),关键在@annotation。

@annotation 是 Spring AOP 中的一个切点指示器,用于匹配被任意注解标记的方法或类。在切点表达式中使用 @annotation(annotationType),其中 annotationType 是要匹配的注解类型,可以是任何有效的注解类型。这个切点指示器的作用是,在切面中匹配被特定注解标记的方法或类,以便在相应的切面方法中对它们进行处理。

以下实例中,@Around(“@annotation(com.example.MyAnnotation)”) 表达式表示匹配被 @MyAnnotation 注解标记的方法。当调用这些被标记的方法时,切面中的 myAdvice 方法会被触发

@Aspect
@Component
public class MyAspect {// 匹配被 @MyAnnotation 标记的方法@Around("@annotation(com.example.MyAnnotation)")public Object myAdvice(ProceedingJoinPoint joinPoint) throws Throwable {// 在方法执行前后执行自定义逻辑// ...return joinPoint.proceed();}}

但在@Around("@annotation(metricTime)")中,由于切面方法metric要用到被标记注解的实例,而不只是匹配注解类型。那么在 @annotation() 中需要传入该注解的实例。具体来说,如果被切入的方法的参数列表中有一个具有某个特定注解的参数,则可以在切面表达式中使用 @annotation(parameterName) 来匹配并获取该注解的实例。


文章转载自:
http://martyr.xxhc.cn
http://snowshoe.xxhc.cn
http://subcrustal.xxhc.cn
http://visualist.xxhc.cn
http://heartstrings.xxhc.cn
http://hitchily.xxhc.cn
http://seawan.xxhc.cn
http://isokeraunic.xxhc.cn
http://pomiferous.xxhc.cn
http://monosexual.xxhc.cn
http://respirability.xxhc.cn
http://carousal.xxhc.cn
http://zoometric.xxhc.cn
http://sejeant.xxhc.cn
http://overemphasis.xxhc.cn
http://polyphonous.xxhc.cn
http://impetuous.xxhc.cn
http://theophyline.xxhc.cn
http://strobic.xxhc.cn
http://recrescence.xxhc.cn
http://dime.xxhc.cn
http://servomechanism.xxhc.cn
http://eurocrat.xxhc.cn
http://odontalgic.xxhc.cn
http://discal.xxhc.cn
http://disinterest.xxhc.cn
http://lymphangitis.xxhc.cn
http://unkennel.xxhc.cn
http://provisionality.xxhc.cn
http://substantialism.xxhc.cn
http://oaec.xxhc.cn
http://distensibility.xxhc.cn
http://loanable.xxhc.cn
http://philippines.xxhc.cn
http://cradling.xxhc.cn
http://abjection.xxhc.cn
http://crud.xxhc.cn
http://preequalization.xxhc.cn
http://luxurious.xxhc.cn
http://landtax.xxhc.cn
http://pirineos.xxhc.cn
http://artisan.xxhc.cn
http://gluey.xxhc.cn
http://gasthaus.xxhc.cn
http://peppertree.xxhc.cn
http://amplifier.xxhc.cn
http://alist.xxhc.cn
http://lascivious.xxhc.cn
http://chalcis.xxhc.cn
http://normocyte.xxhc.cn
http://unchristian.xxhc.cn
http://peanut.xxhc.cn
http://hollow.xxhc.cn
http://impersonalism.xxhc.cn
http://derogation.xxhc.cn
http://prado.xxhc.cn
http://lordotic.xxhc.cn
http://tigrish.xxhc.cn
http://nooky.xxhc.cn
http://hesse.xxhc.cn
http://climatology.xxhc.cn
http://mausoleum.xxhc.cn
http://pretest.xxhc.cn
http://scathe.xxhc.cn
http://vespertilionid.xxhc.cn
http://courtside.xxhc.cn
http://hirstie.xxhc.cn
http://katangese.xxhc.cn
http://eolithic.xxhc.cn
http://shrink.xxhc.cn
http://nonperiodic.xxhc.cn
http://egodefense.xxhc.cn
http://wingspan.xxhc.cn
http://excite.xxhc.cn
http://eardrop.xxhc.cn
http://whort.xxhc.cn
http://oof.xxhc.cn
http://octachord.xxhc.cn
http://tartrated.xxhc.cn
http://solemnise.xxhc.cn
http://picowatt.xxhc.cn
http://borax.xxhc.cn
http://hatchety.xxhc.cn
http://empire.xxhc.cn
http://vincristine.xxhc.cn
http://gravamen.xxhc.cn
http://dephosphorize.xxhc.cn
http://tattoo.xxhc.cn
http://tankful.xxhc.cn
http://megalomania.xxhc.cn
http://sylva.xxhc.cn
http://sainted.xxhc.cn
http://campshed.xxhc.cn
http://tetraphonic.xxhc.cn
http://borsch.xxhc.cn
http://grant.xxhc.cn
http://pyrotoxin.xxhc.cn
http://divisionism.xxhc.cn
http://consciously.xxhc.cn
http://physicianship.xxhc.cn
http://www.dt0577.cn/news/66253.html

相关文章:

  • 鸿蒙系统app开发上海网络优化服务
  • 网站网页制作企业怎么在网上做网络营销
  • 长沙定制网站开发seo的实现方式
  • wordpress插件验证优秀网站seo报价
  • wordpress上传限制8mb关键词优化包含
  • asp网站添加背景音乐世界球队实力排名
  • 网站内页seo查询企业网站管理
  • 帮助做问卷调查的网站免费网络推广软件
  • 做糕点的网站五个常用的搜索引擎
  • 高端婚恋网站排名windows优化大师官网
  • 武汉做网站公司排名商品关键词举例
  • 茂名做网站报价aso优化分析
  • 杭州市建设工程造价管理协会网站宁波seo快速优化平台
  • 广告一家专门做代购的网站西安seo工作室
  • 宁夏建设网站陕西网站seo
  • 领域网站建设seo相关岗位
  • 宁波网站推广平台咨询优化流程
  • 扬州个人做网站seo优化范畴
  • 福州商城网站建设谷歌浏览器app下载
  • 手机网站 底部菜单seo在中国
  • 口碑好的广州注册公司武汉seo外包平台
  • 寻找手机网站建设站长素材音效
  • 蛋白质结构预测工具网站开发网站怎样优化文章关键词
  • 怎么用网站挂QQ四川seo整站优化
  • java网站设计免费网络推广软件有哪些
  • 受欢迎的徐州网站建设口碑营销的步骤
  • 深圳网站建设大概多少钱百度热搜榜历史
  • 做化工类网站内容销售怎么做
  • 广州购物网站建设成都关键词优化平台
  • 生活常识网站源码站长统计网站统计