From e9f2b1979765d21f46e26748cc97f5f7ffa5117c Mon Sep 17 00:00:00 2001 From: zhangzhiyong Date: Tue, 19 Sep 2023 18:06:23 +0800 Subject: [PATCH] [enhancement] Docean needs to support the ability to extend Ozhera (added a plugin, modified the logic of class searching) --- .../docean/plugin/spring/SpringPlugin.java | 8 +-- jcommon/docean-spring-starter/pom.xml | 67 +++++++++++++++++++ .../spring/config/DoceanAutoConfigure.java | 61 +++++++++++++++++ .../docean/spring/extension/Extensions.java | 24 +++++++ .../main/resources/META-INF/spring.factories | 1 + .../java/com/xiaomi/youpin/docean/Ioc.java | 10 ++- jcommon/pom.xml | 1 + 7 files changed, 166 insertions(+), 6 deletions(-) create mode 100644 jcommon/docean-spring-starter/pom.xml create mode 100644 jcommon/docean-spring-starter/src/main/java/run/mone/docean/spring/config/DoceanAutoConfigure.java create mode 100644 jcommon/docean-spring-starter/src/main/java/run/mone/docean/spring/extension/Extensions.java create mode 100644 jcommon/docean-spring-starter/src/main/resources/META-INF/spring.factories diff --git a/jcommon/docean-plugin/docean-plugin-spring/src/main/java/run/mone/docean/plugin/spring/SpringPlugin.java b/jcommon/docean-plugin/docean-plugin-spring/src/main/java/run/mone/docean/plugin/spring/SpringPlugin.java index 7af384138..7602df807 100644 --- a/jcommon/docean-plugin/docean-plugin-spring/src/main/java/run/mone/docean/plugin/spring/SpringPlugin.java +++ b/jcommon/docean-plugin/docean-plugin-spring/src/main/java/run/mone/docean/plugin/spring/SpringPlugin.java @@ -40,9 +40,9 @@ /** * @Author goodjava@qq.com * @Date 2022-07-12 - * 让docean用起来像spring + * Make Docean feel like Spring. *

- * 适配spring 的注解(Service Repository Component Autowired PreDestroy PostConstruct) + * Annotations compatible with Spring(Service Repository Component Autowired PreDestroy PostConstruct) */ @DOceanPlugin @Slf4j @@ -80,7 +80,7 @@ public void putBean(String name, Bean bean) { public String getInitMethodName(Object obj, Class clazz) { return Arrays.stream(clazz.getMethods()) .map(it -> Arrays.stream(it.getAnnotations()).filter(anno -> anno instanceof PostConstruct) - .findAny().map(it2->it.getName()).orElse(Cons.INIT)) + .findAny().map(it2 -> it.getName()).orElse(Cons.INIT)) .filter(name -> !Cons.INIT.equals(name)).findAny().orElse(Cons.INIT); } @@ -88,7 +88,7 @@ public String getInitMethodName(Object obj, Class clazz) { public String getDestoryMethodName(Object obj, Class clazz) { return Arrays.stream(clazz.getMethods()) .map(it -> Arrays.stream(it.getAnnotations()).filter(anno -> anno instanceof PreDestroy) - .findAny().map(it2->it.getName()).orElse(Cons.DESTORY)) + .findAny().map(it2 -> it.getName()).orElse(Cons.DESTORY)) .filter(name -> !Cons.DESTORY.equals(name)).findAny().orElse(Cons.DESTORY); } diff --git a/jcommon/docean-spring-starter/pom.xml b/jcommon/docean-spring-starter/pom.xml new file mode 100644 index 000000000..2cc80448f --- /dev/null +++ b/jcommon/docean-spring-starter/pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + + run.mone + jcommon + 1.4-SNAPSHOT + + + docean-spring-starter + + + UTF-8 + 2.7.15 + 5.3.29 + + + + + + + org.springframework.boot + spring-boot-starter-web + ${springboot.version} + provided + + + + + run.mone + docean + 1.4-java20-SNAPSHOT + provided + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.11.0 + + + --add-modules=jdk.incubator.concurrent + --enable-preview + + 20 + 20 + 20 + + + + + + + + + + + + \ No newline at end of file diff --git a/jcommon/docean-spring-starter/src/main/java/run/mone/docean/spring/config/DoceanAutoConfigure.java b/jcommon/docean-spring-starter/src/main/java/run/mone/docean/spring/config/DoceanAutoConfigure.java new file mode 100644 index 000000000..7807697b3 --- /dev/null +++ b/jcommon/docean-spring-starter/src/main/java/run/mone/docean/spring/config/DoceanAutoConfigure.java @@ -0,0 +1,61 @@ +package run.mone.docean.spring.config; + +import com.google.common.base.Splitter; +import com.xiaomi.youpin.docean.Ioc; +import com.xiaomi.youpin.docean.common.Safe; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import run.mone.docean.spring.extension.Extensions; + +import javax.annotation.PostConstruct; +import javax.annotation.Resource; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author goodjava@qq.com + * @date 2023/9/19 13:46 + */ +@Configuration +@Slf4j +public class DoceanAutoConfigure { + + private Ioc ioc; + + @Resource + private ApplicationContext ac; + + @Value("${extensions:}") + private String extensionsConfig; + + public static Map extensionMap = new HashMap<>(); + + + @PostConstruct + public void initConfig() { + List list = Splitter.on(":").splitToList(extensionsConfig); + if (list.size() == 3) { + extensionMap.put(list.get(0), list.get(1)); + ioc = Ioc.ins().name("extension").setContextFunction(name -> { + if (ac.containsBean(name)) { + return ac.getBean(name); + } + return Safe.callAndLog(() -> ac.getBean(Class.forName(name)), null); + }).init(list.get(2), "run.mone.docean.plugin.spring"); + } + } + + + @Bean + @ConditionalOnMissingBean + public Extensions extensions() { + Extensions extensions = new Extensions(ioc); + return extensions; + } + +} diff --git a/jcommon/docean-spring-starter/src/main/java/run/mone/docean/spring/extension/Extensions.java b/jcommon/docean-spring-starter/src/main/java/run/mone/docean/spring/extension/Extensions.java new file mode 100644 index 000000000..3c27967b3 --- /dev/null +++ b/jcommon/docean-spring-starter/src/main/java/run/mone/docean/spring/extension/Extensions.java @@ -0,0 +1,24 @@ +package run.mone.docean.spring.extension; + +import com.xiaomi.youpin.docean.Ioc; +import run.mone.docean.spring.config.DoceanAutoConfigure; + +/** + * @author goodjava@qq.com + * @date 2023/9/19 14:20 + */ +public class Extensions { + + private Ioc ioc; + + public Extensions(Ioc ioc) { + this.ioc = ioc; + } + + public T get(String name) { + String key = DoceanAutoConfigure.extensionMap.get(name); + return ioc.getBean(key); + } + + +} diff --git a/jcommon/docean-spring-starter/src/main/resources/META-INF/spring.factories b/jcommon/docean-spring-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..abf32f032 --- /dev/null +++ b/jcommon/docean-spring-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=run.mone.docean.spring.config.DoceanAutoConfigure \ No newline at end of file diff --git a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Ioc.java b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Ioc.java index 4f9640cff..0f402b242 100644 --- a/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Ioc.java +++ b/jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Ioc.java @@ -72,10 +72,10 @@ public class Ioc { /** * It needs to be used when interacting with containers like spring */ - private Function contextFunction = new Function() { + private Function contextFunction = new Function<>() { @Override public @Nullable Object apply(@Nullable String s) { - return new Object(); + return null; } }; @@ -312,6 +312,12 @@ private void initIoc0(String name, Bean bean, Field field) { o.getDependenceFieldMap().put(bean.getName(), field); ReflectUtils.setField(bean.getObj(), field, o.getObj()); }); + + //If there is a parent container, try to retrieve it from the parent container (such as Spring). + if (!Optional.ofNullable(b).isPresent()) { + Object obj = this.contextFunction.apply(name); + Optional.ofNullable(obj).ifPresent(o -> ReflectUtils.setField(bean.getObj(), field, o)); + } } private void callInit(Bean it) { diff --git a/jcommon/pom.xml b/jcommon/pom.xml index 93f6e2496..18567ee23 100644 --- a/jcommon/pom.xml +++ b/jcommon/pom.xml @@ -97,6 +97,7 @@ excel match infra-common + docean-spring-starter