mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-09 04:59:21 +08:00
refactor(extension/crud): BaseController => AbstractBaseController,并移除 CrudApiStrategy
This commit is contained in:
@@ -23,9 +23,7 @@ import org.springframework.core.BridgeMethodResolver;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import top.continew.starter.extension.crud.annotation.CrudApi;
|
||||
import top.continew.starter.extension.crud.controller.BaseController;
|
||||
import top.continew.starter.extension.crud.handler.CrudApiHandler;
|
||||
import top.continew.starter.extension.crud.handler.CrudApiStrategy;
|
||||
import top.continew.starter.extension.crud.controller.AbstractBaseController;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Objects;
|
||||
@@ -47,14 +45,9 @@ public class CrudApiAnnotationInterceptor implements MethodInterceptor {
|
||||
Method targetMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
|
||||
// 获取 @CrudApi 注解
|
||||
CrudApi crudApi = AnnotatedElementUtils.findMergedAnnotation(targetMethod, CrudApi.class);
|
||||
// 获取处理器
|
||||
CrudApiHandler<?> crudApiHandler = CrudApiStrategy.INSTANCE.handlerMap.get(targetClass);
|
||||
if (crudApiHandler != null) {
|
||||
crudApiHandler.preHandle(crudApi, invocation.getArguments(), targetMethod, targetClass);
|
||||
} else {
|
||||
CrudApiStrategy.INSTANCE.handlerMap.get(BaseController.class)
|
||||
.preHandle(crudApi, invocation.getArguments(), targetMethod, targetClass);
|
||||
}
|
||||
// 执行处理
|
||||
AbstractBaseController controller = (AbstractBaseController)invocation.getThis();
|
||||
controller.preHandle(crudApi, invocation.getArguments(), targetMethod, targetClass);
|
||||
return invocation.proceed();
|
||||
}
|
||||
}
|
||||
|
@@ -19,7 +19,6 @@ package top.continew.starter.extension.crud.autoconfigure;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
@@ -27,10 +26,6 @@ import org.springframework.context.annotation.Bean;
|
||||
import top.continew.starter.extension.crud.annotation.CrudApi;
|
||||
import top.continew.starter.extension.crud.aop.CrudApiAnnotationAdvisor;
|
||||
import top.continew.starter.extension.crud.aop.CrudApiAnnotationInterceptor;
|
||||
import top.continew.starter.extension.crud.handler.CrudApiHandler;
|
||||
import top.continew.starter.extension.crud.handler.CrudApiStrategy;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* CRUD REST Controller 自动配置
|
||||
@@ -44,18 +39,6 @@ public class CrudRestControllerAutoConfiguration {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(CrudRestControllerAutoConfiguration.class);
|
||||
|
||||
/**
|
||||
* 注入自定义处理器
|
||||
*
|
||||
* @param handlerList 自定义处理器集合
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setCrudApiHandler(List<CrudApiHandler<?>> handlerList) {
|
||||
for (CrudApiHandler<?> handler : handlerList) {
|
||||
CrudApiStrategy.INSTANCE.registerHandler(handler);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* CRUD API 注解通知
|
||||
*/
|
||||
|
@@ -27,6 +27,7 @@ import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import top.continew.starter.extension.crud.annotation.CrudApi;
|
||||
import top.continew.starter.extension.crud.enums.Api;
|
||||
import top.continew.starter.extension.crud.handler.CrudApiHandler;
|
||||
import top.continew.starter.extension.crud.model.query.PageQuery;
|
||||
import top.continew.starter.extension.crud.model.query.SortQuery;
|
||||
import top.continew.starter.extension.crud.model.req.BaseReq;
|
||||
@@ -38,7 +39,7 @@ import top.continew.starter.extension.crud.validation.CrudValidationGroup;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 控制器基类
|
||||
* 控制器抽象基类
|
||||
*
|
||||
* @param <S> 业务接口
|
||||
* @param <L> 列表类型
|
||||
@@ -48,7 +49,7 @@ import java.util.List;
|
||||
* @author Charles7c
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public abstract class BaseController<S extends BaseService<L, D, Q, C>, L, D, Q, C extends BaseReq> {
|
||||
public abstract class AbstractBaseController<S extends BaseService<L, D, Q, C>, L, D, Q, C extends BaseReq> implements CrudApiHandler {
|
||||
|
||||
@Autowired
|
||||
protected S baseService;
|
@@ -17,7 +17,6 @@
|
||||
package top.continew.starter.extension.crud.handler;
|
||||
|
||||
import top.continew.starter.extension.crud.annotation.CrudApi;
|
||||
import top.continew.starter.extension.crud.controller.BaseController;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@@ -27,14 +26,7 @@ import java.lang.reflect.Method;
|
||||
* @author Charles7c
|
||||
* @since 2.7.5
|
||||
*/
|
||||
public interface CrudApiHandler<T extends BaseController> {
|
||||
|
||||
/**
|
||||
* 获取处理器控制器类
|
||||
*
|
||||
* @return 处理器控制器类
|
||||
*/
|
||||
Class<T> getHandlerControllerClass();
|
||||
public interface CrudApiHandler {
|
||||
|
||||
/**
|
||||
* 前置处理
|
||||
|
@@ -1,70 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.extension.crud.handler;
|
||||
|
||||
import top.continew.starter.extension.crud.controller.BaseController;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* CRUD API 策略
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2.7.5
|
||||
*/
|
||||
public final class CrudApiStrategy {
|
||||
|
||||
/**
|
||||
* 全局单例引用
|
||||
*/
|
||||
public static final CrudApiStrategy INSTANCE = new CrudApiStrategy();
|
||||
|
||||
/**
|
||||
* 处理器集合
|
||||
*/
|
||||
public Map<Class<?>, CrudApiHandler<?>> handlerMap = new LinkedHashMap<>();
|
||||
|
||||
private CrudApiStrategy() {
|
||||
registerDefaultHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册所有默认的处理器
|
||||
*/
|
||||
public void registerDefaultHandler() {
|
||||
handlerMap.put(BaseController.class, new DefaultCrudApiHandler());
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册一个处理器
|
||||
*
|
||||
* @param handler 处理器
|
||||
*/
|
||||
public void registerHandler(CrudApiHandler<?> handler) {
|
||||
handlerMap.put(handler.getHandlerControllerClass(), handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除一个处理器
|
||||
*
|
||||
* @param controllerClass 控制器类
|
||||
*/
|
||||
public void removeHandler(Class<?> controllerClass) {
|
||||
handlerMap.remove(controllerClass);
|
||||
}
|
||||
}
|
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.starter.extension.crud.handler;
|
||||
|
||||
import top.continew.starter.extension.crud.annotation.CrudApi;
|
||||
import top.continew.starter.extension.crud.controller.BaseController;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* CRUD API 处理器(默认)
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2.7.5
|
||||
*/
|
||||
public class DefaultCrudApiHandler implements CrudApiHandler<BaseController> {
|
||||
|
||||
@Override
|
||||
public Class getHandlerControllerClass() {
|
||||
return BaseController.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preHandle(CrudApi crudApi, Object[] args, Method targetMethod, Class<?> targetClass) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user