Skip to content

Commit

Permalink
[enhancement] Docean needs to support the ability to extend Ozhera (a…
Browse files Browse the repository at this point in the history
…dded a plugin, modified the logic of class searching)
  • Loading branch information
goodjava committed Sep 20, 2023
1 parent afa0279 commit 3764474
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
/**
* @Author [email protected]
* @Date 2022-07-12
* 让docean用起来像spring
* Make Docean feel like Spring.
* <p>
* 适配spring 的注解(Service Repository Component Autowired PreDestroy PostConstruct)
* Annotations compatible with Spring(Service Repository Component Autowired PreDestroy PostConstruct)
*/
@DOceanPlugin
@Slf4j
Expand Down Expand Up @@ -80,15 +80,15 @@ 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);
}

@Override
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);
}

Expand Down
67 changes: 67 additions & 0 deletions jcommon/docean-spring-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>run.mone</groupId>
<artifactId>jcommon</artifactId>
<version>1.4-SNAPSHOT</version>
</parent>

<artifactId>docean-spring-starter</artifactId>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springboot.version>2.7.15</springboot.version>
<spring.version>5.3.29</spring.version>
</properties>


<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
<scope>provided</scope>
</dependency>


<dependency>
<groupId>run.mone</groupId>
<artifactId>docean</artifactId>
<version>1.4-java20-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

</dependencies>


<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<compilerArgs>
<arg>--add-modules=jdk.incubator.concurrent</arg>
<arg>--enable-preview</arg>
</compilerArgs>
<compilerVersion>20</compilerVersion>
<source>20</source>
<target>20</target>
</configuration>
</plugin>


</plugins>


</build>



</project>
Original file line number Diff line number Diff line change
@@ -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 [email protected]
* @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<String, String> extensionMap = new HashMap<>();


@PostConstruct
public void initConfig() {
List<String> 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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package run.mone.docean.spring.extension;

import com.xiaomi.youpin.docean.Ioc;
import run.mone.docean.spring.config.DoceanAutoConfigure;

/**
* @author [email protected]
* @date 2023/9/19 14:20
*/
public class Extensions {

private Ioc ioc;

public Extensions(Ioc ioc) {
this.ioc = ioc;
}

public <T> T get(String name) {
String key = DoceanAutoConfigure.extensionMap.get(name);
return ioc.getBean(key);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=run.mone.docean.spring.config.DoceanAutoConfigure
10 changes: 8 additions & 2 deletions jcommon/docean/src/main/java/com/xiaomi/youpin/docean/Ioc.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public class Ioc {
/**
* It needs to be used when interacting with containers like spring
*/
private Function<String, Object> contextFunction = new Function<String, Object>() {
private Function<String, Object> contextFunction = new Function<>() {
@Override
public @Nullable Object apply(@Nullable String s) {
return new Object();
return null;
}
};

Expand Down Expand Up @@ -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 = Safe.callAndLog(()-> this.contextFunction.apply(name),null);
Optional.ofNullable(obj).ifPresent(o -> ReflectUtils.setField(bean.getObj(), field, o));
}
}

private void callInit(Bean it) {
Expand Down
71 changes: 36 additions & 35 deletions jcommon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<module>excel</module>
<module>match</module>
<module>infra-common</module>
<module>docean-spring-starter</module>
</modules>


Expand Down Expand Up @@ -208,48 +209,48 @@


<!-- To package the source code, you need to add this plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-source-plugin</artifactId>-->
<!-- <version>2.1</version>-->
<!-- <configuration>-->
<!-- <attach>true</attach>-->
<!-- </configuration>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <phase>compile</phase>-->
<!-- <goals>-->
<!-- <goal>jar</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
</plugins>
</build>

<distributionManagement>
<repository>
<id>central</id>
<name>maven-release-virtual</name>
<url>https://pkgs.d.xiaomi.net/artifactory/maven-release-virtual</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>maven-snapshot-virtual</name>
<url>https://pkgs.d.xiaomi.net/artifactory/maven-snapshot-virtual</url>
</snapshotRepository>
</distributionManagement>

<!-- <distributionManagement>-->
<!-- <snapshotRepository>-->
<!-- <id>ossrh</id>-->
<!-- <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>-->
<!-- </snapshotRepository>-->
<!-- <repository>-->
<!-- <id>ossrh</id>-->
<!-- <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>-->
<!-- <id>central</id>-->
<!-- <name>maven-release-virtual</name>-->
<!-- <url>https://pkgs.d.xiaomi.net/artifactory/maven-release-virtual</url>-->
<!-- </repository>-->
<!-- <snapshotRepository>-->
<!-- <id>snapshots</id>-->
<!-- <name>maven-snapshot-virtual</name>-->
<!-- <url>https://pkgs.d.xiaomi.net/artifactory/maven-snapshot-virtual</url>-->
<!-- </snapshotRepository>-->
<!-- </distributionManagement>-->

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>


</project>

0 comments on commit 3764474

Please sign in to comment.