注解,其实就是这么简单( 三 )
在实际的方法使用中 , 我们只需要给必须要修改的值赋值即可
@myAnnotaion2(name = "Simon")public void test1(){System.out.println("测试注解1");}复制代码因此 , 自定义注解可以归纳如下:
- @interface用来声明一个注解 , 格式:public @interface 注解名{定义内容}
- 其中每一个方法实际上声明了一个配置参数
- 方法的名称就是参数的名称
- 返回值类型就是参数的类型(返回值只能时基本类型 , Class,String,enum)
- 可以通过default用来声明参数的默认值
- 如果只有一个参数成员 , 一般参数名为value
- 注解元素必须要有值 , 我们定义注解元素时 , 经常使用空字符串0作为默认值
5.1参数值是基本类型
- 首次 , 我们创建一个带有参数的注解
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})@Retention(value = http://kandian.youth.cn/index/RetentionPolicy.RUNTIME)@interface MyAnnotation5 {String name() default" ";int age() default 0;}复制代码- 将该注解修饰到某一方法上
@MyAnnotation5(name = "Simon", age = 25)public void testInjectValue(String name,int age) {System.out.println("获取注解中的参数值:");System.out.println(name);System.out.println(age);}复制代码- 反射获取注解中的参数并注入到方法中
//反射获取类 , 并得到类中的方法Class aClass = InjectValue.class;Method method=aClass.getMethod("testInjectValue",String.class,int.class);//获取注解中的属性值MyAnnotation5 myAnnotation5=method.getAnnotation(MyAnnotation5.class);String name=myAnnotation5.name();int age=myAnnotation5.age();//将属性值注入到相应的方法中Object o=aClass.newInstance();method.invoke(o,name,age);复制代码5.2参数值是对象前面我们讲解如何将注解中的参数为基本数据类型注入到方法中 , 那么如何将注解中的参数为对象注入到方法中呢?- 创建一个类用于生成对象
public class Animal {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}复制代码- 创建一个类 , 类中的属性是上一个类的对象 , 并创建一个带参数的注解 , 参数的类型为对象
public class AnimalDao {private Animal animal;public Animal getAnimal() {return animal;}@MyAnnotation6(name = "Dog",age = 12)public void setAnimal(Animal animal) {this.animal = animal;}}复制代码- 获取注解中的对象
public class TestInjectObject {public static void main(String[] args) throws Exception {//1.使用PropertyDescriptor得到想要注入的属性PropertyDescriptor descriptor = new PropertyDescriptor("animal", AnimalDao.class);//2.得到要想注入属性的具体对象Animal animal = (Animal) descriptor.getPropertyType().newInstance();//3.得到该属性的写方法Method method = descriptor.getWriteMethod();//4.得到写方法的注解Annotation annotation = method.getAnnotation(MyAnnotation6.class);//5.得到注解上的信息Method[] methods = annotation.getClass().getMethods();//6.将注解上的信息填充到animal对象上for (Method m : methods) {//得到注解上属性的名字String name = m.getName();//看看animal对象有没有与之对应的方法try {PropertyDescriptor descriptor1 = new PropertyDescriptor(name, Animal.class);Method method1 = descriptor1.getWriteMethod();//得到注解中的值Object o = m.invoke(annotation, null);//调用animal对象的setter方法 , 将注解上的值设置进去method1.invoke(animal, o);} catch (Exception e) {continue;}}AnimalDao animalDao = new AnimalDao();method.invoke(animalDao, animal);System.out.println(animalDao.getAnimal().getName());System.out.println(animalDao.getAnimal().getAge());}}@Target(value = http://kandian.youth.cn/index/{ElementType.METHOD, ElementType.TYPE})@Retention(value = RetentionPolicy.RUNTIME)@interface MyAnnotation6 {String name();int age();}复制代码【注解,其实就是这么简单】因此 , 将对象注入到方法中可以总结如下- 创建想要获得属性的对象
- 根据对象获取该属性的方法
- 得到方法中的注解
- 获取注解中的信息
- 将注解信息注入到对象中
- 将对象中的属性写入到方法中
- 基建|深信服何朝曦:离开安全的“新基建”,就是在沙子上盖高楼
- 超清|带着 vivo S7 逛野生动物园,随手一拍就是超清大片
- 上手|OriginOS上手体验如何?化繁为简就是正确的打开方式!
- 谷歌|小米10i惊现谷歌商店,网友:这不就是我们的Redmi Note 9?
- 黑科技|又一产品被央视曝光,披着“黑科技”外衣,其实在收割智商税
- 天花板|小米10一上市就是天花板,销量突破800万,你入手了吗?
- 不到|半分钟不到,一部手机生产出来了,这就是中国速度、华为速度
- 其实我是个美食家 凯度ST40DZ-A8蒸烤一体机使用测评
- 特斯拉造自行车,也许就是这个样……
- 流畅就是跑分高?vivo选择说不,用这款杀器终结国产手机内卷
