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

合肥制作企业网站免费创建网站软件

合肥制作企业网站,免费创建网站软件,做一个自我介绍的网页,京东物流网站建设特点1.什么是注解 注解(Annotation),也叫元数据。一种代码级别的说明。它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元…

1.什么是注解

注解(Annotation),也叫元数据。一种代码级别的说明。它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。

元注解

有一些注解可以修饰其他注解,这些注解就称为元注解(meta annotation)。Java标准库已经定义了一些元注解,我们只需要使用元注解,通常不需要自己去编写元注解。

@Target

最常用的元注解是@Target。使用@Target可以定义Annotation能够被应用于源码的哪些位置:

  • 类或接口:ElementType.TYPE
  • 字段:ElementType.FIELD
  • 方法:ElementType.METHOD
  • 构造方法:ElementType.CONSTRUCTOR
  • 方法参数:ElementType.PARAMETER

例如,定义注解@Report可用在方法上,我们必须添加一个@Target(ElementType.METHOD)

@Target(ElementType.METHOD)
public @interface Report {int type() default 0;String level() default "info";String value() default "";
}

定义注解@Report可用在方法或字段上,可以把@Target注解参数变为数组{ ElementType.METHOD, ElementType.FIELD }

@Target({ElementType.METHOD,ElementType.FIELD
})
public @interface Report {...
}

实际上@Target定义的valueElementType[]数组,只有一个元素时,可以省略数组的写法。

@Retention

另一个重要的元注解@Retention定义了Annotation的生命周期:

  • 仅编译期:RetentionPolicy.SOURCE
  • 仅class文件:RetentionPolicy.CLASS
  • 运行期:RetentionPolicy.RUNTIME

如果@Retention不存在,则该Annotation默认为CLASS。因为通常我们自定义的Annotation都是RUNTIME,所以,务必要加上@Retention(RetentionPolicy.RUNTIME)这个元注解:

@Retention(RetentionPolicy.RUNTIME)
public @interface Report {int type() default 0;String level() default "info";String value() default "";
}
@Repeatable

使用@Repeatable这个元注解可以定义Annotation是否可重复。这个注解应用不是特别广泛。

@Repeatable(Reports.class)
@Target(ElementType.TYPE)
public @interface Report {int type() default 0;String level() default "info";String value() default "";
}@Target(ElementType.TYPE)
public @interface Reports {Report[] value();
}

经过@Repeatable修饰后,在某个类型声明处,就可以添加多个@Report注解:

@Report(type=1, level="debug")
@Report(type=2, level="warning")
public class Hello {
}
@Inherited

使用@Inherited定义子类是否可继承父类定义的Annotation@Inherited仅针对@Target(ElementType.TYPE)类型的annotation有效,并且仅针对class的继承,对interface的继承无效:

@Inherited
@Target(ElementType.TYPE)
public @interface Report {int type() default 0;String level() default "info";String value() default "";
}

在使用的时候,如果一个类用到了@Report

@Report(type=1)
public class Person {
}

则它的子类默认也定义了该注解:

public class Student extends Person {
}

2.代码工程

实验目的

实现统计方法执行时间的注解

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springboot-demo</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>annotations</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId></dependency><dependency><groupId>org.assertj</groupId><artifactId>assertj-core</artifactId></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId></dependency><!-- fastjson --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.21</version></dependency></dependencies>
</project>

controller

使用自定义注解@RequestTime

package com.et.annotation.controller;import com.et.annotation.RequestTime;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;@RestController
public class HelloWorldController {@RequestMapping("/hello")@RequestTimepublic Map<String, Object> showHelloWorld(){Map<String, Object> map = new HashMap<>();map.put("msg", "HelloWorld");return map;}
}

custom annotation

自定义@RequestTime注解

package com.et.annotation;import java.lang.annotation.*;/*** computa the excute time for the method*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface RequestTime {}

具体的拦截逻辑类

package com.et.annotation;import com.et.annotation.util.AspectUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;import java.lang.reflect.Method;@Slf4j
@Aspect
@Component
public class RequestTimeAspect {@Pointcut(value = "@annotation(com.et.annotation.RequestTime)")public void pointcut() {}@Around("pointcut()")public Object handle(ProceedingJoinPoint point) throws Throwable {Method currentMethod = AspectUtil.getMethod(point);long starttime = System.currentTimeMillis();Object result = point.proceed();long endtime = System.currentTimeMillis();long requesttime =endtime-starttime;//if(requesttime>1000){log.info(AspectUtil.getClassName(point)+"."+currentMethod.getName()+"execute time:"+requesttime+" ms");//}return result;}
}

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

  • https://github.com/Harries/springboot-demo(annotations)

3.测试

  1. 启动Spring Boot应用程序
  2. 访问http://127.0.0.1:8088/hello
  3. 控制台输出 日志
2024-08-10 19:30:43.670 INFO 3343 --- [nio-8088-exec-1] com.et.annotation.RequestTimeAspect : com_et_annotation_controller_HelloWorldController.showHelloWorldexecute time:41 ms

4.引用

  • https://www.baeldung.com/java-custom-annotation 
  • Spring Boot如何自定义注解? | Harries Blog™
http://www.shuangfujiaoyu.com/news/24442.html

相关文章:

  • 淘宝的网站建设网站怎么做收录
  • wordpress中文客户端seo技术公司
  • 网站模版购买如何让百度收录
  • 做网站如何连数据库南京网站推广公司
  • php动态网站开发软件长尾词seo排名
  • 南京网页网站制作宁波seo关键词优化教程
  • 中山做app网站公司哪家好aso是什么意思
  • 京东怎么做轮播图链接网站百度推广和百度竞价有什么区别
  • 南京做网站建设搭建的公司网络销售工资一般多少
  • b2c是指什么的电子商务模式搜索引擎优化培训免费咨询
  • 高能建站系统怎么收费网络营销推广目标
  • 网站的建设及推广深圳短视频推广
  • 开平 做一网站微信搜一搜seo优化
  • 下载了网站源码外链平台
  • 衡阳电商网站建设青岛网站建设技术外包
  • 网站域名 空间 是每年都要缴费吗自媒体视频剪辑培训班
  • 网站改备案百度网盘网址是多少
  • 新竹网站苏州seo营销
  • 网站管理后台源码免费的十大免费货源网站
  • 洛阳做网站哪家好广州百度推广开户
  • 动态网站转静态网站亚马逊alexa
  • 灰蓝 网站模板淄博网站seo
  • wordpress导航调用分类郑州seo外包阿亮
  • 制作好看的wordpress页面seo 百度网盘
  • 怎么找网站做公示兰州压热搜
  • 上海大型网站建设最近新闻大事件
  • 网站首页图片滑动怎么做seo快速排名上首页
  • wordpress怎么访问数据库淘宝seo搜索排名优化
  • 怎样做28网站代理干净无广告的搜索引擎
  • 用vs2008做网站视频教程重庆森林经典台词独白