【问题标题】:How to get method arguments using reflection如何使用反射获取方法参数
【发布时间】:2021-12-27 12:47:09
【问题描述】:

此方法首先由 beanDefintirions 检查原始 Bean(Configureablelistablebeanfactory beanFactory 被使用;)。

ConfigurableListableBeanFactory beanFactory;注入。

然后对从BeanFactory获取的原始bean的所有方法进行迭代。在搜索了某个注解的方法后,我们从Applicationcontext中得到了Bean

这个 Bean 是原始 Bean 之上的一个代理包装器,它是在 -> postProcessBeforeInitialization() 中形成的强>阶段。现在通过这个bean,我调用了一个标有我需要的注解的方法,但它需要另一个参数Obj ..args

如何获得缺少的参数?

Использую Srping 5.xjava 11

private void runMethodWithPostProxyThirdPhaseAnnotation(String beanName, ApplicationContext applicationContext) {

      BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);


      try {
          String originalBeanClassName = beanDefinition.getBeanClassName();

          if (originalBeanClassName != null) {
              Class<?> originalClass = Class.forName(originalBeanClassName);
              Method[] methods = originalClass.getMethods();

              for (Method method : methods) {
                  if (method.isAnnotationPresent(PostProxyThirdPhase.class)) {
                      

                      String originalMethodName = method.getName();
                      Class<?>[] parameterTypesFromOriginalMethod = method.getParameterTypes();

                      Object beanAfterProxy = applicationContext.getBean(beanName);

                      Method methodFromProxyBean = beanAfterProxy
                              .getClass()
                              .getMethod(originalMethodName, parameterTypesFromOriginalMethod);

                      methodFromProxyBean.invoke(beanAfterProxy, ?);
                  }
              }
          }

      } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
          e.printStackTrace();
      }

}

【问题讨论】:

    标签: spring reflection java-11


    【解决方案1】:

    解决方案


    三阶段构造函数(实现)。

    答案在该方法的文档中:

    • 公共对象调用(Object obj, Object... args)

    @param args 用于方法调用的参数

    
    import bean.post.process.annotation.PostProxyThirdPhase;
    import bean.post.process.bean.Quoter;
    import org.springframework.beans.factory.config.BeanDefinition;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationListener;
    import org.springframework.context.event.ContextRefreshedEvent;
    import org.springframework.stereotype.Component;
    
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.Arrays;
    
    @Component
    public class PostProxyInvokerContextListener implements ApplicationListener<ContextRefreshedEvent> {
    
        private final ConfigurableListableBeanFactory beanFactory;
    
        private final Quoter quoter;
    
        public PostProxyInvokerContextListener(ConfigurableListableBeanFactory beanFactory, Quoter quoter) {
            this.beanFactory = beanFactory;
            this.quoter = quoter;
        }
    
    
        @Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
            ApplicationContext applicationContext = event.getApplicationContext();
    
            String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
    
            Arrays.stream(beanDefinitionNames)
                    .forEach(beanName -> runMethodWithPostProxyThirdPhaseAnnotation(beanName, applicationContext));
        }
    
    
        private void runMethodWithPostProxyThirdPhaseAnnotation(String beanName, ApplicationContext applicationContext) {
    
            BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
    
            try {
                String originalBeanClassName = beanDefinition.getBeanClassName();
    
                if (originalBeanClassName != null) {
                    Class<?> originalClass = Class.forName(originalBeanClassName);
                    Method[] methods = originalClass.getMethods();
    
                    for (Method method : methods) {
                        if (method.isAnnotationPresent(PostProxyThirdPhase.class)) {
    
                            String originalMethodName = method.getName();
                            Class<?>[] parameterTypesFromOriginalMethod = method.getParameterTypes();
    
                            Object beanAfterProxy = applicationContext.getBean(beanName);
    
                            Method methodFromProxyBean = beanAfterProxy
                                    .getClass()
                                    .getMethod(originalMethodName, parameterTypesFromOriginalMethod);
    
                            Object[] args = new Object[]{quoter};
    
                            methodFromProxyBean.invoke(beanAfterProxy, args);
                        }
                    }
                }
    
            } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-03
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      • 1970-01-01
      • 2010-10-11
      相关资源
      最近更新 更多