spring mvc 启动过程可分为如下3步:
=============================================
web.xml
DispatcherServlet Spring MVC总入口
1、配置阶段 配置初始化参数 classpathapplication.xml 配置了无数个bean
/*.json
Init方法 被web容器调用,如tomcat
加载application.xml内容
扫描相关联的class 根据用户配置的包名,用list存下来
2、初始化阶段 IOC容器初始化 就是一个map, beanId 实例
依赖注入DI 如果声明类中定义了成员变量,而且需要赋值
初始化一个HandlerMapping 将URL和contral类中某一个方法进行关联,保存到一个map中
service方法 doget/dopost
调用一个doDispatch方法 根据URL去匹配已经初始化完毕的HandleMapping中的method
3、运行阶段 通过反射机制,动态调用
通过Respons输出
简易源码实现过程如下:
package spring;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.ControllerEventListener;
import com.sun.corba.se.impl.encoding.OSFCodeSetRegistry.Entry;
import com.sun.jmx.remote.util.Service;
import com.sun.org.apache.xml.internal.security.Init;
public class guoDispacherServlet extends HttpServlet{
private Properties p = new Properties();
private List<String> classNames = new ArrayList<String>();
private Map<String, Object> ioc = new HashMap<String, Object>();
private void Init(ServletConfig config) throws javax.servlet.ServletException {
//1.加载配置文件
doloadConfig(config.getInitParameter(“contextConfiglocation”));
//2.扫描所有相关类
doScanner(p.getProperty(“scanPackeage”));
//3.初始化所有class相关实例,并且保存到IOC容器中
doInstance();
//4.自动化的依赖注入
doAutowired();
//5.初始化HandlerMapping
initHandlerMapping();
System.out.println(“初始化成功!”);
}
private void initHandlerMapping() {
// TODO Auto-generated method stub
}
private void doAutowired() {
if(ioc.isEmpty()){
return;
}
for(java.util.Map.Entry<String, Object> entry:ioc.entrySet()){
Field[] fields = entry.getValue().getClass().getDeclaredFields();
for(Field field: fields){
if(!field.isAnnotationPresent(Autowried.class)){
continue;
}else{
Autowried autoweried field.getAnnotation(Autowried.class);
}
}
}
}
private void doInstance() {
if(classNames.isEmpty()){
return;
}
for(String className:classNames){
Class<?> clazz = Class.forName(className);
//进行实例化
if(clazz.isAnnotationPresent(Service.class)){
String beanName = lowerFirst(clazz.getSimpleName());
ioc.put(beanName, clazz.newInstance());
}else if(clazz.isAnnotationPresent(ControllerEventListener.class)){
}else{
continue;
}
}
}
private void doScanner( String packageName) {
URL url = this.getClass().getClassLoader().getResource(“/”+packageName.replaceAll(“\\.”, “/”));
File classerDir = new File(url.getFile());
for(File file:classerDir.listFiles()){
if(file.isDirectory()){
doScanner(packageName+”.”+file.getName());
}else{
String className = packageName+”.”+file.getName().replace(“.class”,””);
classNames.add(className);
}
}
}
private void doloadConfig(String location) {
InputStream is = this.getClass().getClassLoader().getResourceAsStream(location);
p.load(is);
}
//字符串首字母变小写函数
private String lowerFirst(String str){
char[] chars = str.toCharArray();
chars[0] +=32;
return String.valueOf(chars);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws javax.servlet.ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws javax.servlet.ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(req, resp);
doDispatch();
}
private void doDispatch() {
// TODO Auto-generated method stub
}
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws javax.servlet.ServletException, IOException {
// TODO Auto-generated method stub
super.service(arg0, arg1);
}
}