注解
- XML,配置文件的一种格式 .xml ; 解耦性
- 注解,Jdk1.5之后出现 ,耦合性增强了,SSM框架中广泛使用
- 只是一个特殊的标记,注解需要有解析程序;编译期间解析(编译器内部已经写好了),运行期间解析(使用反射进行解析)
- 注解可以标记在类,成员变量,方法,局部变量,形参
1.1.注解分类
1.1.1.内置注解
@Override:重写方法;编译器只要识别到此注解,检查子类的方法和父类的方法签名是否一致
@Deprecated:即将抛弃,不建议使用的
@SuppressWarnings(“”)
1.1.2.元注解
注解的语法
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
//注解名和类名起名一致
}
//本质上是Annotation的子接口
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public interface Override extends Annotation {
//注解名和类名起名一致
}
-
注解的注解
@Target:表示注解可以标记在哪些类型上面
@Retention:注解的保留期
@Doucumented 在生成java API文档的时候,加入注解内容
@Inherited 表示子类可以继承
@Target取值
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
}
@Retention取值
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
*/
SOURCE, // 表示注解在源码中存在,编译器一旦编译完成,丢弃此注解,不会存在与class文件中
/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
*/
CLASS, // 当类加载器把class文件加载到内存中的时候会丢弃此注解
/**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
*
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME // 在运行期间依然保留此注解
}
1.1.3.自定义注解
1.注解的定义
@Target(value = {ElementType.METHOD, ElementType.TYPE})
@Retention(value = RetentionPolicy.SOURCE)
public @interface CustomAnnotation {
// 当成员只有一个的时候,名字是value
int value();
// 可以不给值,使用此默认值
String name() default "anne";
}
注解的成员数据类型:
所有的基本数据类型
String
Class
Enum
注解类型
以上数据类型的数组
注解中成员如果只有一个,名字是value 那么在使用的时候value可以省略直接赋值
2.注解的使用
public class Son extends Father {
private String name;
@Override
public void study() {
}
@CustomAnnotation(12)
public void method() {
}
}
1.1.4. 使用注解模拟Junit4测试
1.定义注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAble {
}
2.在类上标记
public class Student {
@TestAble
public void method1() {
System.out.println("method1 执行了");
}
public void method2() {
System.out.println("method2 执行了");
}
@TestAble
public void method3() {
System.out.println("method3 执行了");
}
public void method4() {
System.out.println("method4 执行了");
}
@TestAble
public void method5() {
System.out.println("method5 执行了");
}
}
3.解析程序,通过反射凡是方法上面有注解的,进行调用
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
Class<?> aClass = Class.forName("com.qy28.sm.annotation.Student");
Object object = aClass.newInstance();
Method[] declaredMethods = aClass.getDeclaredMethods();
for (Method method : declaredMethods) {
if (method.isAnnotationPresent(TestAble.class)) {
method.invoke(object);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/192952.html