注释嵌套注释_DIY注释

注释嵌套注释

从Java 5开始,Java中出现了注释。 我想做一个自己的注释,只是为了看看需要什么。 但是,我发现它们只是接口。

有擦

接口后面没有牙。 必须执行一些代码。 我认为这是橡胶行之有效的方法,我真的找到了解决方法。

首先,我需要一个目标

我选择了一个最近的热门话题:缓存。 我不想实现JSR 109(JCache),但也不想做典型的“ Hello World”。 我选择实现两个注释,一个注释不带任何参数,另一个注释不带参数。 我还需要一个缓存提供程序。 如果我要这样做的话,还可以将真正的缓存库加入其中。 它还遵循我的设计理念,即使用产品/库来达成目标,而不是将所有内容都进行家庭纺。 经过仔细考虑,我选择了hazelcast作为我的缓存引擎。 它是市场上最快的,而且是免费的。

更多决定

在选择了目标之后,我仍然需要找出如何在它们后面扎牙的方法。 经过一番挖掘,我发现了两种方法:

反射

几乎每次使用反射时,我都会为编写这么笨拙的代码感到遗憾。 另外,要按照我想要的方式进行操作,我必须创建自己的框架。 听起来两个注解的工作量很大。

面向方面的编程(AOP)

这非常适合我想做的事。 AOP致力于将样板代码减少到一个地方。 这将很方便并且与缓存紧密结合,因为缓存可分为以下步骤:

  1. 检查此情况是否之前已完成。
  2. 如果是这样的话:
    1. 检索存储的结果
  3. 如果不:
    1. 运行功能
    2. 存储结果
  4. 返回结果

也许这过于简单,但总的来说是正确的。 就像所有事物一样,细节决定成败。

同时,回到AOP牧场

虽然我知道AOP是适合我的地方,但我对此并不了解。 我发现Spring有一个AOP库,而众所周知的库是AspectJ。 AspectJ对我不熟悉,需要运行时引擎才能工作。 我对Spring更加熟悉,所以选择了它。 在研究Spring的AOP时,我发现我必须深入研究AspectJ的注释,因此无论如何我还是以某种形式或方式被AspectJ所困。

新概念,新词汇

编写方面不像编写对象。 它们是对象,但并非如此,因此当然需要一组新的术语。 我使用的是Spring AOP文档中的内容

我确实需要阅读几次该页面才能理解所讲的内容。 强烈建议您执行相同的操作,否则其余帖子听起来像胡言乱语。

切入点的构成和建议

切入点设计很容易,因为我只对带有注释的方法感兴趣。 它需要的建议是周围的建议,因为如果已经进行了匹配的调用,我就需要能够避免调用该方法。

最后的代码

Maven 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"><modelVersion>4.0.0</modelVersion><groupId>com.darylmathison</groupId><artifactId>annotation-implementation</artifactId><version>1.0-SNAPSHOT</version><properties><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><spring.version>4.2.4.RELEASE</spring.version></properties><description>This project is an example of how to implement an annotation via Spring AOP.</description><scm><url>https://github.com/darylmathison/annotation-implementation-example.git</url><connection>scm:git:https://github.com/darylmathison/annotation-implementation-example.git</connection><developerConnection>scm:git:git@github.com:darylmathison/annotation-implementation-example.git</developerConnection></scm><issueManagement><system>GitHub</system><url>https://github.com/darylmathison/annotation-implementation-example/issues</url></issueManagement><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version><scope>test</scope></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.8</version></dependency><dependency><groupId>com.hazelcast</groupId><artifactId>hazelcast</artifactId><version>3.6</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies><reporting><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-project-info-reports-plugin</artifactId><version>2.7</version><reportSets><reportSet><reports><report>dependencies</report><report>index</report><report>project-team</report><report>issue-tracking</report><report>scm</report></reports></reportSet></reportSets></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-report-plugin</artifactId><version>2.18.1</version></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-javadoc-plugin</artifactId><version>2.10.3</version><reportSets><reportSet><reports><report>javadoc</report><report>test-javadoc</report></reports></reportSet></reportSets></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jxr-plugin</artifactId><version>2.5</version><configuration><linkJavadoc>true</linkJavadoc></configuration><reportSets><reportSet><reports><report>jxr</report><report>test-jxr</report></reports></reportSet></reportSets></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-changelog-plugin</artifactId><version>2.3</version><configuration><type>range</type><range>90</range></configuration></plugin></plugins></reporting>
</project>

注释

快取

缓存注释的可爱名称,对吗?

package com.darylmathison.ai.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** Created by Daryl on 2/19/2016.*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface CacheMe {
}

CacheMeNow

package com.darylmathison.ai.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** Created by Daryl on 2/19/2016.*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface CacheMeNow {String key();
}

弹簧配置

我决定使用基于Java的配置,而不是像通常为了改变速度而使用的XML。 EnableAspectJAutoProxy批注是使Spring AOP开始工作的关键。 我一直在我旁边,直到我读到有关这颗小宝石的这篇文章。 有时,这是一天中最容易燃烧的事情。

AppConfig

package com.darylmathison.ai.config;import com.darylmathison.ai.cache.CacheAspect;
import com.darylmathison.ai.service.FibonacciService;
import com.darylmathison.ai.service.FibonacciServiceImpl;
import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionPolicy;
import com.hazelcast.config.MapConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;import java.util.HashMap;
import java.util.Map;/*** Created by Daryl on 2/20/2016.*/
@Configuration
@ComponentScan(basePackages = "com.darylmathison.ai")
@EnableAspectJAutoProxy
public class AppConfig {@Beanpublic Map<String, Object> cache() {Config config = new Config();MapConfig mapConfig = new MapConfig();mapConfig.setEvictionPercentage(50);mapConfig.setEvictionPolicy(EvictionPolicy.LFU);mapConfig.setTimeToLiveSeconds(300);Map<String, MapConfig> mapConfigMap = new HashMap<>();mapConfigMap.put("cache", mapConfig);config.setMapConfigs(mapConfigMap);HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);return instance.getMap("cache");}@Beanpublic FibonacciService fibonacci() {return new FibonacciServiceImpl();}@Beanpublic CacheAspect cacheAspect() {return new CacheAspect();}
}

服务编号

基于经典Spring的设计需要服务吗? 由于Spring使用代理来实现其AOP,因此强烈建议为带注释的类定义一个接口以实现。

斐波那契服务

package com.darylmathison.ai.service;/*** Created by Daryl on 2/20/2016.*/
public interface FibonacciService {long calculate(int rounds);long calculateWithKey(int rounds);
}

FibonacciServiceImpl

package com.darylmathison.ai.service;import com.darylmathison.ai.annotation.CacheMe;
import com.darylmathison.ai.annotation.CacheMeNow;/*** Created by Daryl on 2/20/2016.*/
public class FibonacciServiceImpl implements FibonacciService {@Override@CacheMepublic long calculate(int rounds) {return sharedCalculate(rounds);}@Override@CacheMeNow(key = "now")public long calculateWithKey(int rounds) {return sharedCalculate(rounds);}private static long sharedCalculate(int rounds) {long[] lastTwo = new long[] {1, 1};for(int i = 0; i < rounds; i++) {long last = lastTwo[1];lastTwo[1] = lastTwo[0] + lastTwo[1];lastTwo[0] = last;}return lastTwo[1];}
}

AOP的东西

这是注释实现的核心。 其他所有内容都可以用来支持后续的工作。

系统存档

根据Spring文档,集中化切入点定义是一个好主意。

package com.darylmathison.ai.cache;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;/*** Created by Daryl on 2/20/2016.*/
@Aspect
public class SystemArch {@Pointcut("@annotation(com.darylmathison.ai.annotation.CacheMe)")public void cacheMeCut() {}@Pointcut("@annotation(com.darylmathison.ai.annotation.CacheMeNow)")public void cacheMeNowCut() {}
}

缓存方面

周围注释使用切入点类的完整方法名称来定义建议的内容。 CacheMeNow批注的建议包括一个额外条件,因此可以定义批注,以便可以读取关键参数。 测试代码中揭示了CacheMeNow中的一个设计错误。

package com.darylmathison.ai.cache;import com.darylmathison.ai.annotation.CacheMeNow;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;import java.util.Map;/*** Created by Daryl on 2/20/2016.*/
@Aspect
public class CacheAspect {@Autowiredprivate Map<String, Object> cache;@Around("com.darylmathison.ai.cache.SystemArch.cacheMeCut()")public Object simpleCache(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {StringBuffer keyBuffer = new StringBuffer();for(Object o: proceedingJoinPoint.getArgs()) {keyBuffer.append(o.hashCode());}String key = keyBuffer.toString();Object ret = cache.get(key);if(ret == null) {ret = proceedingJoinPoint.proceed();cache.put(key, ret);}return ret;}@Around("com.darylmathison.ai.cache.SystemArch.cacheMeNowCut() && @annotation(cacheMeNow)")public Object simpleCacheWithParam(ProceedingJoinPoint proceedingJoinPoint, CacheMeNow cacheMeNow) throws Throwable {Object ret = cache.get(cacheMeNow.key());if(ret == null) {ret = proceedingJoinPoint.proceed();cache.put(cacheMeNow.key(), ret);}return ret;}
}

测试代码

显示注释确实引起缓存的驱动程序代码。

斐波那契检验

package com.darylmathison.ai.service;import com.darylmathison.ai.config.AppConfig;
import org.junit.Assert;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** Created by Daryl on 2/20/2016.*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfig.class})
public class FibonacciTest {private static final int ROUNDS = 12;private static final long ANSWER = 377;@Autowiredprivate FibonacciService fibonacci;@org.junit.Testpublic void testCalculate() throws Exception {long start = System.currentTimeMillis();Assert.assertEquals(ANSWER, fibonacci.calculate(ROUNDS));long middle = System.currentTimeMillis();Assert.assertEquals(ANSWER, fibonacci.calculate(ROUNDS));long end = System.currentTimeMillis();Assert.assertTrue((end - middle) < (middle - start));}@org.junit.Testpublic void testCalculateWithKey() throws Exception {Assert.assertEquals(ANSWER, fibonacci.calculateWithKey(ROUNDS));// This test should not passAssert.assertEquals(ANSWER, fibonacci.calculateWithKey(13));}
}

结论

注释不必很难实现。 使用AOP编程,我可以用很少的代码来实现两个注释。

翻译自: https://www.javacodegeeks.com/2016/03/diy-annotations-3.html

注释嵌套注释

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/336185.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

汇编 cmp_汇编复习

第一章计算机组成五部分&#xff1a;&#xff08;运算器、控制器&#xff09;、存储器、输入/输出设备↑↑ CPU ↑↑ ↑内存↑三条总线&#xff1a;控制总线、地址总线、数据总线不同进制及BCD码的转换特殊ascll ‘0’~‘9’—— 30H ~ 39H‘A’~‘F’—— 41H ~ 46H回车 —— …

【WebRTC---进阶篇】(四)mediasoup服务器的布署与使用

Nodejs安装 Nodejs安装 下载mediasoup Demo,并配置 详细GitHub地址 https://github.com/versatica/mediasoup-demo

睡眠是锁定计算机怎么设置密码,电脑休眠锁屏怎么设置

想要保护好自己的隐私&#xff0c;就要懂得电脑休眠锁屏怎么设置哦&#xff01;什么&#xff1f;你不会设置&#xff1f;不怕&#xff0c;小编这就来为您支招&#xff01;请看下文内容&#xff01;电脑休眠锁屏怎么设置用户账户面板1、首先您需要创建用户密码&#xff0c;如果您…

junit5和junit4_JUnit 5 –设置

junit5和junit42015年11月&#xff0c; JUnit Lambda团队展示了他们的原型 。 此后&#xff0c;该项目更名为JUnit 5&#xff0c;并于2016年2月发布了Alpha版本。我们将在一系列简短文章中进行探讨&#xff1a; 建立 基本 建筑 条件 注射 … 本节讨论JUnit 5的设置&…

markdown 生成目录_github上如何为markdown文件生成目录

写在前面熟悉markdown都知道可以使用[TOC]自动生成markdown文件的标题目录&#xff0c;比如在typora&#xff0c;vscode(需要插件)等本地编辑器中&#xff0c;或者在CSDN等网页编辑器中&#xff0c;但是github却不支持[TOC]标签&#xff0c;至于为什么不支持感兴趣的可以深入搜…

【WebRTC---进阶篇】(五)mediasoup的信令系统

mediasoup demo分析 app 客户端部分 broadcasters 推拉流部分 server 服务端部分 config.js 相当于一个配置文件&#xff0c;获取一些基本配置信息。获取的信息来交给server.js。 server.js 先从config.js获取信息&#xff0c;然后启动HTTPS webSocket服务等&#xff0c;…

axure html尺寸,axure怎么确定尺寸

回答&#xff1a;您好如做的是室内设计的话&#xff0c;那么来说可能会些参数提供给您的(例如长宽高)如果没有参数的话&#xff0c;只要把比例做好就可以了。只要比例做好了&#xff0c;东西看起来就自然像。至于教程的话&#xff0c;一般录制教程前都会有做好准备的&#xff0…

java工程引入scala_引入ReactiveInflux:用于Scala和Java的无阻塞InfluxDB驱动程序,支持Apache Spark...

java工程引入scala我很高兴宣布Pygmalios开发的ReactiveInflux的第一个发行版。 InfluxDB错过了Scala和Java的非阻塞驱动程序。 不变性&#xff0c;可测试性和可扩展性是ReactiveInflux的关键功能。 加上对Apache Spark的支持&#xff0c;它是首选武器。 https://github.com/p…

exe打包工具哪个最好_为你的 Python 程序写个启动工具箱

到目前为止&#xff0c;公众号已经介绍了不少图形界面的软件&#xff0c;比如猜数游戏、PDF阅读器、贪吃蛇游戏、天气查询软件、PDF 阅读器等。为了方便他人使用&#xff0c;我们常把图形界面打包成 exe 文件。但是如果我们只是为了自己使用方便的话&#xff0c;我们有必要把程…

【WebRTC---进阶篇】(六)SELECT网络模型

select函数原型 int WSAAPI select(_In_ int nfds,_Inout_opt_ fd_set FAR * readfds,_Inout_opt_ fd_set FAR * writefds,_Inout_opt_ fd_set FAR * exceptfds,_In_opt_ const struct timeval FAR * timeout); 函数功能:监视多个文件描述符的状态变化,在IO中负责IO的第一步…

计算机管理没有打印机列队,在Windows清除打印队列如果打印机被卡住,也没有打印输出...

我相信自己已经勾起回忆一拉似曾相识 &#xff0c;右侧的主题&#xff1f; 我们每个人&#xff0c;在一段时间或其他&#xff0c;都在打印过程中面临的问题&#xff0c;特别是给打印命令&#xff0c;并打印输出不休后等待。 无论是在家里还是办公室里&#xff0c;那就是我们所有…

gatling 使用_使用Gatling + Gradle + Jenkins Pipeline为您的JAX-RS(和JavaEE)应用程序进行连续压力测试...

gatling 使用在这篇文章中&#xff0c;我将解释如何使用Gatling项目为您的JAX-RS Java EE端点编写压力测试&#xff0c;以及如何将它们与Gradle和Jenkins Pipeline集成&#xff0c;因此&#xff0c;除了进行简单的压力测试外&#xff0c;您还可以使用以下方法&#xff1a; 连续…

【开源项目】Socket服务端与客户端传输视频文件

TCP Server端 #define WIN32_LEAN_AND_MEAN #define _WINSOCK_DEPRECATED_NO_WARNINGS#include<windows.h> #include<WinSock2.h> #include<stdio.h> #include <malloc.h>#pragma comment(lib,"ws2_32.lib")int main() {//启动Windows sock…

java comparator_【面试题】Java必考面试题全集(15)

Java基础面试题(15)1&#xff1a;Comparator 与Comparable 有什么不同&#xff1f;2&#xff1a;Object中有哪些方法&#xff1f;3&#xff1a;说下jdk8中的一些新特性4&#xff1a;在64 位 JVM 中&#xff0c;int 的长度是多数&#xff1f;5&#xff1a;java每改一点都需要重新…

px是什么意思计算机二级,px是什么意思?照片中的px是什么的缩写?

px是什么意思?px(Pixel&#xff0c;像素)是可以在数字显示设备上显示和表示的数字图像或图形的最小单位。像素是数字图形中的基本逻辑单元。将像素组合在一起以在计算机显示器上形成完整的图像&#xff0c;视频&#xff0c;文本或任何可见的东西。像素也称为图像元素。若把影像…

【OpenGL从入门到精通(一)】Windows搭建OpenGL的渲染环境,并初始化一个OPenGL窗口

注意&#xff1a;需要在Windows 窗口程序下&#xff0c;而不能是控制台程序&#xff0c;Windows平台的VS下已经包含了OpenGL相关的API&#xff0c;可以直接引用 #include <windows.h> #include<gl/GL.h> #include<gl/GLU.h>#pragma comment(lib,"openg…

java 交替_Java 8:使用交替接口公开的类型安全地图生成器

java 交替动态展示您的课程 当我还是Java新手时&#xff0c;我记得当时以为应该有一种方法可以删除或隐藏我不想公开的类中的方法。 就像用private方法或类似方法覆盖public方法一样&#xff08;哪种情况是不可能的&#xff0c;也不应该是不可能的&#xff09;。 显然&#xff…

ieee期刊_论文绘图神器来了:一行代码绘制不同期刊格式图表,哈佛博士后开源...

贾浩楠 发自 凹非寺量子位 报道 | 公众号 QbitAI「一篇论文投多个期刊&#xff0c;每个期刊对图表格式要求不一&#xff0c;同一组数据要用多种工具分别绘图。」不光是你&#xff0c;哈佛大学天文研究所的博士后&#xff0c;也不堪忍受论文重复绘图之苦。他的解决办法是&#x…

微课与计算机技术的论文,微课在高校计算机教学的运用论文

微课在高校计算机教学的运用论文摘要&#xff1a;现代信息社会不断发展进步&#xff0c;高校计算机教学也面临着复杂的形势&#xff0c;为全面提高计算机教学质量&#xff0c;提升学生的专业素质及综合能力&#xff0c;应当微课加以科学化应用。本文基于微课的内涵及应用意义出…

【OpenGL从入门到精通(二)】绘制一个点

1.想要绘制一个点&#xff0c;首先要在OpenGL初始化中先设置矩阵 2.然后在绘制场景中进行点的绘制。其中包括 当前颜色设置&#xff1b;点的位置&#xff0c;点的大小等等 #include <windows.h> #include<gl/GL.h> #include<gl/GLU.h>#pragma comment(lib,…