Maven添加依赖:
<!-- 用于动态扫描 -->
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.12</version>
</dependency>
接口类:
package com.test.pageMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author HelSon
* @class BasePageMethod
* @description 业务处理接口
* @date 2021-05-10
**/
public interface BasePageMethod {
void init(HttpServletRequest request, HttpServletResponse response);
void run();
}
抽象类:
package com.test.pageMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author HelSon
* @class BasePageMethod
* @description 业务处理基类
* @date 2021-05-10
**/
public interface BasePageMethod {
void init(HttpServletRequest request, HttpServletResponse response);
void run();
}
实现类:(需要创建一个文件夹implement,用于给工厂类扫描)
package com.test.pageMethod.implement;
import com.test.pageMethod.ABasePageMethod;
import java.util.Map;
/**
* @author HelSon
* @class TestPageMethod
* @description 测试1
* @date 2021-05-10
**/
public class TestPageMethod extends ABasePageMethod {
// 设置跳转页面名称
@Override
public void setViewName() {
this.viewName = "test1";
}
// 执行的业务内容
@Override
public void run() {
System.out.println("执行"+ this.getViewName() +"的run方法");
System.out.println("页面名称:" + this.getViewName());
System.out.println("请求参数:");
// 请求
Map Param = request.getParameterMap();
Param.forEach((k, v)->{
System.out.println(" Key:"+ k +", Value:"+ v);
});
}
}
工厂类:
package com.test.pageMethod;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Set;
/**
* @author HelSon
* @class PageMethodFactory
* @description 业务处理工厂
* @date 2021-05-10
**/
public class PageMethodFactory {
PageMethodFactory() {}
static final Logger log = LoggerFactory.getLogger(PageMethodFactory.class);
// 存储业务处理
static Map<String, ABasePageMethod> pageMethodMaps = Maps.newHashMap();
static {
// 扫描该路径下的类
Reflections reflections = new Reflections("com.test.pageMethod.implement");
//获取继承了ABasePageMethod的所有类
Set<Class<? extends ABasePageMethod>> classSet = reflections.getSubTypesOf(ABasePageMethod.class);
for (Class<? extends ABasePageMethod> aClass : classSet) {
putMethod(aClass);
}
}
// 执行实现方法
public static void runMehthod(String methodType, HttpServletRequest request, HttpServletResponse response){
if(methodType == null) {
return;
}
// 拥有对应工厂及方法
if (pageMethodMaps.containsKey(methodType)) {
// 初始化并执行业务内容
pageMethodMaps.get(methodType).init(request, response);
pageMethodMaps.get(methodType).run();
}
}
// 添加页面处理逻辑
private static void putMethod(Class aBasePageMethod) {
try{
ABasePageMethod pageMethod = (ABasePageMethod) aBasePageMethod.newInstance();
if(StringUtils.isBlank(pageMethod.getViewName())){
log.error(pageMethod.getClass().getName() +"类没有设置name!");
}
pageMethodMaps.put(pageMethod.getViewName(), pageMethod);
} catch (Exception e){
log.error("业务处理添加异常!{}" ,e.getMessage(), e);
}
}
}
调用验证:
package com.test;
import com.test.pageMethod.PageMethodFactory;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 页面跳转
*/
@Controller
@RequestMapping("test")
public class TestPage{
@RequestMapping("/{method}")
public String go(@PathVariable String method, HttpServletRequest request, HttpServletResponse response){
PageMethodFactory.runMehthod(method, request, response);
return "/test/" + viewName;
}
}