-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
刘义
committed
Oct 28, 2018
1 parent
74d47a2
commit 1361948
Showing
7 changed files
with
188 additions
and
2 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
framework/src/main/java/com/custom/framework/bean/Handler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
45
framework/src/main/java/com/custom/framework/bean/Request.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
framework/src/main/java/com/custom/framework/helper/ControllerHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
framework/src/main/java/com/custom/framework/helper/HelperLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters