首先我们可以自己写一个注解:

@target(elementtype.method)
@retention(retentionpolicy.runtime)
public @interface annosample {
    string value();
}

注解使用 @interface来标识。这个注解定义了一个属性value,只能作用于方法上,生命周期是运行时。

@target用于指定可以放置注解的位置,这里指定的method说明该注解只能放置到方法上面,还可以指定type(类、接口、枚举类),field实例,parameter形参,constructor构造器等等
@retention用于定义注解的生命周期:source是编译期间丢弃。编译完成后,这些注释没有任何意义。class类加载期间丢弃,这是默认值。runtime不会丢弃,可以在运行时使用反射去获取

那么我们就使用该注解:

public class helloworld {
    
    @annosample(value = "hello")
    public void hello(){
        system.out.println("hello,world");
    }
}

到此,创建一个注解并使用它我们已经完成了。

但是我们可以发现,该注解并没有带来任何的改变,有这个注解和没有这个注解区别不大。那么,我们需要知道,注解本身只能是被看作元数据,它不包含任何业务逻辑。注解更像是一个标签,一个声明,表面被注释的这个地方,将具有某种特定的逻辑

注解让这个方法有了一个标签,让我们知道应该去这个地方加一点逻辑。那么怎么去获取这个标签呢?
可以使用反射

  • 利用反射机制获取一个类的 class 对象 通过这个 class 对象可以去获取他的每一个方法
  •  method,或字段 field 等等method,field 等类提供了类似于 getannotation() 的方法来获取这个字段或者方法的所有注解
  • 拿到注解之后,我们可以判断这个注解是否是我们要实现的注解,如果是则实现注解逻辑

具体实现如下:

public class main {
    public static void main(string[] args) throws exception {
        class c=class.forname("helloworld");
        method[] methods = c.getmethods();
        for (method method : methods) {
            annotation[] annotations = method.getdeclaredannotations();
            for (annotation annotation : annotations) {
                if (annotation.annotationtype()==annosample.class) {
                    system.out.println(((annosample)annotation).value());
                }
            }
        }
    }
}

上面代码就是,通过反射获得前面所写的helloworld类的method数组并且遍历,并且遍历每个方法上的所有注解,如果找到我们需要判断的注解if (annotation.annotationtype()==annosample.class)那么就做一些逻辑处理,这里是打印出value的值

既然已经了解了注解的基础知识,那么我们去看看spring的@autowired是怎么实现的

@autowired

看下源码:

@target({elementtype.constructor, elementtype.method, elementtype.parameter, elementtype.field, elementtype.annotation_type})
@retention(retentionpolicy.runtime)
@documented
public @interface autowired {

	/**
	 * declares whether the annotated dependency is required.
	 * <p>defaults to {@code true}.
	 */
	boolean required() default true;

}

解读一下,该注解可以用在构造器、实例方法、形参、实例变量、注解上,生命周期是运行时。这里的 @documented只是表明是否在java doc中添加注解。
可以知道,@autowired注解本身并没有什么特别的,重要的是应该是关于这个注解的特定逻辑。
逻辑所在的类,就在源码上面有提示了:

连续两次使用 shift进行全局搜索查询这个类。

其中的buildautowiringmetadata()方法是逻辑所在:

第一个箭头是得到当前的class,然后第二个箭头就是去判断 targetclass中的所有filed,查看是否有@autowired。 下面的dowithlocalmethods和这里判断 filed类似。
通过了@autowired判断之后,执行如下

currelements.add(new autowiredfieldelement(field, required));

这是将该字段放入一个容器中去,因为使用了 @autowired的实例变量/方法不止一个,所以全部找出之后进行判断。

在该方法的最后:

返回的是这个类和使用了@autowired注解的实例集合。返回的是这个,那么下一步应该就是对其进行注入了。

注入的逻辑在postprocessproperties()方法中:

可以看到这个方法中的第一个就是调用 findautowiringmetadata()方法,然后这个方法里面又调用了我们前面分析的buildautowiringmetadata(),也就是说我们得到了类及其注解信息,然后开始调用下面的inject()方法进行注入

可以看到,对于字段,那么就调用反射类field的set()方法设置值

field.set(target, getresourcetoinject(target, requestingbeanname));

对于方法,就使用invoke去带入具体的参数值进行执行:

method.invoke(target, getresourcetoinject(target, requestingbeanname));

getresourcetoinject()方法的参数就是要注入的 bean 的名字,这个方法的功能就是根据这个 bean 的名字去拿到它。

到此这篇关于理解java注解及spring的@autowired是如何实现的的文章就介绍到这了,更多相关java注解spring的@autowired内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!