注册HandlerUrlMapping
URL注册HandlerUrlMapping
package com.mayikt.init;
import com.mayikt.ext.MayiktController;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
public class MayiktAbstractHandlerUrlMapping implements ApplicationContextAware, InitializingBean {
@Autowired
private ApplicationContext applicationContext;
@Override
public void afterPropertiesSet() throws Exception {
String[] beanDefinitionNames = this.applicationContext.getBeanDefinitionNames();
Arrays.stream(beanDefinitionNames).forEach((beanName) -> {
Object handler = this.applicationContext.getBean(beanName);
if (handler != null) {
Class<?> handlerClass = handler.getClass();
Component componentController = handlerClass.getDeclaredAnnotation(Component.class);
if (componentController != null) {
String url = componentController.value();
MayiktComponentRegistContainer.getRegistUrl().put(url + ".do", handler);
}
}
});
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
}
}
package com.mayikt.init;
import java.util.HashMap;
import java.util.Map;
public class MayiktComponentRegistContainer {
private static Map<String, Object> registUrl = new HashMap<>();
public static Map<String, Object> getRegistUrl() {
return registUrl;
}
}
package com.mayikt.init.handler;
import com.mayikt.ext.MayiktHandlerExecutionChain;
import com.mayikt.ext.MayiktHandlerMapping;
import com.mayikt.init.MayiktComponentRegistContainer;
import javax.servlet.http.HttpServletRequest;
public class BeanNameUrlHandlerMapping implements MayiktHandlerMapping {
@Override
public MayiktHandlerExecutionChain getHandler(HttpServletRequest request) {
String requestURI = request.getRequestURI();
Object handler = MayiktComponentRegistContainer.getRegistUrl().get(requestURI);
MayiktHandlerExecutionChain mayiktHandlerExecutionChain =
new MayiktHandlerExecutionChain(handler);
return mayiktHandlerExecutionChain;
}
}
HandlerMethodMapping注册HandlerUrlMapping
package com.mayikt.init;
import com.mayikt.ext.MayiktController;
import com.mayikt.ext.MayiktRequestMapping;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Arrays;
@Component
public class MayiktAbstractHandlerMethodMapping implements ApplicationContextAware, InitializingBean {
private ApplicationContext applicationContext;
@Override
public void afterPropertiesSet() throws Exception {
String[] beanDefinitionNames = this.applicationContext.getBeanDefinitionNames();
Arrays.stream(beanDefinitionNames).forEach((beanName) -> {
Object handler = this.applicationContext.getBean(beanName);
if (handler != null) {
Class<?> handlerClass = handler.getClass();
MayiktController mayiktController = handlerClass.getDeclaredAnnotation(MayiktController.class);
if (mayiktController != null)
detectHandlerMethods(handler, handlerClass);
}
});
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
private void detectHandlerMethods(Object handler, Class<?> handlerClass) {
Method[] declaredMethods = handlerClass.getDeclaredMethods();
Arrays.stream(declaredMethods).forEach((method -> {
MayiktRequestMapping mayiktRequestMapping = method.getDeclaredAnnotation(MayiktRequestMapping.class);
if (mayiktRequestMapping != null) {
String reqURL = mayiktRequestMapping.value();
MayiktMappingRegistContainer.getRegistUrl().put(reqURL+".do", new MayiktMappingRegistration(handler, method, reqURL));
}
}));
}
}
package com.mayikt.init;
import java.util.HashMap;
import java.util.Map;
public class MayiktMappingRegistContainer {
private static Map<String, MayiktMappingRegistration> registUrl = new HashMap<>();
public static Map<String, MayiktMappingRegistration> getRegistUrl() {
return registUrl;
}
}
package com.mayikt.init.handler;
import com.mayikt.ext.MayiktHandlerExecutionChain;
import com.mayikt.ext.MayiktHandlerMapping;
import com.mayikt.init.MayiktAbstractHandlerMethodMapping;
import com.mayikt.init.MayiktMappingRegistContainer;
import com.mayikt.init.MayiktMappingRegistration;
import javax.servlet.http.HttpServletRequest;
public class RequestMappingHandlerMapping extends MayiktAbstractHandlerMethodMapping implements MayiktHandlerMapping {
@Override
public MayiktHandlerExecutionChain getHandler(HttpServletRequest request) {
String requestURI = request.getRequestURI();
MayiktMappingRegistration mayiktMappingRegistration = MayiktMappingRegistContainer.getRegistUrl().get(requestURI);
if (mayiktMappingRegistration == null) {
return null;
}
MayiktHandlerExecutionChain mayiktHandlerExecutionChain =
new MayiktHandlerExecutionChain(mayiktMappingRegistration);
return mayiktHandlerExecutionChain;
}
}
适配器
package com.mayikt.init.adapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface MayiktHandlerAdapter {
Object handle(HttpServletRequest request, HttpServletResponse response, Object handler);
boolean supports(Object handler);
}
package com.mayikt.init.adapter;
import com.mayikt.ext.IMayiktController;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MayiktSimpleControllerHandlerAdapter implements MayiktHandlerAdapter {
@Override
public Object handle(HttpServletRequest request, HttpServletResponse response, Object handler) {
IMayiktController mayiktController = (IMayiktController) handler;
return mayiktController.handleRequest(request, response);
}
@Override
public boolean supports(Object handler) {
return handler instanceof IMayiktController;
}
}
package com.mayikt.init.adapter;
import com.mayikt.init.MayiktMappingRegistration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MayiktRequestMappingHandlerAdapter implements MayiktHandlerAdapter {
@Override
public Object handle(HttpServletRequest request, HttpServletResponse response, Object handler) {
MayiktMappingRegistration mappingRegistration = (MayiktMappingRegistration) handler;
Method handlerMethod = mappingRegistration.getHandlerMethod();
Object handlerInfo = mappingRegistration.getHandler();
try {
Object result = handlerMethod.invoke(handlerInfo, null);
return result;
} catch (Exception e) {
return null;
}
}
@Override
public boolean supports(Object handler) {
return handler instanceof MayiktMappingRegistration;
}
}
MayiktDispatcherServlet
package com.mayikt.servlet;
import com.mayikt.adapter.MayiktHandlerAdapter;
import com.mayikt.adapter.MayiktSimpleControllerHandlerAdapter;
import com.mayikt.adapter.ReqHandlerMethodAdapter;
import com.mayikt.ext.IController;
import com.mayikt.ext.container.MayiktMappingRegistration;
import com.mayikt.ext.handlermapping.MayiktBeanNameUrlHandlerMapping;
import com.mayikt.ext.handlermapping.MayiktHandlerExecutionChain;
import com.mayikt.ext.handlermapping.MayiktHandlerMapping;
import com.mayikt.ext.handlermapping.MayiktRequestMappingHandlerMapping;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerMapping;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
@WebServlet("*.do")
public class MayiktDispatcherServlet implements Servlet {
private List<MayiktHandlerMapping> handlerMappings;
private List<MayiktHandlerAdapter> handlerAdapters;
@Override
public void init(ServletConfig config) throws ServletException {
initHandlerMapping();
initHandlerAdapter();
}
private void initHandlerMapping() {
handlerMappings = new ArrayList<>();
handlerMappings.add(new MayiktBeanNameUrlHandlerMapping());
handlerMappings.add(new MayiktRequestMappingHandlerMapping());
}
private void initHandlerAdapter() {
handlerAdapters = new ArrayList<>();
handlerAdapters.add(new MayiktSimpleControllerHandlerAdapter());
handlerAdapters.add(new ReqHandlerMethodAdapter());
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
PrintWriter writer = res.getWriter();
try {
MayiktHandlerExecutionChain handlerExecutionChain = getHandler(request);
if (handlerExecutionChain == null) {
response.setStatus(404);
return;
}
MayiktHandlerAdapter handlerAdapter = getHandlerAdapter(handlerExecutionChain.getHandler());
Object result = handlerAdapter.handle(request, response, handlerExecutionChain.getHandler());
writer.print(result);
} catch (Exception e) {
} finally {
if (writer != null) {
writer.close();
}
}
}
protected MayiktHandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
for (MayiktHandlerMapping handlerMapping : handlerMappings) {
MayiktHandlerExecutionChain handler = handlerMapping.getHandler(request);
if (handler != null) {
return handler;
}
}
return null;
}
protected MayiktHandlerAdapter getHandlerAdapter(Object handler) {
for (MayiktHandlerAdapter handlerAdapter : handlerAdapters) {
if (handlerAdapter.supports(handler)) {
return handlerAdapter;
}
}
return null;
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
相关代码
📎手写springmvc相关代码.rar