【保姆级教程】自定义注解实现 @Autowired 同款功能

2023-05-16

前置的啰啰嗦嗦

本文提供一个超级简单、超级简单、超级简单的约等于无脑创建自定义注解实现@Autowired同款功能的方法,想立马实现可直接跳到第二部分

我们既然想自己搞一个跟@Autowired功能一样的注解,最简单的做法其实就是模仿@Autowired的写法,不知道有没有客官记得之前介绍过的applyMergedBeanDefinitionPostProcessors()方法,里面介绍了是在AutowiredAnnotationBeanPostProcessor中对@Autowired注解进行解析的,所以我们想要给我们自定义的注解实现一样的功能就可以参考AutowiredAnnotationBeanPostProcessor这个类

  • 参考:https://blog.csdn.net/aqin1012/article/details/128633664

这次实践我们一共需要创建6个文件:

  • 1个自定义注解类(AqinAutowired.java

  • 1个自定义注解的BeanPostProcessor(MyAnnotationBeanPostProcessor.java

  • 2个测试类(MyAnnotationController.javaMyAnnotationService.java

  • 1个配置文件(populateBean.xml

  • 1个启动类(Test.java

着急想看到效果的同学可以直接无脑copy我贴出的代码,先执行成功开心一下(或者直接往下看我的截的执行结果颅内兴奋下也成,我把分析放在最后,有兴趣自行前往观看

文件总览

8b56f374e38133e6480ecc4467190120.png

AqinAutowired.java

afc4fa4d3428dee37d7a0a3cf2c95aa5.png
package com.aqin.custom.annotation;
 
import java.lang.annotation.*;
 
/** * @Description* @Authoraqin1012 AQin. * @Date11/10/22 1:08 PM * @Version1.0 */
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AqinAutowired {/**    * Declares whether the annotated dependency is required.    * <p>Defaults to {@codetrue}.    */boolean required()default true;
}

插播涉及到的元注解的简介(简单得不能再简单叻(˶‾᷄ ⁻̫ ‾᷅˵))

  • @Target 括号内加能作用的对象

  • @Retention 控制生命周期

  • @Documented 生成Javadoc

MyAnnotationBeanPostProcessor.java

这个代码太长了,其实就是除了包类名全部复制AutowiredAnnotationBeanPostProcessor这个类,然后哪里报错处理下就好,这个代码太长就不全贴叻(以免觉得我在水字数=_=),真正需要修改的部分其实只有构造函数的部分

将构造函数中的代码改为下图中红框框内的即可(下图中上半部分是AutowiredAnnotationBeanPostProcessor这个类原先的构造函数,下半部分为MyAnnotationBeanPostProcessor中修改后的构造函数,嘿嘿解释得真详细我真体贴(˶‾᷄ ⁻̫ ‾᷅˵))

6a940ccccf4290726726da6970eaeb06.png
this.autowiredAnnotationTypes.add(AqinAutowired.class);

MyAnnotationController.java

f766feb696a754cb48547028dd4b463e.png
package com.aqin.custom.annotation;
 
/** 
* @Description
* @Authoraqin1012 AQin. 
* @Date11/10/22 5:07 PM 
* @Version1.0 
*/
public class MyAnnotationController {
    @AqinAutowired
   private MyAnnotationService myAnnotationService;
 
   public void testMyAnnotation(){myAnnotationService.test();
   }
}

MyAnnotationService.java

9e37b10f4374daab892147f80a6700f0.png
package com.aqin.custom.annotation;
 
import org.springframework.stereotype.Service;
 
/** 
* @Description
* @Authoraqin1012 AQin. 
* @Date11/10/22 5:08 PM 
* @Version1.0 
*/
public class MyAnnotationService {
    public void test() {
        System.out.println("(*≧ω≦)~");
    }
}

populateBean.xml

6cfca4973fe777ac8aa6155dfc265fcb.png
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
 
   <bean id="myAnnotationBeanPostProcessor"
        class="com.aqin.custom.annotation.MyAnnotationBeanPostProcessor"></bean>
   <bean id="myAnnotationController" class="com.aqin.custom.annotation.MyAnnotationController"></bean>
   <bean id="myAnnotationService" class="com.aqin.custom.annotation.MyAnnotationService"></bean>
 
</beans>

Test.java

701c31ac28d158a21f7ab7e2433eeb53.png
package com.aqin.custom.annotation;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
/**
 * @Description
 * @Author aqin1012 AQin.
 * @Date 11/11/22 9:50 AM
 * @Version 1.0
 */
public class Test {
   public static void main(String[] args) {
      ApplicationContext applicationContext = new ClassPathXmlApplicationContext("populateBean.xml");
      MyAnnotationController bean = applicationContext.getBean(MyAnnotationController.class);
      bean.testMyAnnotation();
   }
}

执行结果

3a352db8f7549e498284d062d8b8e864.png

过程分析

applyMergedBeanDefinitionPostProcessors()方法中进行MergedBeanDefinitionPostProcessor的实例遍历时,遍历到AutowiredAnnotationBeanPostProcessor,然后进入它的postProcessMergedBeanDefinition()方法一样,我们自定义的的这个MyAnnotationBeanPostProcessor也实现了MergedBeanDefinitionPostProcessor接口(如下图可以看到我们创建的MyAnnotationBeanPostProcessor

9f50793d9ca04b5dbf9f4a55eaeca3cb.png

在当前beanName为MyAnnotationBeanPostProcessor时,我们进入applyMergedBeanDefinitionPostProcessors()方法(如下图)

426afe3c44c6d8ab1d0523e05129baf8.png

当参数bp为MyAnnotationBeanPostProcessor

d40ad07d3d3d3461e7e36c850aae9751.png

此时进入的就是MyAnnotationBeanPostProcessor中的postProcessMergedBeanDefinition()

5ea323a805ae963f51cf551d2428a8d0.png

然后逻辑与AutowiredAnnotationBeanPostProcessor中的一模一样(嘿嘿毕竟我们代码都是直接搬过来的⁄(⁄ ⁄ ⁄ω⁄ ⁄ ⁄)⁄)

进入findAutowiringMetadata() -> buildAutowiringMetadata()

e0e479b333b582d2e1e249ee4142e210.png

找出带有当前注解的字段/方法

bce96bd767af515c4596d653f1b55a9a.png

客官┏ (^ω^)=记不记得这个autowiredAnnotationTypes,还记得上面我们在复制AutowiredAnnotationBeanPostProcessor的代码到MyAnnotationBeanPostProcessor中以后,除了处理报错内容还改了些什么吗?

对,构造函数,我们把原先构造函数中添加autowiredAnnotationTypes中的注解类换成了我们自定义的注解类AqinAutowired.class,所以在这次的逻辑中,会将被注解AqinAutowired修饰的字段/方法添加进特定存放的集合

来源:blog.csdn.net/aqin1012/article/

details/128938759

推荐

Java面试题宝典

技术内卷群,一起来学习!!

4fe0393fdd34c806ce8ba8ce5e3b41a5.jpeg

PS:因为公众号平台更改了推送规则,如果不想错过内容,记得读完点一下“在看”,加个“星标”,这样每次新文章推送才会第一时间出现在你的订阅列表里。点“在看”支持我们吧!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【保姆级教程】自定义注解实现 @Autowired 同款功能 的相关文章

随机推荐