资讯专栏INFORMATION COLUMN

SpringBoot 动态代理|反射|注解(四)- 动态代理对象注入到Spring容器

lingdududu / 2544人阅读

摘要:上一篇动态代理反射注解优化代码三注解本篇我们将实现通过代理生成的对象注入到容器中。单元测试优化代码待续参考文章

上一篇:SpringBoot 动态代理|反射|注解|AOP 优化代码(三)-注解

本篇我们将实现通过代理生成的对象注入到spring容器中。
首先需要实现BeanDefinitionRegistryPostProcessor, ApplicationContextAware两个接口,作用分别为:
ApplicationContextAware:可以获得ApplicationContext对象,然后获取Spring容器中的对象
BeanDefinitionRegistryPostProcessor+ FactoryBean:可以将我们自定义的bean注入到spring容器

</>复制代码

  1. @Slf4j
  2. @Component
  3. public class HandlerBeanDefinitionRegistry implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware {
  4. private ApplicationContext applicationContext;
  5. @Override
  6. public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
  7. /**
  8. * 获取AutoImpl注解的接口,这些接口就需要通过动态代理提供默认实现
  9. */
  10. Set> classes = getAutoImplClasses();
  11. for (Class clazz : classes) {
  12. /**
  13. * 获取继承自HandlerRouter的接口的泛型的类型typeName,传入到DynamicProxyBeanFactory
  14. * 以便传入到DynamicProxyBeanFactory扫描typeName的实现类,然后按照feign和url两种实现
  15. * 方式分类
  16. */
  17. Type[] types = clazz.getGenericInterfaces();
  18. ParameterizedType type = (ParameterizedType) types[0];
  19. String typeName = type.getActualTypeArguments()[0].getTypeName();
  20. /**
  21. * 通过FactoryBean注入到spring容器,HandlerInterfaceFactoryBean实现以下功能:
  22. * 1.调用动态代理DynamicProxyBeanFactory提供HandlerRouter子接口的默认实现
  23. * 2.将第一步的默认实现,注入到spring容器
  24. */
  25. HandlerRouterAutoImpl handlerRouterAutoImpl = clazz.getAnnotation(HandlerRouterAutoImpl.class);
  26. BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
  27. GenericBeanDefinition definition = (GenericBeanDefinition) builder.getRawBeanDefinition();
  28. definition.getPropertyValues().add("interfaceClass", clazz);
  29. definition.getPropertyValues().add("typeName", typeName);
  30. definition.getPropertyValues().add("context", applicationContext);
  31. definition.setBeanClass(HandlerInterfaceFactoryBean.class);
  32. definition.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_TYPE);
  33. beanDefinitionRegistry.registerBeanDefinition(handlerRouterAutoImpl.name(), definition);
  34. }
  35. }
  36. @Override
  37. public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
  38. log.info("------------------------>postProcessBeanFactory");
  39. }
  40. /**
  41. * 通过反射扫描出所有使用HandlerRouterAutoImpl的类
  42. * @return
  43. */
  44. private Set> getAutoImplClasses() {
  45. Reflections reflections = new Reflections(
  46. "io.ubt.iot.devicemanager.impl.handler.*",
  47. new TypeAnnotationsScanner(),
  48. new SubTypesScanner()
  49. );
  50. return reflections.getTypesAnnotatedWith(HandlerRouterAutoImpl.class);
  51. }
  52. @Override
  53. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  54. this.applicationContext = applicationContext;
  55. log.info("------------------->setApplicationContext");
  56. }
  57. /**
  58. * 通过class获取所有该类型的bean
  59. *
  60. * @param clazz
  61. * @return
  62. */
  63. private Map getBeans(Class clazz) {
  64. return applicationContext.getBeansOfType(clazz);
  65. }
  66. private String getYmlProperty(String propery) {
  67. return applicationContext.getEnvironment().getProperty(propery);
  68. }
  69. }

HandlerInterfaceFactoryBean 通过动态代理创建默认实现类

</>复制代码

  1. @Slf4j
  2. @Data
  3. public class HandlerInterfaceFactoryBean implements FactoryBean {
  4. private Class interfaceClass;
  5. private String typeName;
  6. private ApplicationContext context;
  7. @Override
  8. public T getObject() throws Exception {
  9. Object object = DynamicProxyBeanFactory.newMapperProxy(typeName, context, interfaceClass);
  10. return (T) object;
  11. }
  12. @Override
  13. public Class getObjectType() {
  14. return interfaceClass;
  15. }
  16. @Override
  17. public boolean isSingleton() {
  18. return true;
  19. }
  20. }

DynamicProxyBeanFactory 最终实现

</>复制代码

  1. @Slf4j
  2. public class DynamicProxyBeanFactory implements InvocationHandler {
  3. private String className;
  4. private ApplicationContext applicationContext;
  5. private Map clientMap = new HashMap<>(2);
  6. public DynamicProxyBeanFactory(String className, ApplicationContext applicationContext) {
  7. this.className = className;
  8. this.applicationContext = applicationContext;
  9. }
  10. @Override
  11. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  12. if (clientMap.size() == 0) {
  13. initClientMap();
  14. }
  15. Integer env = (Integer) args[0];
  16. return 1 == env.intValue() ? clientMap.get(ClientType.FEIGN) : clientMap.get(ClientType.URL);
  17. }
  18. private void initClientMap() throws ClassNotFoundException {
  19. //获取classStr 接口的所有实现类
  20. Map classMap = applicationContext.getBeansOfType(Class.forName(className));
  21. log.info("DynamicProxyBeanFactory className:{} impl class:{}",className,classMap);
  22. for (Map.Entry entry : classMap.entrySet()) {
  23. //根据ApiClientType注解将实现类分为Feign和Url两种类型
  24. ApiClient apiClient = entry.getValue().getClass().getAnnotation(ApiClient.class);
  25. if (apiClient == null) {
  26. continue;
  27. }
  28. clientMap.put(apiClient.type(), entry.getValue());
  29. }
  30. log.info("DynamicProxyBeanFactory clientMap:{}",clientMap);
  31. }
  32. public static T newMapperProxy(String classStr,ApplicationContext applicationContext,Class mapperInterface) {
  33. ClassLoader classLoader = mapperInterface.getClassLoader();
  34. Class[] interfaces = new Class[]{mapperInterface};
  35. DynamicProxyBeanFactory proxy = new DynamicProxyBeanFactory(classStr,applicationContext);
  36. return (T) Proxy.newProxyInstance(classLoader, interfaces, proxy);
  37. }
  38. }

以上便是注解获取目标类,动态代理提供默认实现,并注入到Spring容器的核心代码。

单元测试

</>复制代码

  1. @Slf4j
  2. @SpringBootTest
  3. @RunWith(SpringRunner.class)
  4. public class OptimizationTest {
  5. @Autowired
  6. @Qualifier("deviceHandlerRouter")
  7. private DeviceHandlerRouter deviceHandlerRouter;
  8. @Test
  9. public void dispatchApp() {
  10. DeviceHandler deviceHandlerFeignImpl = deviceHandlerRouter.getHandler(1, null);
  11. log.info("DeviceHandler-------------->{}",deviceHandlerFeignImpl);
  12. DeviceHandler deviceHandlerUrlImpl = deviceHandlerRouter.getHandler(2, null);
  13. log.info("DeviceHandler-------------->{}",deviceHandlerUrlImpl);
  14. }
  15. }

Aop优化代码待续

参考文章https://blog.csdn.net/qq_2059...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/75310.html

相关文章

  • SpringBoot 动态代理|反射|注解|AOP 优化代码(三)-注解

    摘要:上一篇动态代理反射注解优化代码二反射我们实现了通过反射完善找到目标类,然后通过动态代理提供默认实现,本篇我们将使用自定义注解来继续优化。下一篇动态代理反射注解四动态代理对象注入到容器 上一篇SpringBoot 动态代理|反射|注解|AOP 优化代码(二)-反射 我们实现了通过反射完善找到目标类,然后通过动态代理提供默认实现,本篇我们将使用自定义注解来继续优化。 创建注解 1.创建枚举...

    Charles 评论0 收藏0
  • SpringBoot 动态代理|反射|注解|AOP 优化代码(二)-反射

    摘要:动态代理反射注解优化代码一动态代理提供接口默认实现我们抛出问题,并且提出解决问题的第一步的方法。重写动态代理类,实现通过的查找出传入的所有泛型的实现下一篇动态代理反射注解优化代码三注解 SpringBoot 动态代理|反射|注解|AOP 优化代码(一)-动态代理提供接口默认实现 我们抛出问题,并且提出解决问题的第一步的方法。下面我们继续深入,动态代理和反射继续解决我们的问题。 改动代...

    spacewander 评论0 收藏0
  • spring 入门 2 自动装配和aop

    摘要:使用注解配置一步骤为主配置文件引入新的命名空间约束导入约束开启使用注解代理配置文件在中指定扫描包下所有类的注解扫描时会扫描指定包下的所有子孙包在类中使用注解完成配置等二将对象注册到容器将注册到容器中,相当于层层层三修改对象的作用范 使用注解配置spring 一、步骤 1.为主配置文件引入新的命名空间(约束) 导入spring-context-4.2.xsd schema约束 show...

    JasinYip 评论0 收藏0
  • Spring入门IOC和AOP学习笔记

    摘要:入门和学习笔记概述框架的核心有两个容器作为超级大工厂,负责管理创建所有的对象,这些对象被称为。中的一些术语切面切面组织多个,放在切面中定义。 Spring入门IOC和AOP学习笔记 概述 Spring框架的核心有两个: Spring容器作为超级大工厂,负责管理、创建所有的Java对象,这些Java对象被称为Bean。 Spring容器管理容器中Bean之间的依赖关系,使用一种叫做依赖...

    wenyiweb 评论0 收藏0
  • Spring自定义注解不生效原因解析及解决方法

    摘要:自定义注解不生效原因解析及解决方法背景项目中,自己基于实现了一套缓存注解。但是最近出现一种情况缓存竟然没有生效,大量请求被击穿到层,导致压力过大。至此,问题得到解决。 自定义注解不生效原因解析及解决方法 背景: 项目中,自己基于spring AOP实现了一套java缓存注解。但是最近出现一种情况:缓存竟然没有生效,大量请求被击穿到db层,导致db压力过大。现在我们看一下具体代码情形(代...

    xbynet 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<