Atitit mvc框架的实现 mvc的原理demo v2 sbb.docx
目录
1.1. 项目启动的时候启动mvc框架扫描,建立url 方法对应表 1
1.2. 执行url的时候,执行webfilter,获取的对应method 1
/springbootUpload/src/mvcpkg/MvcCtrol.java
public class Application {
public static void main(String[] args) {
//boot mvc framwork
MvcUtil.get_url_method_mapper(MvcCtrol.class);
Method meth =MvcUtil. url_method_maps.get(uri);
Class<? > class1 = meth.getDeclaringClass();
Object classObj = ConstructorUtils.invokeConstructor(class1, null);
Object[] args =MvcUtil.getArgs(meth,httpServletRequest,httpServletResponse); //new Object[] {};
String r = (String) meth.invoke(classObj, args);
@MyComponent
public class MvcCtrol {
// http://localhost:8080/mvctest
@MyMapping("/mvctest")
public String mvctestMeth(HttpServletRequest req) {
return "data";
}
}
package mvcpkg;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.reflect.ConstructorUtils;
import org.springframework.stereotype.Component;
import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
@Component
@WebFilter(urlPatterns = "/*", filterName = "mvcFilter")
public class MvcFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) {
try {
// get url out mapper
// Map<String, Method> url_method_maps = MvcUtil.get_url_out_mapper();
HttpServletRequest httpServletRequest = (HttpServletRequest) arg0;
HttpServletResponse httpServletResponse = (HttpServletResponse) arg1;
String uri = httpServletRequest.getRequestURI();
System.out.println(uri);
if(MvcUtil.url_method_maps.get(uri)==null)
{
arg2.doFilter(arg0, arg1);
return;
}
Method meth =MvcUtil. url_method_maps.get(uri);
Class<? > class1 = meth.getDeclaringClass();
Object classObj = ConstructorUtils.invokeConstructor(class1, null);
Object[] args =MvcUtil.getArgs(meth,httpServletRequest,httpServletResponse); //new Object[] {};
String r = (String) meth.invoke(classObj, args);
outputHtml(httpServletResponse, r);
return; //not to next ,beirs springmvc show err,cause cant this mappering
// arg2.doFilter(arg0, arg1);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// private List getMycomponentClassList() {
// List li_allclass=Lists.newArrayList();
// li_allclass.add(MvcCtrol.class);
//
// List li= Lists.newArrayList();
// classList.add();
//
// }
private void outputHtml(HttpServletResponse httpServletResponse, String txt) throws IOException {
byte[] s = txt.getBytes();
ServletOutputStream outputStream = httpServletResponse.getOutputStream();
outputStream.write(s);
outputStream.flush();
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
package mvcpkg;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
public class MvcUtil {
public static Map<String, Method> url_method_maps = Maps.newLinkedHashMap();
public static Map<String, Method> get_url_method_mapper(Class class1) {
// MyComponent MyComponentAnno = (MyComponent) class1.getAnnotation(MyComponent.class);
// if (MyComponentAnno == null)
// return;
// 获取一个类的注解,根据注解名
Method[] methds = class1.getMethods();
for (Method method : methds) {
MyMapping anno1 = (MyMapping) method.getAnnotation(MyMapping.class);
if (anno1 == null)
continue;
String uri_mapping = anno1.value();
url_method_maps.put(uri_mapping, method);
}
return url_method_maps;
}
public static Object[] getArgs(Method meth, ServletRequest arg0, ServletResponse arg1) {
List li=Lists.newArrayList();
Parameter[] Parameters_arr= meth.getParameters();
for (Parameter parameter : Parameters_arr) {
Class<?> type1 = parameter.getType();
if ( type1 ==HttpServletRequest.class)
{
li.add(arg0);
}
}
return li.toArray();
}
private static void ClassListForeach(Consumer<Class<?>> consumer) {
List<Class<?>> li_allclass = Lists.newArrayList();
li_allclass.add(MvcCtrol.class);
for (Class class1 : li_allclass) {
consumer.accept(class1);
}
}
// public static Map<String, MethodObj> get_url_out_mapper() {
// Map<String, MethodObj> url_method_maps = Maps.newLinkedHashMap();
// ClassListForeach(new Consumer<Class<?>>() {
//
// @Override
// public void accept(Class<?> cls) {
//
// MyComponent MyComponentAnno = (MyComponent) cls.getAnnotation(MyComponent.class);
// if (MyComponentAnno == null)
// return;
//
// // 获取一个类的注解,根据注解名
// Method[] methds = cls.getMethods();
// for (Method method : methds) {
// MyMapping anno1 = (MyMapping) method.getAnnotation(MyMapping.class);
// if (anno1 == null)
// continue;
//
// String uri_mapping = anno1.value();
// MethodObj MethodObj1 = new MethodObj();
// MethodObj1.classProp = cls;
// MethodObj1.methodProp = method;
// url_method_maps.put(uri_mapping, MethodObj1);
// }
//
// }
// });
//
// return url_method_maps;
//}
}