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

十堰网站制作wordpress手机怎么使用

十堰网站制作,wordpress手机怎么使用,网站维护经费,高清免费爱做网站前言 接触过Spring的都知道,aop是其中重要的特性之一。笔者在开发做项目中,aop更多地是要和注解搭配:在某些方法上加上自定义注解,然后要对这些方法进行增强(很少用execution指定,哪些包下的哪些方法要增强)。那这时就…

前言

  接触过Spring的都知道,aop是其中重要的特性之一。笔者在开发做项目中,aop更多地是要和注解搭配:在某些方法上加上自定义注解,然后要对这些方法进行增强(很少用execution指定,哪些包下的哪些方法要增强)。那这时就要引出@annotation、@target、@within了。我们一一讲解。

@annotation

  方法上是否有指定注解;子类调用不重写的方法会被aop拦截,调用重写的方法看是否加了指定注解。


  首先引入依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId><version>2.7.4</version>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>2.7.4</version><scope>test</scope>
</dependency>

  自定义一个注解:

import java.lang.annotation.Target;
import java.lang.annotation.*;@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Outer {int limit() default 0;}

  目标类:

import org.springframework.stereotype.Component;@Component
public class Target {@Outer(limit = 8)public void invoke() {System.out.println("执行Target的方法");}}
@Component
public class SonTarget extends Target {}

  切面类:

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;@Component
@Aspect
public class MyAspect {@Around("@annotation(com.gs.spring_boot_demo.aop.Outer)")public Object around(ProceedingJoinPoint point) throws Throwable {Method method = ((MethodSignature)point.getSignature()).getMethod();Outer outer = method.getAnnotation(Outer.class);System.out.println("aop前置:" + outer.limit());return point.proceed();}}

  编写测试类:

import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.junit.jupiter.api.Test;@SpringBootTest
public class AopTest {@Resourceprivate Target target;@Autowiredprivate SonTarget sonTarget;@Testpublic void aop() {target.invoke();System.out.println("---");sonTarget.invoke();}}

  运行aop方法,打印结果:

在这里插入图片描述


  把子类SonTarget修改一下,

import org.springframework.stereotype.Component;@Component
public class SonTarget extends Target {public void invoke() {System.out.println("子类执行Target的方法");}}

  再次运行测试类,这时子类的invoke()方法不会被拦截了:

在这里插入图片描述

  当然,如果SonTarget的invoke()方法上加上@Outer,那就能被aop拦截了。


@target

  调用方法的对象,所属的类上是否有指定注解;注解被@Inherited修饰,子类调用会生效;无@Inherited,看子类上有无该注解。


  自定义注解不动,目标类修改为:
import org.springframework.stereotype.Component;@Component
@Outer(limit = 8)
public class Target {public void invoke() {System.out.println("执行Target的方法");}}
import org.springframework.stereotype.Component;@Component
public class SonTarget extends Target {public void invoke() {System.out.println("子类执行Target的方法");}}

  切面类修改为:

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;@Component
@Aspect
public class MyAspect {/*** 要注意一下,@target很硬霸:所有的bean都会被动态代理(不管类上有没有加自定* 义注解),所以要约束为:本项目下的包下* 不然测试用例运行时会报错:依赖中有些类是final的,被动态代理会报错*/@Around("@target(com.gs.spring_boot_demo.aop.Outer) && within(com.gs.spring_boot_demo..*)")public void around(ProceedingJoinPoint point) throws Throwable {Method method = ((MethodSignature)point.getSignature()).getMethod();Outer outer = method.getDeclaringClass().getAnnotation(Outer.class);System.out.println("aop前置:" + outer.limit());point.proceed();}}

  测试类不动,运行:

在这里插入图片描述

  SonTarget的invoke()没有被拦截,想要被拦截,就在SonTarget类上添加@Outer;或者自定义注解上增加@Inherited(表明父类加上该注解后,子类能够继承):

import java.lang.annotation.Target;
import java.lang.annotation.*;@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface Outer {int limit() default 0;}

@within

  方法所属的类上,是否有指定注解;注解没有被@Inherited修饰,子类调用不重写的方法会被拦截,调用重写的方法看子类上是否有注解;注解被@Inherited修饰,子类调用方法都会被拦截,不管是否重写

  自定义注解改一下,就把修饰它的@Inherited去掉;
  目标类:

import org.springframework.stereotype.Component;@Component
@Outer(limit = 8)
public class Target {public void invoke() {System.out.println("执行Target的方法");}}
import org.springframework.stereotype.Component;@Component
public class SonTarget extends Target {}

  切面类修改为:

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;@Component
@Aspect
public class MyAspect {@Around("@within(com.gs.spring_boot_demo.aop.Outer)")public void around(ProceedingJoinPoint point) throws Throwable {Method method = ((MethodSignature)point.getSignature()).getMethod();Outer outer = method.getDeclaringClass().getAnnotation(Outer.class);System.out.println("aop前置:" + outer.limit());point.proceed();}}

  测试类不动,运行:

在这里插入图片描述

  子类的方法能被拦截;我们把子类的方法重写一下:

import org.springframework.stereotype.Component;@Component
public class SonTarget extends Target {public void invoke() {System.out.println("子类执行Target的方法");}}

  再次运行测试类,打印出结果:

在这里插入图片描述

  子类的方法没有被拦截,想要被拦截,SonTarget类上加上@Outer。

  我们再试一下自定义注解被@Inherited修饰的情况。@Outer注解加上@Inherited,然后Target不动,SonTarget也不动(重写了invoke()方法,类上也没有@Outer),运行测试类:

在这里插入图片描述

  SonTaget改一下,不重写invoke()方法,运行测试类:

在这里插入图片描述

http://www.yayakq.cn/news/864729/

相关文章:

  • 适合夫妻二人观看的电视剧成都seo
  • wordpress淘客采集苏州优化有限公司
  • 泉州网站seo公司网站维护更新费用
  • 西昌建设招聘信息网站wordpress 加载
  • 找网站推广济南网站建设首选传承网络
  • 网站是陕西省城乡建设综合服务中心化妆品购物网站建设目的
  • 河北专业做网站天津百度推广排名
  • 桂林旅游网站制作ICP备案网站服务内容
  • 深圳市做网站知名公司有哪些网站管理助手创建数据库
  • 深圳营销网站dede title 我的网站
  • 国外饮品网站铁岭网站开发
  • 网站建设哪家公司好成都网站建设logo在线设计标小智
  • 网站html代码做商城网站应该用什么程序
  • 东莞 传媒 网站建设易语言登录WordPress账号
  • 做网站用源码上海市城市建设工程学校网站
  • c2c网站都有哪些如何推进网站建设
  • 电子商务网站建设规划门户网站建设意见
  • 东莞建设小学网站社交网站开发客户
  • 怎么查网站备案号秦淮区建设局网站
  • 视频直播源码刷seo快速排名
  • 做网站赚钱吗?遂宁网站seo
  • 横栏建设网站网站建设项目规划书案例分析
  • 国内餐饮设计网站建设博客建站系统
  • 景区网站怎么做网页版微信二维码登录
  • 毕节建设厅网站网站建设课结课感受
  • 个人网站 做外贸寻花问柳-专注做一家男人的网站猪
  • 服务器2003怎么做网站郑州电商网站设计
  • 宁波品牌网站制作哪家好app开发与网站建设
  • 东莞网站建设收费seo课程培训视频
  • 哪个网站服务器比较好中小公司做网站