ListableBeanFactory#findAnnotationOnBean SpringBoot 低版本适配. (#64)

pull/84/head
chen.ma 3 years ago
parent 6ccfd8fef7
commit eee7628986

@ -9,6 +9,7 @@ import cn.hippo4j.common.web.base.Result;
import cn.hippo4j.starter.common.CommonDynamicThreadPool;
import cn.hippo4j.starter.config.BootstrapProperties;
import cn.hippo4j.starter.remote.HttpAgent;
import cn.hippo4j.starter.toolkit.DynamicThreadPoolAnnotationUtil;
import cn.hippo4j.starter.toolkit.thread.QueueTypeEnum;
import cn.hippo4j.starter.toolkit.thread.RejectedTypeEnum;
import cn.hippo4j.starter.toolkit.thread.ThreadPoolBuilder;
@ -60,16 +61,29 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof DynamicThreadPoolExecutor) {
DynamicThreadPool dynamicThreadPool = ApplicationContextHolder.findAnnotationOnBean(beanName, DynamicThreadPool.class);
if (Objects.isNull(dynamicThreadPool)) {
return bean;
DynamicThreadPool dynamicThreadPool;
try {
dynamicThreadPool = ApplicationContextHolder.findAnnotationOnBean(beanName, DynamicThreadPool.class);
if (Objects.isNull(dynamicThreadPool)) {
// 适配低版本 SpringBoot
dynamicThreadPool = DynamicThreadPoolAnnotationUtil.findAnnotationOnBean(beanName, DynamicThreadPool.class);
if (Objects.isNull(dynamicThreadPool)) {
return bean;
}
}
} catch (Exception ex) {
log.error("Failed to create dynamic thread pool in annotation mode.", ex);
}
DynamicThreadPoolExecutor dynamicExecutor = (DynamicThreadPoolExecutor) bean;
DynamicThreadPoolWrapper wrap = new DynamicThreadPoolWrapper(dynamicExecutor.getThreadPoolId(), dynamicExecutor);
ThreadPoolExecutor remoteExecutor = fillPoolAndRegister(wrap);
subscribeConfig(wrap);
return remoteExecutor;
} else if (bean instanceof DynamicThreadPoolWrapper) {
}
if (bean instanceof DynamicThreadPoolWrapper) {
DynamicThreadPoolWrapper wrap = (DynamicThreadPoolWrapper) bean;
registerAndSubscribe(wrap);
}

@ -0,0 +1,47 @@
package cn.hippo4j.starter.toolkit;
import cn.hippo4j.common.config.ApplicationContextHolder;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.annotation.AnnotationUtils;
import java.lang.annotation.Annotation;
import java.util.Optional;
/**
* {@link DynamicThreadPoolAnnotationUtil} SpringBoot.
*
* <p>Spring version >= 5.2.0 , SpringBoot version 2.2.0.RELEASE
* {@link ListableBeanFactory#findAnnotationOnBean(java.lang.String, java.lang.Class)}
*
* <p>, , Spring .
* , Spring version < 5.3.14 , SpringBoot version < 2.6.2
*
* @author chen.ma
* @date 2022/1/5 21:15
*/
public class DynamicThreadPoolAnnotationUtil {
/**
* {@param beanName} {@param annotationType} .
*
* @param beanName
* @param annotationType
* @param <A>
* @return
*/
public static <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) {
AbstractApplicationContext context = (AbstractApplicationContext) ApplicationContextHolder.getInstance();
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
A annotation = Optional.ofNullable(beanFactory)
.map(each -> (RootBeanDefinition) beanFactory.getMergedBeanDefinition(beanName))
.map(definition -> definition.getResolvedFactoryMethod())
.map(factoryMethod -> AnnotationUtils.getAnnotation(factoryMethod, annotationType))
.orElse(null);
return annotation;
}
}
Loading…
Cancel
Save