自定义注解
1.创建自定义注解
import java.lang.annotation.*;
public class Test {
//注解可以显示赋值, 如果没有默认值, 我们就必须给注解赋值
@MyAnnotation(age = 18)
public void test() {
}
}
//定义一个注解
//表示我们的注解可以用在哪些地方
@Target(value = {ElementType.METHOD, ElementType.TYPE_USE})
//表示我们的注解在什么地方还有效
@Retention(value = RetentionPolicy.RUNTIME)
//表示废弃存在隐患,可以使用,但不建议继续使用
@Deprecated
//表示是否将我们的注解生成JAVAdoc
@Documented
//子类可以继承父类的注解
@Inherited
@interface MyAnnotation {
//注解的参数: 参数类型 + 参数名();
String name() default "";
int age() default 0;
int id() default -1;//如果默认值为-1,代表不存在
String[] schools() default {"北京大学", "清华大学"};
}
AOP注解
增强处理(Advice)
前置增强
后置增强
环绕增强/异常抛出增强/最终增强等类型
切入点(Pointcut)
连接点(Join Point)
切面(Aspect)
目标对象(Target object)
AOP代理(AOP proxy)
织入(Weaving)
2.反射获取注解
package shejimoshi;
import java.lang.annotation.*;
import java.lang.reflect.Field;
public class Test2 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class c1 = Class.forName("shejimoshi.Student");
//通过反射获得注解
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
//获得注解value的值
TableKai annotation1 = (TableKai) c1.getAnnotation(TableKai.class);
String value = annotation1.value();
System.out.println(value);
//获得类指定的注解
Field name = c1.getDeclaredField("name");
FieldKai annotation2 = name.getAnnotation(FieldKai.class);
System.out.println(annotation2.cloumnName());
System.out.println(annotation2.type());
System.out.println(annotation2.length());
}
}
@TableKai("db_student")
class Student{
@FieldKai(cloumnName = "db_id",type = "int",length = 32)
private int id;
@FieldKai(cloumnName = "db_age",type = "int",length = 3)
private int age;
@FieldKai(cloumnName = "db_name",type = "int",length = 50)
private String name;
public Student() {
}
public Student(String name,int id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableKai{
String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldKai{
String cloumnName();
String type();
int length();
}
3.反射获取方法属性
package shejimoshi;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
//获得class对象
Class c1 = Class.forName("shejimoshi.User");
//构造一个对象
//本质是调用了类的无参构造
User user = (User) c1.newInstance();
System.out.println(user);
//通过构造器创建对象
Constructor constructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
User user2 = (User) constructor.newInstance("瑞雯", 1, 18);
System.out.println(user2);
//通过反射调用普通方法
User user3 = (User) c1.newInstance();
//通过反射获取方法
Method setName = c1.getDeclaredMethod("setName", String.class);
//invoke: 激活的意思
//(对象,"方法的值")
setName.invoke(user3,"蒙多");
System.out.println(user3.getName());
//通过反射调用普通方法
User user4 = (User) c1.newInstance();
Field name = c1.getDeclaredField("name");
//不能直接操作私有属性,需要关闭程序的安全检测,但是会降低性能
//检查是否开启,默认开启, true为关闭
name.setAccessible(true);
name.set(user4,"瑞雯2");
System.out.println(user4.getName());
}
}
class User {
private String name;
private int id;
private int age;
public User() {
}
public User(String name,int id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}