Feat:动态代码编译引擎成熟!

This commit is contained in:
wangyu 2021-01-05 00:08:35 +08:00
parent 8798d4784d
commit 2f2ae4b6ef
2 changed files with 11 additions and 3 deletions

View File

@ -0,0 +1,6 @@
package com.flyfish.framework.beans.enums;
public enum RestBeanCandidate {
REPOSITORY, SERVICE, CONTROLLER
}

View File

@ -8,6 +8,7 @@ import com.flyfish.framework.compiler.DynamicJavaCompiler;
import com.flyfish.framework.compiler.support.JavaSource;
import com.flyfish.framework.compiler.template.TemplateCompiler;
import com.flyfish.framework.utils.Assert;
import com.flyfish.framework.utils.StringFormats;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
@ -41,7 +42,10 @@ public class RestBeanAutoConfigure implements ImportBeanDefinitionRegistrar, Res
// 预编译模板
private final Map<String, Function<JavaSource, String>> templates = Stream.of("Repository", "Service", "Controller")
.collect(Collectors.toMap(key -> key, filename -> TemplateCompiler.compile("/templates/" + filename + ".tpl")));
// 存放父类值的映射
private final Map<String, Function<RestBean, Class<?>>> superClasses;
// 编译器引用
private final DynamicJavaCompiler compiler = DynamicJavaCompiler.delegate();
// 资源加载器
private ResourceLoader resourceLoader;
@ -68,8 +72,6 @@ public class RestBeanAutoConfigure implements ImportBeanDefinitionRegistrar, Res
Set<String> basePackages = getBasePackages(metadata);
// 扫描包路径检测RestBean注解的bean
Reflections reflections = new Reflections(basePackages);
// 编译器
DynamicJavaCompiler compiler = DynamicJavaCompiler.delegate();
// repo注册器
CustomRepositoryRegistrar repositoryRegistrar = new CustomRepositoryRegistrar(metadata, resourceLoader,
environment, registry, beanNameGenerator);
@ -84,7 +86,7 @@ public class RestBeanAutoConfigure implements ImportBeanDefinitionRegistrar, Res
.setBeanClass(clazz.getCanonicalName())
.setQueryBeanClass(restBean.queryClass().getCanonicalName())
.setPackageName(basePackages.stream().findFirst().orElse("com.flyfish.project"))
.setUri(restBean.value());
.setUri(StringUtils.isBlank(restBean.value()) ? StringFormats.camel2Line(clazz.getSimpleName()) : restBean.value());
// 分别生成实现类从repo到controller
templates.forEach((key, template) -> {
source.setSuperClass(superClasses.get(key).apply(restBean).getCanonicalName());