Skip to content

Commit

Permalink
优化ioc代码,为aop做准备
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiahui committed Aug 20, 2019
1 parent a68608d commit 0195ca1
Show file tree
Hide file tree
Showing 21 changed files with 89 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.xiaohui.demo;

import com.xiaohui.ioc.annotation.Autowired;
import com.xiaohui.ioc.annotation.Controller;
import com.xiaohui.ioc.annotation.RequestMapping;
import com.xiaohui.ioc.annotation.RequestParam;
import com.xiaohui.ioc.beans.factory.annotation.Autowired;
import com.xiaohui.ioc.beans.factory.annotation.Controller;
import com.xiaohui.ioc.beans.factory.annotation.RequestMapping;
import com.xiaohui.ioc.beans.factory.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.xiaohui.demo;

import com.xiaohui.ioc.annotation.Component;
import com.xiaohui.ioc.beans.factory.annotation.Component;

@Component(value = "testService")
public class TestService {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.xiaohui.ioc.beans.aware;

import context.app.ApplicationContext;

/**
* @author ly
* @create 2019-01-12 16:06
**/
public interface ApplicationContextAware {
void setApplicationContext(ApplicationContext applicationContext);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.xiaohui.ioc.beans.aware;

/**
* Aware接口 用于注入一些需要用到的对象
*/
public interface Aware {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.xiaohui.ioc.beans.aware;

import com.xiaohui.ioc.beans.factory.BeanDefinitionRegistry;

public interface BeanFactoryAware extends Aware{

void setBeanFactory(BeanDefinitionRegistry beanDefinitionRegistry);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.xiaohui.ioc;
package com.xiaohui.ioc.beans.factory;

import java.util.Map;

public interface BeanFactory {
public interface BeanDefinitionRegistry {
/**
* 根据id获取bean
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.xiaohui.ioc;
package com.xiaohui.ioc.beans.factory;

import com.xiaohui.ioc.support.BeanDefinition;
import com.xiaohui.ioc.beans.factory.config.BeanDefinition;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.xiaohui.ioc.annotation;
package com.xiaohui.ioc.beans.factory.annotation;

import java.lang.annotation.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.xiaohui.ioc.annotation;
package com.xiaohui.ioc.beans.factory.annotation;

import java.lang.annotation.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.xiaohui.ioc.annotation;
package com.xiaohui.ioc.beans.factory.annotation;

import java.lang.annotation.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.xiaohui.ioc.annotation;
package com.xiaohui.ioc.beans.factory.annotation;

import java.lang.annotation.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.xiaohui.ioc.annotation;
package com.xiaohui.ioc.beans.factory.annotation;

import java.lang.annotation.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.xiaohui.ioc.support;
package com.xiaohui.ioc.beans.factory.config;

/**
* Bean的元数据类
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.xiaohui.ioc.support;
package com.xiaohui.ioc.beans.factory.config;

import com.xiaohui.ioc.annotation.Component;
import com.xiaohui.ioc.annotation.Controller;
import com.xiaohui.ioc.beans.factory.annotation.Component;
import com.xiaohui.ioc.beans.factory.annotation.Controller;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.xiaohui.ioc.support;
package com.xiaohui.ioc.beans.factory.config;


import com.xiaohui.ioc.BeanRegister;
import com.xiaohui.ioc.beans.factory.BeanRegister;

import java.io.File;
import java.net.URL;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.xiaohui.ioc.support;

import com.xiaohui.ioc.BeanFactory;
import com.xiaohui.ioc.BeanRegister;
import com.xiaohui.ioc.beans.factory.BeanDefinitionRegistry;
import com.xiaohui.ioc.beans.factory.BeanRegister;
import com.xiaohui.ioc.beans.factory.config.BeanDefinition;
import com.xiaohui.ioc.beans.factory.config.BeanDefinitionParser;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -11,7 +13,7 @@
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

public class AnnotationBeanFactory implements BeanFactory, BeanRegister {
public class AnnotationBeanFactory implements BeanDefinitionRegistry, BeanRegister {
private Map<String, Object> instanceMapping = new ConcurrentHashMap<>();

// 保存所有bean的信息,主要包含bean的类型 id等信息(初始化时,遍历该list,实例化所有bean)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xiaohui.ioc.support;

import com.xiaohui.ioc.BeanRegister;
import com.xiaohui.ioc.beans.factory.BeanRegister;
import com.xiaohui.ioc.beans.factory.config.BeanDefinition;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.xiaohui.ioc.support;

import com.xiaohui.ioc.annotation.Autowired;
import com.xiaohui.ioc.beans.factory.annotation.Autowired;
import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public HandlerAdapter(Map<String, Integer> paramType) {
*
* @param req
* @param resp
* @param handler 和url匹配的handler
* @param handlerMapping 和url匹配的handler
* @throws Exception
*/
public void handle(HttpServletRequest req, HttpServletResponse resp, Handler handler) throws Exception {
public void handle(HttpServletRequest req, HttpServletResponse resp, HandlerMapping handlerMapping) throws Exception {
// 获取要调用方法的全部参数类型
Class<?>[] parameterTypes = handler.getMethod().getParameterTypes();
Class<?>[] parameterTypes = handlerMapping.getMethod().getParameterTypes();
// 创建一个反射调用需要的参数值得数组,数组长度和参数长度一样
Object[] paramValues = new Object[parameterTypes.length];
/**
Expand Down Expand Up @@ -57,7 +57,7 @@ public void handle(HttpServletRequest req, HttpServletResponse resp, Handler han
}
}
// 最后反射调用Controller的method方法
handler.getMethod().invoke(handler.getController(), paramValues);
handlerMapping.getMethod().invoke(handlerMapping.getController(), paramValues);
}

private Object castValueType(String value, Class<?> clazz) {
Expand Down
14 changes: 12 additions & 2 deletions ...rc/main/java/com/xiaohui/mvc/Handler.java → .../java/com/xiaohui/mvc/HandlerMapping.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.lang.reflect.Method;
import java.util.regex.Pattern;

public class Handler {
public class HandlerMapping {

// 调用对应的具体controller对象
private Object controller;
Expand All @@ -13,7 +13,7 @@ public class Handler {
// 对应RequestMapping的url正则
private Pattern pattern;

public Handler(Object controller, Method method, Pattern pattern) {
public HandlerMapping(Object controller, Method method, Pattern pattern) {
this.controller = controller;
this.method = method;
this.pattern = pattern;
Expand Down Expand Up @@ -42,4 +42,14 @@ public Pattern getPattern() {
public void setPattern(Pattern pattern) {
this.pattern = pattern;
}

@Override
public int hashCode() {
return super.hashCode();
}

@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.xiaohui.mvc.servlet;


import com.xiaohui.ioc.annotation.Controller;
import com.xiaohui.ioc.annotation.RequestMapping;
import com.xiaohui.ioc.annotation.RequestParam;
import com.xiaohui.ioc.beans.factory.annotation.Controller;
import com.xiaohui.ioc.beans.factory.annotation.RequestMapping;
import com.xiaohui.ioc.beans.factory.annotation.RequestParam;
import com.xiaohui.ioc.support.AnnotationBeanFactory;
import com.xiaohui.mvc.Handler;
import com.xiaohui.mvc.HandlerMapping;
import com.xiaohui.mvc.HandlerAdapter;

import javax.servlet.ServletException;
Expand All @@ -22,8 +22,8 @@
public class DispatcherServlet extends HttpServlet {

private static final String CONTEXT_CONFIG_LOCATION = "globalConfig";
private List<Handler> handlerMapping = new ArrayList<>();
private Map<Handler, HandlerAdapter> adapterMapping = new ConcurrentHashMap<>();
private List<HandlerMapping> handlerMapping = new ArrayList<>();
private Map<HandlerMapping, HandlerAdapter> adapterMapping = new ConcurrentHashMap<>();

@Override
public void init() throws ServletException {
Expand Down Expand Up @@ -69,8 +69,8 @@ private void initHandlerExceptionResolvers(AnnotationBeanFactory context) {
private void initHandlerAdapters(AnnotationBeanFactory context) {
if (handlerMapping.isEmpty()) return;
// 遍历所有的handlerMapping
for (Handler handler : handlerMapping) {
Method method = handler.getMethod();
for (HandlerMapping handlerMapping : handlerMapping) {
Method method = handlerMapping.getMethod();
// 创建一个保存RequestParam 注解的value(即参数名)==>index(参数位置索引)
Map<String, Integer> paramType = new HashMap<String, Integer>();
// 获取所有的参数类型数组
Expand Down Expand Up @@ -101,7 +101,7 @@ private void initHandlerAdapters(AnnotationBeanFactory context) {
}
}
}
adapterMapping.put(handler, new HandlerAdapter(paramType));
adapterMapping.put(handlerMapping, new HandlerAdapter(paramType));
}

}
Expand Down Expand Up @@ -133,7 +133,7 @@ private void initHandlerMappings(AnnotationBeanFactory context) {
String regex = (url + subUrl).replaceAll("/+", "/");
Pattern pattern = Pattern.compile(regex);
// 添加到handlerMapping中去
handlerMapping.add(new Handler(entry.getValue(), method, pattern));
handlerMapping.add(new HandlerMapping(entry.getValue(), method, pattern));
}

}
Expand Down Expand Up @@ -166,35 +166,35 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S

private void doDispatch(HttpServletRequest req, HttpServletResponse resp) {
// 取出匹配的handler,如果没有则跳过
Optional.ofNullable(getHandler(req)).ifPresent(handler -> {
Optional.ofNullable(getHandler(req)).ifPresent(handlerMapping -> {
// 根据handler取出HandlerAdapter
HandlerAdapter ha = getHandlerAdapter(handler);
HandlerAdapter ha = getHandlerAdapter(handlerMapping);

// 调用handle方法处理请求,暂时未做ModalAndView处理
try {
ha.handle(req, resp, handler);
ha.handle(req, resp, handlerMapping);
} catch (Exception e) {
e.printStackTrace();
}
});

}

private HandlerAdapter getHandlerAdapter(Handler handler) {
private HandlerAdapter getHandlerAdapter(HandlerMapping handlerMapping) {
if (adapterMapping.isEmpty()) return null;
return adapterMapping.get(handler);
return adapterMapping.get(handlerMapping);
}

private Handler getHandler(HttpServletRequest req) {
private HandlerMapping getHandler(HttpServletRequest req) {
if (handlerMapping.isEmpty()) return null;
String contextPath = req.getContextPath();
String url = req.getRequestURI();
// 获取请求的url 除去contextPath剩余的
url = url.replace(contextPath, "").replaceAll("/+", "/");
for (Handler handler : handlerMapping) {
if (handler.getPattern().matcher(url).matches()) {
for (HandlerMapping handlerMapping : handlerMapping) {
if (handlerMapping.getPattern().matcher(url).matches()) {
// 匹配到就把handler返回
return handler;
return handlerMapping;
}
}
return null;
Expand Down

0 comments on commit 0195ca1

Please sign in to comment.