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

淄博企业建网站扬州网络推广哪家好

淄博企业建网站,扬州网络推广哪家好,阿勒泰建设招聘网站,中国制造网外贸网网站目录 一. Java注解简介 1.1 Java注解分类 1.2 JDK基本注解 Override Deprecated SuppressWarnings 1.3 JDK元注解 从 Java 7 开始,额外添加了 3 个注解: 1.4 自定义注解 如何自定义注解? 二. 自定义注解示例 枚举类: 示例一&…

目录

一. Java注解简介

1.1 Java注解分类

1.2 JDK基本注解

@Override

@Deprecated

@SuppressWarnings

1.3 JDK元注解

从 Java 7 开始,额外添加了 3 个注解:

1.4 自定义注解

如何自定义注解?

二. 自定义注解示例

枚举类:

示例一:获取类与方法上的注解值

示例二:获取到类属性上的注解属性值

示例三:获取参数修饰注解对应的属性值

 三. aop自定义注解的应用

MyLog自定义注解类

MyLogAspect切面类

controller层


一. Java注解简介

注解是JDK1.5版本开始引入的一个特性,是附加在代码中的一些元信息,用于对代码进行说明,可以对包、类、接口、字段、方法参数、局部变量等进行注解。注解相关类都包含在java.lang.annotation包中。它主要的作用有以下四方面:

  •  生成文档,通过代码里标识的元数据生成javadoc文档。
  •  编译检查,通过代码里标识的元数据让编译器在编译期间进行检查验证。
  •  编译时动态处理,编译时通过代码里标识的元数据动态处理,例如动态生成代码。
  •  运行时动态处理,运行时通过代码里标识的元数据动态处理,例如使用反射注入实例。

1.1 Java注解分类

  • JDK基本注解
  • JDK元注解
  • 自定义注解

1.2 JDK基本注解

@Override

检查该方法是否是重写方法。如果发现其父类,或者是引用的接口中并没有该方法时,会报编译错误。

@Deprecated

标记过时方法。如果使用该方法,会报编译警告。 这个不是报错,只是警告,提醒我们这个方法可能会有问题,可能有更好的方法来实现!

@SuppressWarnings

指示编译器去忽略注解中声明的警告,其参数有:

deprecation,使用了过时的类或方法时的警告

unchecked,执行了未检查的转换时的警告

fallthrough,当 switch 程序块直接通往下一种情况而没有 break 时的警告

path,在类路径、源文件路径等中有不存在的路径时的警告

serial,当在可序列化的类上缺少serialVersionUID 定义时的警告

finally ,任何 finally 子句不能正常完成时的警告

all,关于以上所有情况的警告

1.3 JDK元注解

用于修饰注解的注解,称为元注解,需要注意,元注解只能修饰注解,不能修饰成员变量、方法、类。

  • @Retention - 标识这个注解怎么保存,是只在代码中,还是编入class文件中,或者是在运行时可以通过反射访问。
  • @Documented - 将此注解包含在 javadoc 中 ,它代表着此注解会被javadoc工具提取成文档
  • @Target - 标记这个注解应该是哪种 Java 成员。
  • @Inherited - 标记这个注解是继承于哪个注解类(默认 注解并没有继承于任何子类)
@Retention:定义注解的保留策略
@Retention(RetentionPolicy.SOURCE)             //注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS)              //默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
@Retention(RetentionPolicy.RUNTIME)            //注解会在class字节码文件中存在,在运行时可以通过反射获取到@Target:指定被修饰的Annotation可以放置的位置(被修饰的目标)
@Target(ElementType.TYPE)                      //接口、类
@Target(ElementType.FIELD)                     //属性
@Target(ElementType.METHOD)                    //方法
@Target(ElementType.PARAMETER)                 //方法参数
@Target(ElementType.CONSTRUCTOR)               //构造函数
@Target(ElementType.LOCAL_VARIABLE)            //局部变量
@Target(ElementType.ANNOTATION_TYPE)           //注解
@Target(ElementType.PACKAGE)                   //包
注:可以指定多个位置,例如:
@Target({ElementType.METHOD, ElementType.TYPE}),也就是此注解可以在方法和类上面使用@Inherited:指定被修饰的Annotation将具有继承性@Documented:指定被修饰的该Annotation可以被javadoc工具提取成文档.

从 Java 7 开始,额外添加了 3 个注解:

  • @SafeVarargs - Java 7 开始支持,忽略任何使用参数为泛型变量的方法或构造函数调用产生的警告。
  • @FunctionalInterface - Java 8 开始支持,标识一个匿名函数或函数式接口。
  • @Repeatable - Java 8 开始支持,标识某注解可以在同一个声明上使用多次。

1.4 自定义注解

注解分类(根据Annotation是否包含成员变量,可以把Annotation分为两类):

标记Annotation:
没有成员变量的Annotation; 这种Annotation仅利用自身的存在与否来提供信息

元数据Annotation:
包含成员变量的Annotation; 它们可以接受(和提供)更多的元数据;

如何自定义注解?

使用@interface关键字, 其定义过程与定义接口非常类似, 需要注意的是:
   Annotation的成员变量在Annotation定义中是以无参的方法形式来声明的, 其方法名和返回值类型定义了该成员变量的名字和类型,
   而且我们还可以使用default关键字为这个成员变量设定默认值;

二. 自定义注解示例

枚举类:

public enum  TranscationModel {Read, Write, ReadWrite
}

枚举是Java1.5引入的新特性,通过关键字enum来定义枚举类。枚举类是一种特殊类,它和普通类一样可以使用构造器、定义成员变量和方法,也能实现一个或多个接口,但枚举类不能继承其他类。 

示例一:获取类与方法上的注解值

MyAnnotation1自定义注解类

package com.xissl.annotation.demo1;import java.lang.annotation.*;/*** MyAnnotation1注解可以用在类、接口、属性、方法上* 注解运行期也保留* 不可继承*/
@Target({ElementType.TYPE, ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation1 {String name();
}

MyAnnotation2自定义注解类 

package com.xissl.annotation.demo1;import java.lang.annotation.*;/***  MyAnnotation2注解可以用在方法上*  注解运行期也保留*  不可继承*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation2 {TranscationModel model() default TranscationModel.ReadWrite;
}

MyAnnotation3自定义注解类  

package com.xissl.annotation.demo1;import java.lang.annotation.*;/*** MyAnnotation3注解可以用在方法上* 注解运行期也保留* 可继承*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface MyAnnotation3 {TranscationModel[] models() default TranscationModel.ReadWrite;
}

测试代码:

package com.xissl.annotation.demo1;/*** 获取类与方法上的注解值*/
@MyAnnotation1(name = "abc")
public class Demo1 {@MyAnnotation1(name = "xyz")private Integer age;@MyAnnotation2(model = TranscationModel.Read)public void list() {System.out.println("list");}@MyAnnotation3(models = {TranscationModel.Read, TranscationModel.Write})public void edit() {System.out.println("edit");}
}
package com.xissl.annotation.demo1;import org.junit.Test;public class Demo1Test {@Testpublic void list() throws Exception {
//        获取类上的注解MyAnnotation1 annotation1 = Demo1.class.getAnnotation(MyAnnotation1.class);System.out.println(annotation1.name());//abc//        获取方法上的注解MyAnnotation2 myAnnotation2 = Demo1.class.getMethod("list").getAnnotation(MyAnnotation2.class);System.out.println(myAnnotation2.model());//Read//        获取属性上的注解MyAnnotation1 myAnnotation1 = Demo1.class.getDeclaredField("age").getAnnotation(MyAnnotation1.class);System.out.println(myAnnotation1.name());// xyz}@Testpublic void edit() throws Exception {MyAnnotation3 myAnnotation3 = Demo1.class.getMethod("edit").getAnnotation(MyAnnotation3.class);for (TranscationModel model : myAnnotation3.models()) {System.out.println(model);//Read,Write}}
}

list()方法测试效果:
 

edit()方法测试效果:

 

 

示例二:获取到类属性上的注解属性值

TestAnnotation自定义注解类

package com.xissl.annotation.demo2;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;//@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TestAnnotation {String value() default "默认value值";String what() default "这里是默认的what属性对应的值";
}

测试代码:

package com.xissl.annotation.demo2;/*** 获取类属性上的注解属性值*/
public class Demo2 {@TestAnnotation(value = "这就是value对应的值_msg1", what = "这就是what对应的值_msg1")private static String msg1;@TestAnnotation("这就是value对应的值1")private static String msg2;@TestAnnotation(value = "这就是value对应的值2")private static String msg3;@TestAnnotation(what = "这就是what对应的值")private static String msg4;
}
package com.xissl.annotation.demo2;import org.junit.Test;public class Demo2Test {@Testpublic void test1() throws Exception {TestAnnotation msg1 = Demo2.class.getDeclaredField("msg1").getAnnotation(TestAnnotation.class);System.out.println(msg1.value());System.out.println(msg1.what());}@Testpublic void test2() throws Exception{TestAnnotation msg2 = Demo2.class.getDeclaredField("msg2").getAnnotation(TestAnnotation.class);System.out.println(msg2.value());System.out.println(msg2.what());}@Testpublic void test3() throws Exception{TestAnnotation msg3 = Demo2.class.getDeclaredField("msg3").getAnnotation(TestAnnotation.class);System.out.println(msg3.value());System.out.println(msg3.what());}@Testpublic void test4() throws Exception{TestAnnotation msg4 = Demo2.class.getDeclaredField("msg4").getAnnotation(TestAnnotation.class);System.out.println(msg4.value());System.out.println(msg4.what());}
}

 test1方法测试效果

 test2方法测试效果

 test3方法测试效果

 test4方法测试效果

示例三:获取参数修饰注解对应的属性值

IsNotNull自定义注解类

package com.xissl.annotation.demo3;import java.lang.annotation.*;/*** 非空注解:使用在方法的参数上,false表示此参数可以为空,true不能为空*/
@Documented
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface IsNotNull {boolean value() default false;
}

测试代码:

package com.xissl.annotation.demo3;/*** 获取参数修饰注解对应的属性值*/
public class Demo3 {public void hello1(@IsNotNull(true) String name) {System.out.println("hello:" + name);}public void hello2(@IsNotNull String name) {System.out.println("hello:" + name);}
}
package com.xissl.annotation.demo3;import org.junit.Test;import java.lang.reflect.Method;
import java.lang.reflect.Parameter;/*** @author 小李飞刀* @site www.javaxl.com*/
public class Demo3Test {@Testpublic void hello1() throws Exception {Demo3 demo3 = new Demo3();for (Parameter parameter : demo3.getClass().getMethod("hello1", String.class).getParameters()) {IsNotNull annotation = parameter.getAnnotation(IsNotNull.class);if(annotation != null){System.out.println(annotation.value());//true}}}@Testpublic void hello2() throws Exception {Demo3 demo3 = new Demo3();for (Parameter parameter : demo3.getClass().getMethod("hello2", String.class).getParameters()) {IsNotNull annotation = parameter.getAnnotation(IsNotNull.class);if(annotation != null){System.out.println(annotation.value());//false}}}@Testpublic void hello3() throws Exception {
//        模拟浏览器传递到后台的参数 解读@requestParamString name = "zs";Demo3 demo3 = new Demo3();Method method = demo3.getClass().getMethod("hello1", String.class);for (Parameter parameter : method.getParameters()) {IsNotNull annotation = parameter.getAnnotation(IsNotNull.class);if(annotation != null){System.out.println(annotation.value());//trueif (annotation.value() && !"".equals(name)){method.invoke(demo3,name);}}}}
}

hello1方法测试效果:

hello2方法测试效果:

hello3方法测试效果:

 三. aop自定义注解的应用

MyLog自定义注解类

package com.xissl.annotation.aop;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyLog {String desc();
}

MyLogAspect切面类

 

package com.xissl.aspect;import com.xissl.annotation.aop.MyLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;@Component
@Aspect
public class MyLogAspect {private static final Logger logger = LoggerFactory.getLogger(MyLogAspect.class);/*** 只要用到了com.javaxl.p2.annotation.springAop.MyLog这个注解的,就是目标类*/@Pointcut("@annotation(com.xissl.annotation.aop.MyLog)")private void MyValid() {}@Before("MyValid()")public void before(JoinPoint joinPoint) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();logger.debug("[" + signature.getName() + " : start.....]");System.out.println("[" + signature.getName() + " : start.....]");MyLog myLog = signature.getMethod().getAnnotation(MyLog.class);logger.debug("【目标对象方法被调用时候产生的日志,记录到日志表中】:"+myLog.desc());System.out.println("【目标对象方法被调用时候产生的日志,记录到日志表中】:" + myLog.desc());}
}

controller层

package com.xissl.web;import com.xissl.annotation.aop.MyLog;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class LogController {@RequestMapping("/mylog")@MyLog(desc = "这是结合spring aop知识,讲解自定义注解应用的一个案例")public void testLogAspect(){System.out.println("这里随便来点啥");}
}

测试结果:


文章转载自:
http://dinncogalactagogue.ssfq.cn
http://dinncoaccumulative.ssfq.cn
http://dinncoelastically.ssfq.cn
http://dinncobutanone.ssfq.cn
http://dinncosyllabography.ssfq.cn
http://dinncodeliverly.ssfq.cn
http://dinncomonohydroxy.ssfq.cn
http://dinncoendoskeleton.ssfq.cn
http://dinncocallet.ssfq.cn
http://dinncospecifical.ssfq.cn
http://dinncogoober.ssfq.cn
http://dinncotympano.ssfq.cn
http://dinncotorte.ssfq.cn
http://dinncoeudemonia.ssfq.cn
http://dinncocaravan.ssfq.cn
http://dinncosomewise.ssfq.cn
http://dinncoaeromodelling.ssfq.cn
http://dinncounmated.ssfq.cn
http://dinncochaung.ssfq.cn
http://dinncoprelusive.ssfq.cn
http://dinncorefinance.ssfq.cn
http://dinncomacroengineering.ssfq.cn
http://dinncowillfully.ssfq.cn
http://dinncoicerink.ssfq.cn
http://dinncofecula.ssfq.cn
http://dinncocabb.ssfq.cn
http://dinncoreflexological.ssfq.cn
http://dinncovalorize.ssfq.cn
http://dinncobanzai.ssfq.cn
http://dinncoamphipath.ssfq.cn
http://dinncoapplicatively.ssfq.cn
http://dinncoretrude.ssfq.cn
http://dinncobimester.ssfq.cn
http://dinncobontbok.ssfq.cn
http://dinncosemitranslucent.ssfq.cn
http://dinncostreptococcal.ssfq.cn
http://dinncotepidarium.ssfq.cn
http://dinncosaran.ssfq.cn
http://dinncopolyp.ssfq.cn
http://dinncocalamondin.ssfq.cn
http://dinncostepdance.ssfq.cn
http://dinncoquackster.ssfq.cn
http://dinncosemaphoric.ssfq.cn
http://dinncoponce.ssfq.cn
http://dinncoreticulate.ssfq.cn
http://dinncoclinking.ssfq.cn
http://dinncoherbalist.ssfq.cn
http://dinncoparamountship.ssfq.cn
http://dinncohoot.ssfq.cn
http://dinncotrick.ssfq.cn
http://dinncotahr.ssfq.cn
http://dinncokeelboat.ssfq.cn
http://dinncojeopardy.ssfq.cn
http://dinncoiodism.ssfq.cn
http://dinncogermina.ssfq.cn
http://dinncointerstock.ssfq.cn
http://dinncosariwon.ssfq.cn
http://dinncocitizenhood.ssfq.cn
http://dinncoglossa.ssfq.cn
http://dinncoclavioline.ssfq.cn
http://dinncocalaverite.ssfq.cn
http://dinncoburgle.ssfq.cn
http://dinncotattle.ssfq.cn
http://dinncoengraver.ssfq.cn
http://dinncolapactic.ssfq.cn
http://dinncosentimo.ssfq.cn
http://dinncoaccusatory.ssfq.cn
http://dinncoarcograph.ssfq.cn
http://dinncomedian.ssfq.cn
http://dinncoforbidden.ssfq.cn
http://dinncoaerodynamically.ssfq.cn
http://dinncolignite.ssfq.cn
http://dinncofondly.ssfq.cn
http://dinncojin.ssfq.cn
http://dinncolushly.ssfq.cn
http://dinncoelves.ssfq.cn
http://dinncohistography.ssfq.cn
http://dinncoappetiser.ssfq.cn
http://dinncoincreasing.ssfq.cn
http://dinncohepatoflavin.ssfq.cn
http://dinncocentime.ssfq.cn
http://dinncobiowarfare.ssfq.cn
http://dinncouropod.ssfq.cn
http://dinncochockstone.ssfq.cn
http://dinncoarsenical.ssfq.cn
http://dinncojul.ssfq.cn
http://dinnconipponese.ssfq.cn
http://dinnconondiabetic.ssfq.cn
http://dinncoquarreller.ssfq.cn
http://dinncoperoxidize.ssfq.cn
http://dinncocahier.ssfq.cn
http://dinncopilus.ssfq.cn
http://dinncostickpin.ssfq.cn
http://dinncoprofitably.ssfq.cn
http://dinncotoughness.ssfq.cn
http://dinncosquacco.ssfq.cn
http://dinncoanalyst.ssfq.cn
http://dinncocytophysiology.ssfq.cn
http://dinncoyankee.ssfq.cn
http://dinncoarenite.ssfq.cn
http://www.dinnco.com/news/109148.html

相关文章:

  • 免费微网站与公众号平台对接网站推广的途径和方法
  • 绵阳做网站多少钱网站建设公司哪家好?该如何选择
  • 梅州做网站设计公司网站搜索排名优化
  • 东莞长安做网站广告策划方案怎么做
  • 大同网站建设哪里好郑州网站制作公司
  • 吉安市网站建设引擎优化搜索
  • 免费的网站服务器湖南网站建设加盟代理
  • 湛江专业的免费建站最新的疫情防控政策和管理措施
  • 微信小程序开发者文档橘子seo
  • 哪个网站上做ppt比较好看营销型网站建设推广
  • 中国做健身补剂的网站潍坊网站关键词推广
  • 贵州省住房和城乡建设厅网站最新热搜榜
  • wordpress防止css采集360优化大师下载官网
  • 公司网站设计 上海外贸网站平台都有哪些 免费的
  • wordpress会员点数看内容网站优化公司推荐
  • 广州做网站公司哪家好百度网址大全下载到桌面
  • 东莞系统网站建设黑龙江今日新闻
  • wordpress新闻动态插件seoul是什么意思
  • 网站备案时间网络推广免费网站
  • 网站建设功能分为几种好用的推广平台
  • 网站如何做口碑营销什么是网站推广?
  • 阿克苏交通建设局网站营销网络建设
  • 做网站不打广告怎么赚钱粤语seo是什么意思
  • 公司网站建设佛山哪家好seoul怎么读
  • 泸州网站建设百度网盘登录
  • 网站底部悬浮代码重大新闻事件
  • 公司网站页面网站开发教程
  • 甘肃疫情最新消息今天50例seo排名技巧
  • web的盒子模型浙江seo关键词
  • 东莞做微网站建设价格sem推广案例