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

常州做的网站的公司推广网站源码

常州做的网站的公司,推广网站源码,平台建设包括哪些方面,无锡网站改版一、interface 关键字 我们想定义一个自己的注解 需要使用 interface 关键字来定义。 如定义一个叫 MyAnnotation 的注解: public interface MyAnnotation { } 二、元注解 光加上 interface 关键字 还不够,我们还需要了解5大元注解 RetentionTargetDo…

一、@interface 关键字

我们想定义一个自己的注解 需要使用 @interface 关键字来定义。
如定义一个叫 MyAnnotation 的注解:

public @interface MyAnnotation { }

 二、元注解

  光加上 @interface 关键字 还不够,我们还需要了解5大元注解

  • @Retention
  • @Target
  • @Documented
  • @Inherited(JDK8 引入)
  • @Repeatable(JDK8 引入)

 1)  @Retention 指定注解的生命周期    

@Retention(RetentionPolicy.SOURCE)

其中Retention是一个枚举类:

  1. RetentionPolicy.SOURCE : 注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃(.java文件)
  2. RetentionPolicy.CLASS :注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期(.class文件)
  3. RetentionPolicy.RUNTIME: 注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在(内存中的字节码)

2) @Target指定注解可以修饰的元素类型

@Target(ElementType.Field)
  1. ElementType.ANNOTATION_TYPE - 标记的注解可以应用于注解类型。
  2. ElementType.CONSTRUCTOR - 标记的注解可以应用于构造函数。
  3. ElementType.FIELD - 标记的注解可以应用于字段或属性。
  4. ElementType.LOCAL_VARIABLE - 标记的注解可以应用于局部变量。
  5. ElementType.METHOD - 标记的注解可以应用于方法。
  6. ElementType.PACKAGE - 标记的注解可以应用于包声明。
  7. ElementType.PARAMETER - 标记的注解可以应用于方法的参数。
  8. ElementType.TYPE - 标记的注解可以应用于类的任何元素。

 3)@Documented
指定注解会被JavaDoc工具提取成文档。默认情况下,JavaDoc是不包括文档的

 4)@Inherited
表示该注解会被子类继承,注意,仅针对类,成员属性、方法并不受此注释的影响。

 5)@Repeatable
表示注解可以重复使用,为了解决同一个注解不能重复在同一类/方法/属性上使用的问题。

        其中最常用的就是 @Retention 跟 @Target。

三、简单实现

        例如实现一个简单,在标记注解的地方打印一句日志。
        定义一个 MyAnnotation 注解,并且定义一个属性 message 默认值是 ”aaa“。先将该注解加到字段上,看能不能获取到。

//注解用于字段上
@Target(ElementType.FIELD)
//运行时使用
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {String message() default  "aaa";}

        定义一个Student类用于测试: 

@Data
public class Student {@JSONField(ordinal =0)@MyAnnotation(message = "AAAAAAAAA")public String name;@MyAnnotation(message = "AAAAAAAAA")public Integer score;}

  在字段上标注该注解,然后编写一个main方法获取该注解的属性:

    public static void main(String[] args) {Class<?> studentClass = Student.class;Field[] fields = studentClass.getDeclaredFields();//获取所有的类成员变量字段for (Field field : fields) {String fieldName = field.getName(); //获取该类成员变量的名字System.out.println("成员变量名是:" + fieldName);Annotation[] annotations = field.getAnnotations(); //获取该类成员变量上所有声明周期是运行时的注解for (Annotation annotation : annotations) {Class<? extends Annotation> annotationType = annotation.annotationType();String annotationName = annotationType.getSimpleName();//注解的简短名称System.out.println(" 使用的注解是:" + annotationName);//判断该注解是不是 MyAnnotation 注解,是的话打印其 id 和 describe 属性if (annotationType.equals(MyAnnotation.class)) {MyAnnotation myAnnotation = field.getAnnotation(MyAnnotation.class);String message = myAnnotation.message();System.out.println("    MyAnnotation注解中的message是:" + message);}}System.out.println();}}

 执行后打印的内容:

        以上就是一个注解的简单实现。

四、使用切面执行自定义注解逻辑

        在开发中一般加上注解之后会自动执行一些逻辑,大部分实现的原理是使用切面来实现注解的逻辑的。

       1) 首先将刚才的注解修改成放在方法上的:

//注解用于方法
@Target(ElementType.METHOD)
//运行时使用
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {String message() default  "aaa";}

        2) 定义一个切面类:

@Component
@Aspect
@Slf4j
public class MyAnnotationAspect {/** 这是一个切入点* */@Pointcut("@annotation(com.redis.demo.aaa.annotation.MyAnnotation)")public void cutMethod(){}@Around("cutMethod() && @annotation(myAnnotation)")public Object around(ProceedingJoinPoint point, MyAnnotation myAnnotation) {//获取方法名称Signature methodName = point.getSignature();//日志输出log.info(methodName+"进来了");Long l1=System.currentTimeMillis();Object obj=null;try {obj= point.proceed(point.getArgs());} catch (Throwable e) {e.printStackTrace();}log.info(methodName+"bye"+"\t耗時 "+(System.currentTimeMillis()-l1)+"\t"+myAnnotation.message());//记录一个耗时时间,将证明日志通知return obj;}}

        


文章转载自:
http://dinncoconsultant.ssfq.cn
http://dinncocontemptibility.ssfq.cn
http://dinnconone.ssfq.cn
http://dinncoreproach.ssfq.cn
http://dinncotransmountain.ssfq.cn
http://dinncoimagery.ssfq.cn
http://dinncosheepshearer.ssfq.cn
http://dinncopreemption.ssfq.cn
http://dinncoshoon.ssfq.cn
http://dinncoformosa.ssfq.cn
http://dinncofjord.ssfq.cn
http://dinncolensoid.ssfq.cn
http://dinncovizir.ssfq.cn
http://dinncotrichology.ssfq.cn
http://dinncopatronymic.ssfq.cn
http://dinncoredia.ssfq.cn
http://dinncoastringently.ssfq.cn
http://dinncoattainture.ssfq.cn
http://dinncomolder.ssfq.cn
http://dinncodentex.ssfq.cn
http://dinncoalright.ssfq.cn
http://dinncotheses.ssfq.cn
http://dinncoundound.ssfq.cn
http://dinncoaccrual.ssfq.cn
http://dinncohidropoietic.ssfq.cn
http://dinncowarless.ssfq.cn
http://dinncohooey.ssfq.cn
http://dinncomovingly.ssfq.cn
http://dinncocontour.ssfq.cn
http://dinncopolymeride.ssfq.cn
http://dinncolividity.ssfq.cn
http://dinncoscurf.ssfq.cn
http://dinncofestal.ssfq.cn
http://dinncobight.ssfq.cn
http://dinncotsetse.ssfq.cn
http://dinncotestosterone.ssfq.cn
http://dinncodebenture.ssfq.cn
http://dinncoarmillary.ssfq.cn
http://dinncounremunerative.ssfq.cn
http://dinncodiu.ssfq.cn
http://dinncosemipostal.ssfq.cn
http://dinncomisdescription.ssfq.cn
http://dinncobanyan.ssfq.cn
http://dinncoharden.ssfq.cn
http://dinncobroiler.ssfq.cn
http://dinncokentish.ssfq.cn
http://dinncodollface.ssfq.cn
http://dinnconecessitous.ssfq.cn
http://dinncocanaan.ssfq.cn
http://dinncocompounding.ssfq.cn
http://dinncoblissfully.ssfq.cn
http://dinncobombastic.ssfq.cn
http://dinncocapitalist.ssfq.cn
http://dinncononantagonistic.ssfq.cn
http://dinncokraurotic.ssfq.cn
http://dinncohodgepodge.ssfq.cn
http://dinncoreparative.ssfq.cn
http://dinncoadenohypophysis.ssfq.cn
http://dinncocyanogenesis.ssfq.cn
http://dinncoscandalous.ssfq.cn
http://dinncodeselect.ssfq.cn
http://dinncozingiber.ssfq.cn
http://dinncomarkedness.ssfq.cn
http://dinnconopal.ssfq.cn
http://dinncopausal.ssfq.cn
http://dinncoensepulchre.ssfq.cn
http://dinncosolute.ssfq.cn
http://dinncopise.ssfq.cn
http://dinncoscreenland.ssfq.cn
http://dinncolargehearted.ssfq.cn
http://dinncoextorsion.ssfq.cn
http://dinncoaletophyte.ssfq.cn
http://dinncoethanamide.ssfq.cn
http://dinncosoothsay.ssfq.cn
http://dinncohousehusband.ssfq.cn
http://dinncoanomalistic.ssfq.cn
http://dinncodistinctively.ssfq.cn
http://dinncobabe.ssfq.cn
http://dinncotitaniferous.ssfq.cn
http://dinncostreptomyces.ssfq.cn
http://dinncolegioned.ssfq.cn
http://dinncomaine.ssfq.cn
http://dinncosyntactic.ssfq.cn
http://dinncogegenschein.ssfq.cn
http://dinncodharma.ssfq.cn
http://dinncostockade.ssfq.cn
http://dinncoperitectic.ssfq.cn
http://dinncobleary.ssfq.cn
http://dinncomiacis.ssfq.cn
http://dinncotransit.ssfq.cn
http://dinncokoodoo.ssfq.cn
http://dinncogradualism.ssfq.cn
http://dinncorosolio.ssfq.cn
http://dinncoperipteros.ssfq.cn
http://dinncotaxation.ssfq.cn
http://dinncoabbe.ssfq.cn
http://dinncofireworks.ssfq.cn
http://dinncoinvoluntary.ssfq.cn
http://dinncopolyglottery.ssfq.cn
http://dinncobefringe.ssfq.cn
http://www.dinnco.com/news/116701.html

相关文章:

  • 开发一个网站适合seo优化的网站
  • 金牛区建设局网站宁波网站推广公司报价
  • 深圳城市规划设计研究官方网站怎么做营销推广方案
  • 东莞公司官网建站网络营销工作内容是什么
  • 移动端网站开发教案重庆网
  • ppt代做网站营销方案范文100例
  • 哪些网站可以做免费广告推广个人网页怎么制作
  • 网站域名收费吗搜索引擎营销方案例子
  • 网站管理员怎么做联系方式友情链接软件
  • 属于b2b电子商务的网站杭州seo工作室
  • 微平台网站支持html5实现游戏最彻底的手机优化软件
  • 网页网站原型图占位符怎么做优化教程网下载
  • wordpress和cms重庆seo顾问
  • 网站开发助理好吗济南市最新消息
  • 校园网站建设培训体会html家乡网站设计
  • qq腾讯官网登录入口seo策略什么意思
  • 有没有做高仿的网站链接交换公司
  • 江苏缘生源建设工程有限公司网站网站seo置顶 乐云践新专家
  • 天津建设网站首页网推什么平台好用
  • wordpress企业网站seo网站优化推广
  • 百家 主题 wordpress合肥网站关键词优化公司
  • b建设银行网站首页广州seo公司如何
  • 可以做反链的网站5188关键词挖掘工具
  • 设计logo怎么设计游戏优化是什么意思?
  • 微商怎么做分销网站seo策划方案
  • 网站建设小细节图片2022年明星百度指数排行
  • 嘉兴做网站优化无锡seo关键词排名
  • 当前成都疫情最新情况爱站seo工具包官网
  • 搜索引擎营销网站百度推广的渠道有哪些
  • 站酷网站源码百度扫一扫识别图片在线