1,加载Controller
我们需要创建 一个ControllerHelper类,让它来处理下面的逻辑:
通过ClassHelper我们可以获取所有定义了Controller注解的类,可以通过反射获取该类中所有带有Action注解的方法,获取Action注解中的请求表达式,进而获取请求方法与请求路径,封装一个请求对象(Request)和处理对象(Handler),最后将Request与Handler建立一个映射关系,存入一个Action Map中,并提供一个可以根据请求方法与请求路径获取处理对象的方法。
首先定义一个名为Request的类,代码如下:
- package org.smart4j.framework.bean;
- import org.apache.commons.lang3.builder.EqualsBuilder;
- import org.apache.commons.lang3.builder.HashCodeBuilder;
- /**
- * Created by jack on 2017/5/24.
- * 请求类,封装请求信息
- */
- 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 void setRequestMethod(String requestMethod) {
- this.requestMethod = requestMethod;
- }
- public String getRequestPath() {
- return requestPath;
- }
- public void setRequestPath(String requestPath) {
- this.requestPath = requestPath;
- }
- @Override
- public int hashCode() {
- return HashCodeBuilder.reflectionHashCode(this);
- }
- @Override
- public boolean equals(Object object) {
- return EqualsBuilder.reflectionEquals(this,object);
- }
- }
然后编写一个Handler的类,代码如下:
- package org.smart4j.framework.bean;
- import java.lang.reflect.Method;
- /**
- * Created by jack on 2017/5/24.
- * 封装Action信息
- */
- 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 void setControllerClass(Class<?> controllerClass) {
- this.controllerClass = controllerClass;
- }
- public Method getActionMethod() {
- return actionMethod;
- }
- public void setActionMethod(Method actionMethod) {
- this.actionMethod = actionMethod;
- }
- }
最后是ControllerHelper的代码:
- package org.smart4j.framework.helper;
- import org.smart4j.framework.annotation.Action;
- import org.smart4j.framework.bean.Handler;
- import org.smart4j.framework.bean.Request;
- import org.smart4j.framework.org.smart4j.framework.util.ArrayUtil;
- import org.smart4j.framework.org.smart4j.framework.util.CollectionUtil;
- import java.lang.reflect.Method;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Set;
- /**
- * Created by liangboqun on 2017/5/24.
- */
- public final class ControllerHelper {
- /**
- * 用于存放请求与处理器的映射关系(简称Aciotn Map)
- */
- private static final Map<Request,Handler> ACTION_MAP = new HashMap<Request, Handler>();
- static {
- /**
- * 获取所有的Controller类
- */
- Set<Class<?>> controllerClassSet = ClassHelper.getControllerClassSet();
- if (CollectionUtil.isNotEmpty(controllerClassSet)){
- //遍历这些Controller
- for (Class<?> controllerClass : controllerClassSet) {
- //获取Controller类中定义的方法
- Method [] methods = controllerClass.getDeclaredMethods();
- if (ArrayUtil.isNotEmpty(methods)){
- //遍历Controller类中的方法
- 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(controllerClass,method);
- //初始化Action Map
- ACTION_MAP.put(request,handler);
- }
- }
- }
- }
- }
- }
- }
- }
- /**
- * 获取Handler
- */
- public static Handler getHandler(String requestMethod,String requstPath){
- Request request = new Request(requestMethod,requstPath);
- return ACTION_MAP.get(request);
- }
- }
可见,我们在ControllerHelper中封装了一个ActionMap,通过它来存放Request与Handler之间的映射关系,然后通过ClassHelper来获取所有带有Controller注解的类,接着遍历这些Controller类,从Action注解中提取URL,最后初始化Request与Handler之间的映射关系
2,初始化框架
通过上面的过程,我们创建了ClassHelper,BeanHelper,IocHelper,ControllerHelper这四个Helper类,需要通过一个入口程序来加载它们,实际上式加载它们的静态代码库,这个加载程序时HelperLoader,代码如下:
- package org.smart4j.framework;
- import org.smart4j.framework.helper.BeanHelper;
- import org.smart4j.framework.helper.ClassHelper;
- import org.smart4j.framework.helper.ControllerHelper;
- import org.smart4j.framework.helper.IocHelper;
- import org.smart4j.framework.org.smart4j.framework.util.ClassUtil;
- /**
- * Created by jack on 2017/5/24.
- * 加载相应类的,帮助类
- */
- public final class HelperLoader {
- //初始化,加载类
- public static void init(){
- Class<?> [] classList = {ClassHelper.class, BeanHelper.class, IocHelper.class, ControllerHelper.class};
- for (Class<?> cls: classList) {
- ClassUtil.loadClass(cls.getName(),true);
- }
- }
- }
现在可以直接调用HelperLoader的init方法来加载这些Helper类了。实际上当我们第一次访问类时,就会加载static块,这里只是为了加载更加集中,所以写了这个加载类。