Skip to content

Commit

Permalink
实现Controller类控制器和初始化框架
Browse files Browse the repository at this point in the history
  • Loading branch information
刘义 committed Oct 28, 2018
1 parent 74d47a2 commit 1361948
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 2 deletions.
34 changes: 34 additions & 0 deletions framework/src/main/java/com/custom/framework/bean/Handler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.custom.framework.bean;

import java.lang.reflect.Method;

/**
* 封装Action信息
* @author liuyi
* @date 2018/10/27
*/
public class Handler {

/**
* Controller类
*/
private Class<?> controllerClass;

/**
* Action方法
*/
private Method actionMethod;

public Handler(Class<?> controllerClass, Method actionMethod) {
this.controllerClass = controllerClass;
this.actionMethod = actionMethod;
}

public Class<?> getControllerClass() {
return controllerClass;
}

public Method getActionMethod() {
return actionMethod;
}
}
45 changes: 45 additions & 0 deletions framework/src/main/java/com/custom/framework/bean/Request.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.custom.framework.bean;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

/**
* 封装请求信息
* @author liuyi
* @date 2018/10/27
*/
public class Request {

/**
* 请求方法
*/
private String requestMethod;

/**
* 请求路径
*/
private String requestPath;

public Request(String requestMethod, String requestPath) {
this.requestMethod = requestMethod;
this.requestPath = requestPath;
}

public String getRequestMethod() {
return requestMethod;
}

public String getRequestPath() {
return requestPath;
}

@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}

@Override
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @author liuyi
* @date 2018/10/25
*/
public class BeanHelper {
public final class BeanHelper {

/**
* 定义Bean映射(用于存放Bean类与Bean实例的映射关系)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.custom.framework.helper;

import com.custom.framework.annotation.Action;
import com.custom.framework.annotation.Controller;
import com.custom.framework.bean.Handler;
import com.custom.framework.bean.Request;
import com.custom.framework.util.ArrayUtil;
import com.custom.framework.util.CollectionUtil;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* 控制器助手类
* @author liuyi
* @date 2018/10/27
*/
public final class ControllerHelper {

/**
* 用于存放请求与处理器的映射关系(简称 Action Map)
*/
private static final Map<Request, Handler> ACTION_MAP = new HashMap<>();

static {
//获取所有Controller类
Set<Class<?>> controlllerClassSet = ClassHelper.getControllerClassSet();
if (CollectionUtil.isNotEmpty(controlllerClassSet)) {
//遍历这些Controller类
for (Class<?> controllerClass : controlllerClassSet) {
//获取Controller类中的方法
Method[] methods = controllerClass.getDeclaredMethods();
if (ArrayUtil.isNotEmpty(methods)) {
//遍历类中的方法
for (Method method : methods) {
//判断当前方法是否带有Action注解
if (method.isAnnotationPresent(Action.class)) {
//从Action注解中获取 URL 映射规则
Action action = method.getAnnotation(Action.class);
String mapping = action.value();
//验证 URL 映射规则
if (mapping.matches("\\w+:/\\w*")) {
String[] array = mapping.split(":");
if (ArrayUtil.isNotEmpty(array) && array.length == 2) {
//获取请求方法与请求路径
String requestMethod = array[0];
String requestPath = array[1];
Request request = new Request(requestMethod, requestPath);
Handler handler = new Handler(Controller.class, method);
//构建初始化Action Map
ACTION_MAP.put(request, handler);
}
}
}
}
}
}
}
}

/**
* 获取Handler
* @param requestMethod
* @param requestPath
* @return
*/
public static Handler getHandler(String requestMethod, String requestPath) {
Request request = new Request(requestMethod, requestPath);
return ACTION_MAP.get(request);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.custom.framework.helper;

import com.custom.framework.annotation.Controller;
import com.custom.framework.util.ClassUtil;

/**
*
* @author liuyi
* @date 2018/10/28
*/
public final class HelperLoader {

public static void init() {
Class<?>[] classList = {
ClassHelper.class,
BeanHelper.class,
IocHelper.class,
Controller.class
};
for (Class<?> cls : classList) {
ClassUtil.loadClass(cls.getName(),true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @author liuyi
* @date 2018/10/27
*/
public class IocHelper {
public final class IocHelper {

static {
//获取所有的Bean类与Bean实例之间的映射关系(简称BeanMap)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.custom.framework.util;

import java.util.Map;
import java.util.Set;

/**
* @author liuyi
* @date 2018/10/27
Expand All @@ -13,4 +15,11 @@ public static boolean isNotEmpty(Map<Class<?>, Object> map) {
}
return true;
}

public static boolean isNotEmpty(Set<Class<?>> set) {
if (set.isEmpty()) {
return false;
}
return true;
}
}

0 comments on commit 1361948

Please sign in to comment.