beanDefintirions首先检查此方法是否使用原始Beans (Configureablelistablebeanfactory beanFactory;)。
ConfigurableListableBeanFactory beanFactory.
然后,对从BeanFactory,中得到的原始bean的所有方法进行迭代。在搜索了某个注释的方法之后,我们从Applicationcontext.中得到了Bean
这个Bean是在->阶段形成的原始Bean上的代理包装器。现在,通过这个bean,我调用了一个已经标记了我需要的注释的方法,但是它需要另一个参数Obj ..args。
我怎么才能得到那个缺失的论点?
Srping 5.x,java 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();
}
}发布于 2021-12-27 15:11:46
解决方案
三相构造函数(实现)。
答案是在方法的文档中:
)
@param用于方法调用的参数
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();
}
}
}https://stackoverflow.com/questions/70495689
复制相似问题