手寫迷你Spring框架
public class DispatcherServlet extends HttpServlet {
private Map<String,Object> mapping = new HashMap<String, Object>();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doPost(req,resp);}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
doDispatch(req,resp);
} catch (Exception e) {
resp.getWriter().write("500 Exception " + Arrays.toString(e.getStackTrace()));
}
}
private void doDispatch(HttpServletRequest req, HttpServletResponse resp) throws Exception {
String url = req.getRequestURI();
String contextPath = req.getContextPath();
url = url.replace(contextPath, "").replaceAll("/+", "/");
if(!this.mapping.containsKey(url)){resp.getWriter().write("404 Not Found!!");return;}
Method method = (Method) this.mapping.get(url);
Map<String,String[]> params = req.getParameterMap();
method.invoke(this.mapping.get(method.getDeclaringClass().getName()),new Object[]{req,resp,params.get("name")[0]});
}
// 當我暈車的時候,我就不去看原始碼了
//init 方法肯定幹得的初始化的工作
//inti 首先我得初始化所有的相關的類, IOC 容器、 servletBean
@Override
public void init(ServletConfig config) throws ServletException {
InputStream is = null;
try{
Properties configContext = new Properties();
is = this.getClass().getClassLoader().getResourceAsStream(config.getInitParameter("contextConfigLocation"));
configContext.load(is);
String scanPackage = configContext.getProperty("scanPackage");
doScanner(scanPackage);
for (String className : mapping.keySet()) {
if(!className.contains(".")){continue;}
Class<?> clazz =外匯跟單gendan5.com Class.forName(className);
if(clazz.isAnnotationPresent(Controller.class)){
mapping.put(className,clazz.newInstance());
String baseUrl = "";
if (clazz.isAnnotationPresent(RequestMapping.class)) {
RequestMapping requestMapping = clazz.getAnnotation(RequestMapping.class);
baseUrl = requestMapping.value();
}
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (!method.isAnnotationPresent(RequestMapping.class)) { continue; }
RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
String url = (baseUrl + "/" + requestMapping.value()).replaceAll("/+", "/");
mapping.put(url, method);
System.out.println("Mapped " + url + "," + method);
}
}else if(clazz.isAnnotationPresent(Service.class)){
Service service = clazz.getAnnotation(Service.class);
String beanName = service.value();
if("".equals(beanName)){beanName = clazz.getName();}
Object instance = clazz.newInstance();
mapping.put(beanName,instance);
for (Class<?> i : clazz.getInterfaces()) {
mapping.put(i.getName(),instance);
}
}else {continue;}
}
for (Object object : mapping.values()) {
if(object == null){continue;}
Class clazz = object.getClass();
if(clazz.isAnnotationPresent(Controller.class)){
Field [] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if(!field.isAnnotationPresent(Autowired.class)){continue; }
Autowired autowired = field.getAnnotation(Autowired.class);
String beanName = autowired.value();
if("".equals(beanName)){beanName = field.getType().getName();}
field.setAccessible(true);
try {
field.set(mapping.get(clazz.getName()),mapping.get(beanName));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
} catch (Exception e) {
}finally {
if(is != null){
try {is.close();} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.print("MVC Framework is init");
}
private void doScanner(String scanPackage) {
URL url = this.getClass().getClassLoader().getResource("/" + scanPackage.replaceAll("\\.","/"));
File classDir = new File(url.getFile());
for (File file : classDir.listFiles()) {
if(file.isDirectory()){ doScanner(scanPackage + "." + file.getName());}else {
if(!file.getName().endsWith(".class")){continue;}
String clazzName = (scanPackage + "." + file.getName().replace(".class",""));
mapping.put(clazzName,null);
}
}
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2911202/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 手寫Spring框架Spring框架
- 手寫Spring ioc 框架,狠狠的“Spring 原始碼Spring框架原始碼
- 《四 spring原始碼》手寫springioc框架Spring原始碼框架
- 手寫Spring MVC框架(一) 實現簡易版mvc框架SpringMVC框架
- 手寫一個最迷你的Web伺服器Web伺服器
- 手寫mybatis框架MyBatis框架
- 手寫 SpringSpring
- 手寫一個迷你版 HashMap,面試隨便問!HashMap面試
- 手寫RPC框架RPC框架
- 純手寫web框架Web框架
- 手寫Spring MVC框架(二) 實現訪問攔截功能SpringMVC框架
- 手寫mini版MVC框架MVC框架
- 純手寫Arraylist集合框架框架
- 手寫自己的MyBatis框架-SqlSessionMyBatis框架SQLSession
- 手寫簡易版React框架React框架
- 自己動手寫 PHP 框架(一)PHP框架
- 迷你 JS 框架 Hyperapp 原始碼解析JS框架APP原始碼
- 手寫事件釋出訂閱框架事件框架
- 兩小時手寫springmvc框架SpringMVC框架
- 手寫開源ORM框架介紹ORM框架
- 手寫Spring---IOC容器(1)Spring
- 帶你手寫基於 Spring 的可插拔式 RPC 框架(一)介紹SpringRPC框架
- 手寫Spring框架,是時候擼個AOP與Bean生命週期融合了!Spring框架Bean
- Spring原始碼系列:初探底層,手寫SpringSpring原始碼
- JavaScript 編寫的迷你 Lisp 直譯器JavaScriptLisp
- 基於netty手寫RPC框架NettyRPC框架
- 仿照dubbo手寫一個RPC框架RPC框架
- 自己動手寫一個持久層框架框架
- 手寫RPC框架(六)整合NettyRPC框架Netty
- 手寫Android網路框架——CatHttp(一)Android框架HTTP
- 手寫Android網路框架——CatHttp(二)Android框架HTTP
- 嘗試手寫一個註解框架框架
- pbgo: 基於Protobuf的rpc/rest迷你框架GoRPCREST框架
- 用一個檔案,實現迷你 Web 框架Web框架
- 《四 spring原始碼》手寫springmvc原始碼SpringMVC
- Spring Boot 動手寫一個 StartSpring Boot
- Spring學習之——手寫Mini版Spring原始碼Spring原始碼
- 《四 spring原始碼》利用TransactionManager手寫spring的aopSpring原始碼