注解的理解:
基本介绍:
使用Annotation 时要在其前面增加 @ 符号,并把 Annotation 当成一个修饰符使用。用于修饰它支持阿程序元素。
三个基本的 Annotation:
@Override:限定某个方法,是重写父类方法,该注解只能用于方法。
class Father {
public void fly() {
}
}
class Son extends Father {
//1. @Override 注解放在fly方法上,表示子类的fly方法重写父类的fly
//2. 如果这里没有写 @Override 还是会重写父类的fly
//3. 如果你写了 @Override注解,编译器就会去检查该方法是否真的重写了父类的方法
//如果的确重写了,编译通过,如果没有构成重写则编译错误
//4. 看看@Override的源码
//如果发现 @interface 表示 一个注解类
/*
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
*/
@Override
public void fly() {
System.out.println("Son fly....");
}
}
补充说明:@interface 的说明,@interface 不是接口,是注解类 是jdk1.5 之后加入的。
@Override 使用说明
@Override 表示是指定重写父类的方法(从编译层面验证),如果父类没有fly方法,或者没有重写方法,则会报错;
如果不写 @Override 注解,而父类仍有 public void fly(){},仍然构成重写;
@Override 只能修饰方法,不能修饰其他类,包,属性等等;
查看 @Override 注解源码@Target(ElementType.METHOD),说明只能修饰方法;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
@Target 是修饰注解阿注解,称为元注解;
@Deprecated:用于表示某个程序元素(类,方法等)已过时。
public class Deprecated_ {
public static void main(String[] args) {
A a = new A();
}
}
//1. @Deprecated 修饰某个元素,表示该元素已经过时了
//2. 即不再推荐使用,但是仍然可以使用
//3. 查看源码
/*
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}
*/
//4. 可以修饰方法,类,字段,包,参数等等
//5. @Deprecated 可以做版本升级过渡使用
@Deprecated
class A{
public int n1;
public void hi(){}
}
@SuppressWarnings:抑制编译器警告。
public class SuppressWarnings_ {
//1. 当我们不希望看到这些警告的时候,可以使用 @SuppressWarnings 注解来抑制警告信息
//2. 在{""}中,可以写入你希望抑制(不显示警告信息)
//3. 关于SuppressWarnings 作用范围是和你放置的位置相关
// 比如@SuppressWarnings放置在 main方法,那么一致警告的范围就是 main
// 通常我们可以放置具体大的语句,方法和类
//4. 看看 @SuppressWarnings 的源码
/*
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
String[] value();
}
*/
@SuppressWarnings({"rawtypes","unchecked","unused",})
public static void main(String[] args) {
List list = new ArrayList();
list.add("jack");
list.add("tom");
list.add("mary");
int i;
System.out.println(list.get(1));
}
}
使用说明
| all | 抑制所有警告 |
|---|---|
| boxing | 抑制装箱、拆箱操作时候的警告 |
| cast | 抑制映射相关的警告 |
| dep-ann | 抑制启用注释的警告 |
| deprecation | 抑制过期方法警告 |
| fallthrough | 抑制在 switch 中缺失 breaks 的警告 |
| finally | 抑制 finally 模块没有返回的警告 |
| hiding | 抑制相对于隐藏变量的局部变量的警告 |
| incomplete-switch | 忽略不完整的 switch 语句 |
| nls | 忽略非 nls 格式的字符 |
| null | 忽略对 null 的操作 |
| rawtypes | 使用 generics 时忽略没有指定相应的类型 |
| restriction | 抑制禁止使用劝阻或禁止引用的警告 |
| serial | 忽略在 serializable 类中没有声明 serialVersionUID 变量 |
| static-access | 抑制不正确的静态访问方式警告 |
| synthetic-access | 抑制子类没有按最优方法访问内部类的警告 |
| unchecked | 抑制没有进行类型检查操作的警告 |
| unqualified-field-access | 抑制没有权限访问的域的警告 |
| unused | 抑制没被使用过的代码的警告 |

//以下为 @Retention 注解源码
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
/**
* Returns the retention policy.
* @return the retention policy
*/
RetentionPolicy value();
}
//以下为 @Target 注解源码
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
/**
* Returns an array of the kinds of elements an annotation type
* can be applied to.
* @return an array of the kinds of elements an annotation type
* can be applied to
*/
ElementType[] value();
}
//修饰的元素的类型
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,
/** Formal parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE,
/**
* Type parameter declaration
*
* @since 1.8
*/
TYPE_PARAMETER,
/**
* Use of a type
*
* @since 1.8
*/
TYPE_USE
}
//以下为 @Documented 注解源码
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}
基本说明:被他修饰的Annotation 将具有继承性,如果某个类使用了 @Inherited 修饰的 Annotation,则子类将自动具有该注解。
如何在ruby中模拟类Java注解?(好吧,我有答案了,概括一下http://bens.me.uk/2009/java-style-annotations-in-ruby) 最佳答案 本文改编自apieceofcodeIwroteinananswertoanotherquestion几个星期前,虽然它当然不是原创的。这是一个著名的Ruby习语,毕竟它已经使用了很多年,至少从rakes的desc方法开始.moduleAnnotationsdefannotations(meth=nil)return@__annotations__[me
基于Spring注解+MyBatis+Servlet实现数据库交换的小小Demo第一步创建web项目,这一步省略,有不会的可以参考之前发布的文档第二步配置pom.xml文件dependencies>dependency>groupId>org.springframeworkgroupId>artifactId>spring-contextartifactId>version>5.2.9.RELEASEversion>dependency>dependency>groupId>org.springframeworkgroupId>artifactId>spring-aspectsartifact
我正在做这个教程:https://youtu.be/qs2n_poLarc?list=WL并且正在尝试学习ionic框架。问题是教程(根据我阅读的内容)有点过时了。视频的作者使用了import{HttpModule}from"@angular/http,但我在StackOverflow上读到我应该使用import{HttpClient}from"@angular/common/http";。问题是当我尝试编译代码时出现此错误:模块“AppModule”导入的意外值“HttpClient”。请添加@NgModule注释。。现在我不知道应该在哪里添加它,因为我的app.module.ts看
文章目录前言注意实现测试环境验证自带的注解自定义valid注解自定义注解和处理类创建参数接收类,并增加字段注解接口中使用自测环节正常测试异常测试自定义全局异常监听扩展递归参数下valid不识别的坑前言再项目开发中,针对前端传递的参数信息,有些接口中需要写大量的if判断,导致代码臃肿,不够优雅。此时,可以使用@Valid实现基本的字段校验。注意实现springboot2.3之前,直接进行开发即可,无需引用额外的依赖集成在spring-boot-starter-web中。springboot2.3之后需要额外引入spring-boot-starter-validation依赖信息测试环境sprin
目录@[TOC](目录)Service注解Service用法及示例传统方式是怎么做的呢?@Service注解是怎么体现业务逻辑复用的?总结Service注解@Service注解是SpringFramework中的一种注解,它标识了这个类是一个业务逻辑层的服务Bean。这意味着当Spring应用启动时,该Bean会被自动创建并加入到Spring应用上下文中。简而言之,@Service注解是一种用于标记服务层Bean的注解,是在SpringBoot应用中实现业务逻辑复用的重要方法之一。Service用法及示例使用@Service注解可以将一个类声明为业务逻辑组件,并将其对象存入Spring容器中。
我正在设置一个kubernet集群来部署我们的容器应用程序。应用程序实际上需要所有标签,但标签超过63个字符,我得到一个error.这使我依赖于注释。服务的注释如下所示:com.example.development.london/component.proxy-config.secure-routes.backend.proxy-path。/仅用于绕过RFC域错误。在Golang应用程序中,请求命名空间的所有服务。实际上基于标签。为此,到目前为止我使用了以下代码。func(kc*KubernetesCollector)generateRoutes(errorChannelchan但是
在go中,是否可以检索结构的变量注释?考虑以下结构:typeATypestruct{IDstring`xml:"my_id"`Datestring`xml:"creation_ts"`}如何使用反射检索ID字段的xml:"my_id"部分?以下将打印变量的名称、类型和值,但不打印注释。s:=reflect.ValueOf(&aType).Elem()typeOfT:=s.Type()fori:=0;i谢谢, 最佳答案 它作为StructField.Tag可用,所以f.Tag引用资料:reflect.StructFieldreflec
谁能用简单的英语解释一下下面两段的意思?(取自http://www.ibm.com/developerworks/java/library/j-cwt08025.html)"Annotationsaremoreflexibleintermsofhowyouusethem,withoptionsforwhethertheannotationinformationistobeincludedinclassfilesoutputbythecompilerandmadeavailabletotheapplicationatruntime"不知道这些是什么意思。Annotations是否可以配置
我正在使用RESTEasy库编写一个RESTfulWS应用程序。我一直在寻找一种动态修改特定模型/xml映射的方法,并找到了MOXY的JAXB实现。问题是当我使用文件jaxb.properties指定MOXY的实现时,当我尝试检索JAXBContext时,出现以下异常:javax.xml.bind.JAXBException:ExceptionDescription:Namecollision.TwoclasseshavetheXMLtypewithurihttp://www.w3.org/2001/XMLSchemaandnameanyType.-withlinkedexceptio
我正在尝试将一些JAXBxjc.exe生成的类转换为简单XML类。我不确定如何注释动态元素。例如,在架构中,我有:当通过xjc.exe运行时,为@XmlElement生成以下注释@XmlElements({@XmlElement(name="HostQueryRq",type=HostQueryRqType.class),@XmlElement(name="CompanyQueryRq",type=CompanyQueryRqType.class),@XmlElement(name="CompanyActivityQueryRq",type=CompanyActivityQueryRq