From a0eaa5a0f6cea86dfe719daca7896f1f0018299a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Tue, 31 Jan 2023 12:01:41 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E9=87=8D=E6=9E=84=20=E6=8A=BD?= =?UTF-8?q?=E5=8F=96=20ruoyi-common-job=20=E5=85=AC=E5=85=B1=E8=B0=83?= =?UTF-8?q?=E5=BA=A6=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-common/pom.xml | 1 + ruoyi-common/ruoyi-common-bom/pom.xml | 7 ++ ruoyi-common/ruoyi-common-job/pom.xml | 43 ++++++++++ .../common}/job/config/XxlJobConfig.java | 10 +-- .../config/properties/XxlJobProperties.java | 2 +- .../job/core/glue/impl/SpringGlueFactory.java | 80 +++++++++++++++++++ ...ot.autoconfigure.AutoConfiguration.imports | 1 + ruoyi-extend/ruoyi-xxl-job-admin/pom.xml | 11 ++- ruoyi-modules/ruoyi-job/pom.xml | 6 +- 9 files changed, 148 insertions(+), 13 deletions(-) create mode 100644 ruoyi-common/ruoyi-common-job/pom.xml rename {ruoyi-modules/ruoyi-job/src/main/java/com/ruoyi => ruoyi-common/ruoyi-common-job/src/main/java/com/ruoyi/common}/job/config/XxlJobConfig.java (86%) rename {ruoyi-modules/ruoyi-job/src/main/java/com/ruoyi => ruoyi-common/ruoyi-common-job/src/main/java/com/ruoyi/common}/job/config/properties/XxlJobProperties.java (93%) create mode 100644 ruoyi-common/ruoyi-common-job/src/main/java/com/xxl/job/core/glue/impl/SpringGlueFactory.java create mode 100644 ruoyi-common/ruoyi-common-job/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index 1bb05e20..6ea57a27 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -17,6 +17,7 @@ ruoyi-common-doc ruoyi-common-excel ruoyi-common-idempotent + ruoyi-common-job ruoyi-common-log ruoyi-common-mail ruoyi-common-mybatis diff --git a/ruoyi-common/ruoyi-common-bom/pom.xml b/ruoyi-common/ruoyi-common-bom/pom.xml index ef001692..49c859df 100644 --- a/ruoyi-common/ruoyi-common-bom/pom.xml +++ b/ruoyi-common/ruoyi-common-bom/pom.xml @@ -54,6 +54,13 @@ ${revision} + + + com.ruoyi + ruoyi-common-job + ${revision} + + com.ruoyi diff --git a/ruoyi-common/ruoyi-common-job/pom.xml b/ruoyi-common/ruoyi-common-job/pom.xml new file mode 100644 index 00000000..ea2b6d03 --- /dev/null +++ b/ruoyi-common/ruoyi-common-job/pom.xml @@ -0,0 +1,43 @@ + + + + com.ruoyi + ruoyi-common + ${revision} + ../pom.xml + + 4.0.0 + + ruoyi-common-job + + + ruoyi-common-job 定时任务 + + + + + + org.springframework.boot + spring-boot-autoconfigure + + + + + com.xuxueli + xxl-job-core + + + + org.projectlombok + lombok + + + + com.ruoyi + ruoyi-common-core + + + + diff --git a/ruoyi-modules/ruoyi-job/src/main/java/com/ruoyi/job/config/XxlJobConfig.java b/ruoyi-common/ruoyi-common-job/src/main/java/com/ruoyi/common/job/config/XxlJobConfig.java similarity index 86% rename from ruoyi-modules/ruoyi-job/src/main/java/com/ruoyi/job/config/XxlJobConfig.java rename to ruoyi-common/ruoyi-common-job/src/main/java/com/ruoyi/common/job/config/XxlJobConfig.java index 5caada8a..34191f12 100644 --- a/ruoyi-modules/ruoyi-job/src/main/java/com/ruoyi/job/config/XxlJobConfig.java +++ b/ruoyi-common/ruoyi-common-job/src/main/java/com/ruoyi/common/job/config/XxlJobConfig.java @@ -1,13 +1,12 @@ -package com.ruoyi.job.config; +package com.ruoyi.common.job.config; -import com.ruoyi.job.config.properties.XxlJobProperties; +import com.ruoyi.common.job.config.properties.XxlJobProperties; import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; -import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; /** * xxl-job config @@ -15,9 +14,8 @@ import org.springframework.context.annotation.Configuration; * @author Lion Li */ @Slf4j -@Configuration +@AutoConfiguration @EnableConfigurationProperties(XxlJobProperties.class) -@AllArgsConstructor @ConditionalOnProperty(prefix = "xxl.job", name = "enabled", havingValue = "true") public class XxlJobConfig { diff --git a/ruoyi-modules/ruoyi-job/src/main/java/com/ruoyi/job/config/properties/XxlJobProperties.java b/ruoyi-common/ruoyi-common-job/src/main/java/com/ruoyi/common/job/config/properties/XxlJobProperties.java similarity index 93% rename from ruoyi-modules/ruoyi-job/src/main/java/com/ruoyi/job/config/properties/XxlJobProperties.java rename to ruoyi-common/ruoyi-common-job/src/main/java/com/ruoyi/common/job/config/properties/XxlJobProperties.java index f2c755a1..fc89a2f3 100644 --- a/ruoyi-modules/ruoyi-job/src/main/java/com/ruoyi/job/config/properties/XxlJobProperties.java +++ b/ruoyi-common/ruoyi-common-job/src/main/java/com/ruoyi/common/job/config/properties/XxlJobProperties.java @@ -1,4 +1,4 @@ -package com.ruoyi.job.config.properties; +package com.ruoyi.common.job.config.properties; import lombok.Data; import lombok.NoArgsConstructor; diff --git a/ruoyi-common/ruoyi-common-job/src/main/java/com/xxl/job/core/glue/impl/SpringGlueFactory.java b/ruoyi-common/ruoyi-common-job/src/main/java/com/xxl/job/core/glue/impl/SpringGlueFactory.java new file mode 100644 index 00000000..00cd8228 --- /dev/null +++ b/ruoyi-common/ruoyi-common-job/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 jakarta.annotation.Resource; +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 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); + } + } + } + } + +} diff --git a/ruoyi-common/ruoyi-common-job/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/ruoyi-common/ruoyi-common-job/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..dfcf8a60 --- /dev/null +++ b/ruoyi-common/ruoyi-common-job/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.ruoyi.common.job.config.XxlJobConfig diff --git a/ruoyi-extend/ruoyi-xxl-job-admin/pom.xml b/ruoyi-extend/ruoyi-xxl-job-admin/pom.xml index afbdd416..ccf19065 100644 --- a/ruoyi-extend/ruoyi-xxl-job-admin/pom.xml +++ b/ruoyi-extend/ruoyi-xxl-job-admin/pom.xml @@ -57,6 +57,11 @@ spring-boot-starter-actuator + + org.springframework.boot + spring-boot-starter-aop + + org.mybatis.spring.boot @@ -73,10 +78,10 @@ spring-boot-admin-starter-client - + - com.xuxueli - xxl-job-core + com.ruoyi + ruoyi-common-job diff --git a/ruoyi-modules/ruoyi-job/pom.xml b/ruoyi-modules/ruoyi-job/pom.xml index e886a293..08ed7317 100644 --- a/ruoyi-modules/ruoyi-job/pom.xml +++ b/ruoyi-modules/ruoyi-job/pom.xml @@ -24,10 +24,10 @@ ruoyi-common-core - + - com.xuxueli - xxl-job-core + com.ruoyi + ruoyi-common-job