1、该框架基于springweb开发;
2、该框架是自己实践经验的总结,适用于有固定流程的业务系统,包括:参数校验、参数初始化、业务校验、业务执行;
3、该框架未做并发处理测试;
4、该框架将不断完善;
5、该框架主要优点是代码和业务逻辑的分类,使代码可读性更高,例如想要添加一个业务接口,只需增加一个类T1001.java继承抽象类MainBusiness.java即可,所有的业务逻辑在T1001.java各自的方法中实现即可;
项目结构:
项目代码:
package com.kkdev.business.user;
import org.springframework.stereotype.Component;
import com.kkdev.business.MainBusiness;
import com.kkdev.domain.TransBean;
@Component
public class T1001 extends MainBusiness {
@Override
public void initParams(TransBean transBean) {
System.out.println("参数初始化--------------");
}
@Override
public void checkParams(TransBean transBean) {
System.out.println("参数校验--------------");
}
@Override
public void checkBusiness(TransBean transBean) {
System.out.println("业务检查--------------");
}
@Override
public void doBusiness(TransBean transBean) {
System.out.println("业务逻辑执行--------------");
}
}
package com.kkdev.business;
import com.kkdev.domain.TransBean;
/**
* 业务流程控制
*
* @author zhouy
*
*/
public interface BaseBusiness {
/**
* 初始化
*
* @param transBean
*/
void initParamsSupper(TransBean transBean);
/**
* 参数校验
*
* @param transBean
*/
void checkParamsSupper(TransBean transBean);
/**
* 业务校验
*
* @param transBean
*/
void checkBusinessSupper(TransBean transBean);
/**
* 业务逻辑
*
* @param transBean
*/
void doBusinessSupper(TransBean transBean);
}
package com.kkdev.business;
import com.kkdev.domain.TransBean;
public abstract class MainBusiness implements BaseBusiness {
public final void doService(TransBean transBean) {
this.initParamsSupper(transBean);
this.checkParamsSupper(transBean);
this.checkBusinessSupper(transBean);
this.doBusinessSupper(transBean);
}
public void initParamsSupper(TransBean transBean) {
System.out.println("【公共】参数初始化。。。");
initParams(transBean);
}
public void checkParamsSupper(TransBean transBean) {
System.out.println("【公共】参数校验。。。");
checkParams(transBean);
}
public void checkBusinessSupper(TransBean transBean) {
System.out.println("【公共】业务校验。。。");
checkBusiness(transBean);
}
public void doBusinessSupper(TransBean transBean) {
System.out.println("【公共】业务逻辑执行。。。");
doBusiness(transBean);
}
public abstract void initParams(TransBean transBean);
public abstract void checkParams(TransBean transBean);
public abstract void checkBusiness(TransBean transBean);
public abstract void doBusiness(TransBean transBean);
}
package com.kkdev.controller;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kkdev.domain.TransBean;
public class BaseController {
public void returnClient(HttpServletResponse response, TransBean transBean) {
Map responseMap = transBean.getResponseMap();
if (responseMap == null || responseMap.isEmpty()) {
responseMap.put("ret_code", "99");
responseMap.put("ret_msg", "系统异常!");
}
System.out.println("返回报文:" + responseMap);
String jsonStr = JSON.toJSONString(responseMap,
SerializerFeature.WriteMapNullValue);
OutputStream out = null;
try {
out = response.getOutputStream();
out.write(jsonStr.getBytes("utf-8"));
out.flush();
} catch (IOException e) {
e.printStackTrace();
System.out.println("系统异常!");
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("系统异常!");
}
}
}
}
}
package com.kkdev.controller;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.kkdev.business.MainBusiness;
import com.kkdev.domain.TransBean;
import com.kkdev.utils.BeanUtils;
import com.kkdev.utils.RequestUtils;
@Controller
public class MainController extends BaseController {
@RequestMapping(value = "/trans{trans_code}")
public void trans(HttpServletResponse response,
@PathVariable(value = "trans_code") String transCode) {
Map params = RequestUtils.getRequestParams();
System.out.println("请求报文:" + params);
TransBean transBean = new TransBean();
if (params == null || params.isEmpty()
|| StringUtils.isEmpty(transCode)) {
transBean.addResponseParam("ret_code", "00");
transBean.addResponseParam("ret_msg", "服务已启动!");
returnClient(response, transBean);
return;
}
transBean.setRequestMap(params);
transBean.setLocalMap(params);
transBean.addLocalParam("trans_code", transCode);
// 生成交易实例
MainBusiness mB = (MainBusiness) BeanUtils.getBean("t" + transCode);
if (mB == null) {
transBean.addResponseParam("ret_code", "99");
transBean.addResponseParam("ret_msg", "系统异常!");
returnClient(response, transBean);
return;
}
mB.doService(transBean);
transBean.addResponseParam("ret_code", "00");
transBean.addResponseParam("ret_msg", "交易成功");
returnClient(response, transBean);
return;
}
}
package com.kkdev.domain;
import java.util.HashMap;
import java.util.Map;
public class TransBean {
private Map requestMap;
private Map localMap;
private Map responseMap;
public String getRequestParam(String key) {
if (requestMap == null) {
return null;
}
return requestMap.get(key);
}
public String getLocalParam(String key) {
if (localMap == null) {
return null;
}
return localMap.get(key);
}
public String getResponseParam(String key) {
if (responseMap == null) {
return null;
}
return responseMap.get(key);
}
public void addRequestParam(String key, String value) {
if (requestMap == null) {
requestMap = new HashMap();
}
requestMap.put(key, value);
}
public void addResponseParam(String key, String value) {
if (responseMap == null) {
responseMap = new HashMap();
}
responseMap.put(key, value);
}
public void addLocalParam(String key, String value) {
if (localMap == null) {
localMap = new HashMap();
}
localMap.put(key, value);
}
public Map getResponseMap() {
return responseMap;
}
public void setResponseMap(Map responseMap) {
this.responseMap = responseMap;
}
public Map getRequestMap() {
return requestMap;
}
public void setRequestMap(Map requestMap) {
this.requestMap = requestMap;
}
public Map getLocalMap() {
return localMap;
}
public void setLocalMap(Map localMap) {
this.localMap = localMap;
}
}
package com.kkdev.utils;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
public class BeanUtils {
private static WebApplicationContext wac = null;
public static Object getBean(String beanId) {
if (wac == null) {
wac = ContextLoader.getCurrentWebApplicationContext();
}
return wac.getBean(beanId);
}
}
package com.kkdev.utils;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
public class RequestUtils {
public static Map getRequestParams() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
Map params = new HashMap();
Enumeration paramsNames = request.getParameterNames();
for (Enumeration e = paramsNames; e.hasMoreElements();) {
String name = e.nextElement().toString();
String value = request.getParameter(name);
params.put(name, value);
}
;
return params;
}
}