国内网站建设需要多少钱,长沙建站宝网络科技有限公司,wordpress主题主页修改,在线图片制作工具从Java 5开始#xff0c;Java中出现了注释。 我想做一个自己的注释#xff0c;只是为了看看需要什么。 但是#xff0c;我发现它们只是接口。 有擦 接口后面没有牙。 必须执行一些代码。 我认为这是橡胶行之有效的方法#xff0c;我真的找到了解决方法。 首先#xff0c… 从Java 5开始Java中出现了注释。 我想做一个自己的注释只是为了看看需要什么。 但是我发现它们只是接口。 有擦 接口后面没有牙。 必须执行一些代码。 我认为这是橡胶行之有效的方法我真的找到了解决方法。 首先我需要一个目标 我选择了一个最近的热门话题缓存。 我不想实现JSR 109JCache但也不想做典型的“ Hello World”。 我选择实现两个注释一个注释不带任何参数另一个注释不带参数。 我还需要一个缓存提供程序。 如果我要这样做的话还可以将真正的缓存库加入其中。 它还遵循我的设计理念即使用产品/库来达成目标而不是在家纺所有东西。 经过仔细考虑我选择了hazelcast作为我的缓存引擎。 它是市场上最快的而且是免费的。 更多决定 选择我的目标后我仍然需要找出如何在它们后面扎牙的方法。 经过一番挖掘我发现了两种方法 反射 几乎每次使用反射时我都会为编写如此笨拙的代码感到遗憾。 另外要按照我想要的方式进行操作我必须创建自己的框架。 听起来两个注解的工作量很大。 面向方面的编程AOP 这非常适合我想做的事。 AOP致力于将样板代码减少到一个地方。 这将很方便并且与缓存紧密结合因为缓存可分为以下步骤 检查此情况是否之前已完成。 如果是这样的话 检索存储的结果 如果不 运行功能 存储结果 返回结果 也许这过于简单化了但说实话。 就像所有事物一样细节决定成败。 同时回到AOP牧场 虽然我知道AOP是适合我的地方但我对此并不了解。 我发现Spring有一个AOP库而众所周知的库是AspectJ。 AspectJ对我不熟悉需要运行时引擎才能工作。 我对Spring更加熟悉所以选择了它。 在研究Spring的AOP时我发现我必须深入研究AspectJ的注释因此无论如何我还是以某种形式或方式被AspectJ所困扰。 新概念新词汇 编写方面不像编写对象。 它们是对象但并非如此因此当然需要一组新的术语。 我使用的是Spring AOP文档中的内容 我确实需要阅读几次页面才能理解所讲的内容。 强烈建议您执行一项操作否则其余帖子听起来会像胡言乱语。 切入点的构成和建议 切入点设计很容易因为我只对带有注释的方法感兴趣。 它需要的建议是周围的建议因为如果已经进行了匹配的调用我就需要能够避免调用该方法。 最后的代码 Maven Pom.xml ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.darylmathison/groupIdartifactIdannotation-implementation/artifactIdversion1.0-SNAPSHOT/versionpropertiesproject.reporting.outputEncodingUTF-8/project.reporting.outputEncodingproject.build.sourceEncodingUTF-8/project.build.sourceEncodingmaven.compiler.source1.8/maven.compiler.sourcemaven.compiler.target1.8/maven.compiler.targetspring.version4.2.4.RELEASE/spring.version/propertiesdescriptionThis project is an example of how to implement an annotation via Spring AOP./descriptionscmurlhttps://github.com/darylmathison/annotation-implementation-example.git/urlconnectionscm:git:https://github.com/darylmathison/annotation-implementation-example.git/connectiondeveloperConnectionscm:git:gitgithub.com:darylmathison/annotation-implementation-example.git/developerConnection/scmissueManagementsystemGitHub/systemurlhttps://github.com/darylmathison/annotation-implementation-example/issues/url/issueManagementdependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-aop/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-test/artifactIdversion${spring.version}/versionscopetest/scope/dependencydependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactIdversion1.8.8/version/dependencydependencygroupIdcom.hazelcast/groupIdartifactIdhazelcast/artifactIdversion3.6/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/versionscopetest/scope/dependency/dependenciesreportingpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-project-info-reports-plugin/artifactIdversion2.7/versionreportSetsreportSetreportsreportdependencies/reportreportindex/reportreportproject-team/reportreportissue-tracking/reportreportscm/report/reports/reportSet/reportSets/pluginplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-surefire-report-plugin/artifactIdversion2.18.1/version/pluginplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-javadoc-plugin/artifactIdversion2.10.3/versionreportSetsreportSetreportsreportjavadoc/reportreporttest-javadoc/report/reports/reportSet/reportSets/pluginplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-jxr-plugin/artifactIdversion2.5/versionconfigurationlinkJavadoctrue/linkJavadoc/configurationreportSetsreportSetreportsreportjxr/reportreporttest-jxr/report/reports/reportSet/reportSets/pluginplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-changelog-plugin/artifactIdversion2.3/versionconfigurationtyperange/typerange90/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 MapString, Object cache() {Config config new Config();MapConfig mapConfig new MapConfig();mapConfig.setEvictionPercentage(50);mapConfig.setEvictionPolicy(EvictionPolicy.LFU);mapConfig.setTimeToLiveSeconds(300);MapString, 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 {OverrideCacheMepublic long calculate(int rounds) {return sharedCalculate(rounds);}OverrideCacheMeNow(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 MapString, 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