当前位置: 首页 > news >正文

【Spring】通过Spring收集自定义注解标识的方法

文章目录

    • 前言
    • 1. 声明注解
    • 2. 使用 Spring 的工厂拓展
    • 3. 收集策略
    • 4. 完整的代码
    • 后记

前言

需求:
用key找到对应的方法实现。使用注解的形式增量开发。

@MyComponent
public class Sample1 {@MyMethod(key = "key1")public String test2() {return "Shenzhen";}
}

任意时刻都能通过key来进行依赖查找

    @Testpublic void test() {Assert.notNull(myBeanFactory.getMethod("key1"), "key1对应的方法不能为空");}

实现思路:

  1. 声明自己的类注解,并要求被 Spring 收集
  2. 声明自己的方法注解,确保可以通过反射获取
  3. 借 Spring 的能力,容器启动收集bean完成后,把bean列表交给自己,用于自己的收集策略。

1. 声明注解

  • 类注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
// 继承Spring的注解,确保类能被Spring扫描
@Component
public @interface MyComponent {@AliasFor(annotation = Component.class)String value() default "";
}
  • 方法注解
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyMethod {// 用于依赖查找的keyString key();
}

2. 使用 Spring 的工厂拓展

找到我们用注解标记的类、方法,Spring 收集完bean之后提供了拓展点供我们遍历这些bean。

  • BeanFactoryPostProcessor 接口
  • 其中 ConfigurableListableBeanFactory beanFactory 提供了容器
@Component
public class MyBeanFactory implements BeanFactoryPostProcessor {@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {// 找到所有注解标记的beanMap<String, Object> beanMap = beanFactory.getBeansWithAnnotation(MyComponent.class);beanMap.forEach((beanName, bean) -> {// 收集bean中用注解标记的方法 (方法先省略)// collectMethod(bean);});}
}

3. 收集策略

  • 收集策略可以很简单,这里就用一个Map收集
  • 用Spring的AnnotationUtils.findAnnotation(method, MyMethod.class) 命中标识的方法
  • 如果命中,则拿到注解中的key,收集key和Method的映射关系
    // 自己的容器private static final Map<String, Method> METHOD_MAP = new HashMap<>();public  Method getMethod(String key) {return METHOD_MAP.get(key);}private void collectMethod(Object bean) {Method[] methods = bean.getClass().getDeclaredMethods();Arrays.stream(methods)// 过滤: 只要MyMethod标识的方法 .filter(method -> Objects.nonNull(getAnnotation(method)))// 收集: 通过MyMethod的注解key,绑定依赖关系,放到自己的容器.forEach(method -> METHOD_MAP.putIfAbsent(getAnnotation(method).key(), method));}private static MyMethod getAnnotation(Method method) {return AnnotationUtils.findAnnotation(method, MyMethod.class);}

4. 完整的代码

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;@Component
public class MyBeanFactory implements BeanFactoryPostProcessor {private static final ConcurrentHashMap<String, Method> METHOD_MAP = new ConcurrentHashMap<>();@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {// 从Spring容器中获取所有MyControllerMap<String, Object> beanMap = beanFactory.getBeansWithAnnotation(MyComponent.class);beanMap.forEach((beanName, bean) -> {// 收集bean里面的所有可用方法collectMethod(bean);});}private void collectMethod(Object bean) {Method[] methods = bean.getClass().getDeclaredMethods();Arrays.stream(methods)// 过滤: 只要MyMethod标识的方法 .filter(method -> Objects.nonNull(getAnnotation(method)))// 收集: 通过MyMethod的注解key,绑定依赖关系,放到自己的容器.forEach(method -> METHOD_MAP.putIfAbsent(getAnnotation(method).key(), method));}private static MyMethod getAnnotation(Method method) {return AnnotationUtils.findAnnotation(method, MyMethod.class);}public  Method getMethod(String key) {return METHOD_MAP.get(key);}
}

后记

记录下用到的 Spring 的 api 和工具

  • 一个核心的接口方法获取bean容器
    BeanFactoryPostProcessor#postProcessBeanFactory

  • 获取指定注解修饰的类
    ConfigurableListableBeanFactory#getBeansWithAnnotation

  • 获取方法上的注解内容
    AnnotationUtils#findAnnotation

http://www.lryc.cn/news/329099.html

相关文章:

  • 基于深度学习的图书管理推荐系统(python版)
  • MATLAB 点云随机渲染赋色(51)
  • 通过一篇文章让你完全掌握VS和电脑常用快捷键的使用方法
  • ChatGPT指引:借助ChatGPT撰写学术论文的技巧
  • 魔改一个过游戏保护的CE
  • rust嵌入式开发之await
  • UE4_碰撞_碰撞蓝图节点——Line Trace For Objects(对象的线条检测)
  • 抽象类和接口的简单认识
  • python-pytorch获取FashionMNIST实际图片标签数据集
  • 深入探秘Python生成器:揭开神秘的面纱
  • 红队攻防渗透技术实战流程:红队目标信息收集之批量信息收集
  • 【vue3学习笔记(二)】(第141-143节)初识setup;ref函数_处理基本类型;ref函数_处理对象类型
  • 若依框架学习使用
  • 蓝桥杯_数学模板
  • 稀碎从零算法笔记Day31-LeetCode:接雨水
  • 微前端的使用和注意事项 - qiankun
  • uniapp微信小程序消息订阅详解
  • git 查看文件夹结构树
  • 设计模式一详解
  • python 进程、线程、协程基本使用
  • SQLite3进行数据库各项常用操作
  • Debian GNU/Linux 安装docker与docker compose
  • 图片标注编辑平台搭建系列教程(2)——fabric.js简介
  • Debian linux版本下运行的openmediavault网盘 千兆网卡升级万兆
  • 前端 CSS 经典:grid 栅格布局
  • 多输入多输出通道
  • http响应练习—在服务器端渲染html(SSR)
  • C++(8): std::deque的使用
  • openwrt开发包含路由器基本功能的web问题记录
  • HarmonyOS ArkTS 骨架屏加载显示(二十五)