Skip to content

Commit

Permalink
fixes #719
Browse files Browse the repository at this point in the history
  • Loading branch information
caochengxiang committed Sep 20, 2023
2 parents cbbb6af + 68ac55a commit f9f9903
Show file tree
Hide file tree
Showing 75 changed files with 173 additions and 2,686 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
/**
* @Author goodjava@qq.com
* @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 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<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 goodjava@qq.com
* @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
2 changes: 1 addition & 1 deletion jcommon/http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
<scope>provided</scope>
</dependency>
</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public static String addServer(String config, String name, List<String> serversT
});

List<String> checkList = StringUtils.isNotEmpty(checkUrl)
? Lists.newArrayList("check interval=3000 rise=2 fall=3 timeout=1000 type=http", String.format("check_http_send \"HEAD %s HTTP/1.0\\r\\n\\r\\n\"", checkUrl))
: Lists.newArrayList("check interval=3000 rise=2 fall=3 timeout=1000 type=tcp");
? Lists.newArrayList("check interval=3000 rise=2 fall=3 timeout=1000 default_down=false type=http", String.format("check_http_send \"HEAD %s HTTP/1.0\\r\\n\\r\\n\"", checkUrl))
: Lists.newArrayList("check interval=3000 rise=2 fall=3 timeout=1000 default_down=false type=tcp");
checkList.stream().forEach(p -> {
NgxParam param = new NgxParam();
param.addValue(p);
Expand Down
2 changes: 1 addition & 1 deletion jcommon/rcurve/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@
<scope>provided</scope>
</dependency>
</dependencies>
</project>
</project>

This file was deleted.

This file was deleted.

Loading

0 comments on commit f9f9903

Please sign in to comment.