From efdb99f8cecc4afb592afad79c761081d5d5cf22 Mon Sep 17 00:00:00 2001 From: lee <4766465@qq.com> Date: Wed, 18 Dec 2024 13:27:00 +0800 Subject: [PATCH] init --- xxl-job/xxl-job-core/src/main/java/com/xxl/job/core/glue/impl/SpringGlueFactory.java | 80 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 80 insertions(+), 0 deletions(-) diff --git a/xxl-job/xxl-job-core/src/main/java/com/xxl/job/core/glue/impl/SpringGlueFactory.java b/xxl-job/xxl-job-core/src/main/java/com/xxl/job/core/glue/impl/SpringGlueFactory.java new file mode 100644 index 0000000..37e44d5 --- /dev/null +++ b/xxl-job/xxl-job-core/src/main/java/com/xxl/job/core/glue/impl/SpringGlueFactory.java @@ -0,0 +1,80 @@ +package com.xxl.job.core.glue.impl; + +import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; +import com.xxl.job.core.glue.GlueFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.core.annotation.AnnotationUtils; + +import javax.annotation.Resource; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +/** + * @author xuxueli 2018-11-01 + */ +public class SpringGlueFactory extends GlueFactory { + private static Logger logger = LoggerFactory.getLogger(SpringGlueFactory.class); + + + /** + * inject action of spring + * @param instance + */ + @Override + public void injectService(Object instance){ + if (instance==null) { + return; + } + + if (XxlJobSpringExecutor.getApplicationContext() == null) { + return; + } + + Field[] fields = instance.getClass().getDeclaredFields(); + for (Field field : fields) { + if (Modifier.isStatic(field.getModifiers())) { + continue; + } + + Object fieldBean = null; + // with bean-id, bean could be found by both @Resource and @Autowired, or bean could only be found by @Autowired + + if (AnnotationUtils.getAnnotation(field, Resource.class) != null) { + try { + Resource resource = AnnotationUtils.getAnnotation(field, Resource.class); + if (resource.name()!=null && resource.name().length()>0){ + fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(resource.name()); + } else { + fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(field.getName()); + } + } catch (Exception e) { + } + if (fieldBean==null ) { + fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(field.getType()); + } + } else if (AnnotationUtils.getAnnotation(field, Autowired.class) != null) { + Qualifier qualifier = AnnotationUtils.getAnnotation(field, Qualifier.class); + if (qualifier!=null && qualifier.value()!=null && qualifier.value().length()>0) { + fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(qualifier.value()); + } else { + fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(field.getType()); + } + } + + if (fieldBean!=null) { + field.setAccessible(true); + try { + field.set(instance, fieldBean); + } catch (IllegalArgumentException e) { + logger.error(e.getMessage(), e); + } catch (IllegalAccessException e) { + logger.error(e.getMessage(), e); + } + } + } + } + +} -- Gitblit v1.9.3