feat:清理代码,新版spring支持自动注入,无需手动注入

This commit is contained in:
wangyu 2021-12-08 16:38:44 +08:00
parent 87316bc592
commit 117ca1e3c4
7 changed files with 26 additions and 47 deletions

View File

@ -0,0 +1,7 @@
package com.flyfish.framework.domain.base;
/**
* 占位标记service
*/
public interface DomainService {
}

View File

@ -1,15 +1,16 @@
package com.flyfish.framework.beans.meta;
import com.flyfish.framework.controller.reactive.ReactiveBaseController;
import com.flyfish.framework.controller.SafeController;
import com.flyfish.framework.controller.reactive.ReactiveBaseController;
import com.flyfish.framework.domain.base.DomainService;
import com.flyfish.framework.domain.base.NameLikeQo;
import com.flyfish.framework.domain.base.Qo;
import com.flyfish.framework.domain.base.Vo;
import com.flyfish.framework.repository.DefaultReactiveRepository;
import com.flyfish.framework.service.BaseReactiveService;
import com.flyfish.framework.service.impl.BaseReactiveServiceImpl;
import org.springframework.core.annotation.AliasFor;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.repository.Repository;
import java.lang.annotation.*;
@ -79,14 +80,14 @@ public @interface RestBean {
*
* @return 结果
*/
Class<? extends DefaultReactiveRepository> repoClass() default DefaultReactiveRepository.class;
Class<? extends Repository> repoClass() default DefaultReactiveRepository.class;
/**
* 服务类动态生成服务
*
* @return 结果
*/
Class<? extends BaseReactiveService> serviceClass() default BaseReactiveServiceImpl.class;
Class<? extends DomainService> serviceClass() default BaseReactiveServiceImpl.class;
/**
* controller的类支持自定义

View File

@ -3,13 +3,11 @@ package com.flyfish.framework.beans.resolver;
import com.flyfish.framework.compiler.DynamicJavaCompiler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.reactive.result.method.AbstractHandlerMethodMapping;
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;
import java.lang.reflect.Method;
import java.util.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* 动态Bean生成解析器
@ -18,21 +16,9 @@ import java.util.*;
*/
@Slf4j
@Lazy(false)
public class DynamicRestBeanResolver implements InitializingBean, DisposableBean {
public class DynamicRestBeanResolver implements DisposableBean {
private static final List<String> controllers = new ArrayList<>();
private static final Map<String, Class<?>> classes = new HashMap<>();
private final Method handlerRegister;
private final RequestMappingHandlerMapping mapping;
public DynamicRestBeanResolver(RequestMappingHandlerMapping mapping) throws NoSuchMethodException {
this.mapping = mapping;
this.handlerRegister = AbstractHandlerMethodMapping.class.getDeclaredMethod("detectHandlerMethods", Object.class);
}
public static void addController(String controller) {
controllers.add(controller);
}
public static void register(String name, Class<?> clazz) {
classes.put(name, clazz);
@ -51,14 +37,4 @@ public class DynamicRestBeanResolver implements InitializingBean, DisposableBean
log.info("正在销毁rest自动配置器");
DynamicJavaCompiler.delegate().close();
}
@Override
public void afterPropertiesSet() throws Exception {
log.info("正在注册controller...");
for (String bean : controllers) {
log.info("注册controller{}", bean);
handlerRegister.setAccessible(true);
handlerRegister.invoke(mapping, bean);
}
}
}

View File

@ -4,11 +4,8 @@ import com.flyfish.framework.beans.resolver.DynamicRestBeanResolver;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.server.WebFilter;
import java.util.ArrayList;
/**
* 默认初始化的Bean配置
*
@ -39,9 +36,8 @@ public class BeanConfig {
@Bean
public DynamicRestBeanResolver dynamicRestBeanResolver(RequestMappingHandlerMapping requestMappingHandlerMapping)
throws NoSuchMethodException {
return new DynamicRestBeanResolver(requestMappingHandlerMapping);
public DynamicRestBeanResolver dynamicRestBeanResolver() {
return new DynamicRestBeanResolver();
}
}

View File

@ -104,11 +104,12 @@ public class RestBeanAutoConfigure implements ImportBeanDefinitionRegistrar, Res
.setUri(makePath(clazz.getSimpleName(), restBean.value()));
// 分别生成实现类从repo到controller
templates.forEach((type, template) -> {
Class<?> superClass = superClasses.getOrDefault(type, noOp).apply(restBean);
// 当且仅当存在vo时才编译view-controller
if (testNotSupport(clazz, type, hasVo)) {
if (testNotSupport(superClass, type, hasVo)) {
return;
}
source.setSuperClass(superClasses.getOrDefault(type, noOp).apply(restBean).getCanonicalName());
source.setSuperClass(superClass.getCanonicalName());
source.setClassName(clazz.getSimpleName() + type.getName());
try {
log.info("尝试注册{}", source.getClassName());
@ -151,10 +152,6 @@ public class RestBeanAutoConfigure implements ImportBeanDefinitionRegistrar, Res
String beanName = StringUtils.uncapitalize(clazz.getSimpleName());
log.info("注册bean{}", beanName);
registry.registerBeanDefinition(beanName, builder.getBeanDefinition());
// 如果是controller注册
if (clazz.getSimpleName().endsWith("Controller")) {
DynamicRestBeanResolver.addController(beanName);
}
}
/**
@ -199,7 +196,7 @@ public class RestBeanAutoConfigure implements ImportBeanDefinitionRegistrar, Res
/**
* 测试是否支持vo
*
* @param clazz
* @param clazz
* @param type 注入类型
* @param hasVo 是否有vo
* @return 结果

View File

@ -2,6 +2,7 @@ package com.flyfish.framework.service;
import com.flyfish.framework.bean.SyncVo;
import com.flyfish.framework.domain.base.Domain;
import com.flyfish.framework.domain.base.DomainService;
import com.flyfish.framework.domain.base.Qo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
@ -16,7 +17,7 @@ import java.util.List;
*
* @author wybab
*/
public interface BaseReactiveService<T extends Domain> {
public interface BaseReactiveService<T extends Domain> extends DomainService {
/**
* 查询

View File

@ -2,6 +2,7 @@ package com.flyfish.framework.service;
import com.flyfish.framework.bean.SyncVo;
import com.flyfish.framework.domain.base.Domain;
import com.flyfish.framework.domain.base.DomainService;
import com.flyfish.framework.domain.base.Qo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
@ -16,7 +17,7 @@ import java.util.Optional;
*
* @param <T> 实体泛型
*/
public interface BaseService<T extends Domain> {
public interface BaseService<T extends Domain> extends DomainService {
/**
* 查询