需要联系的几个文件:DaosupportController,extents Controller,BeanFactory,CoreDispatcherController,appcontext.xml.
有web.xml中
<servlet>
<!-- servlet获得控制文件Class的名字,类名 -->
<servlet-name>smvcCoreDispatcher</servlet-name>
<servlet-class>org.bluechant.mvc.core.CoreDispatcherController</servlet-class>
.....
</servlet>
有CoreDispatcherController开始-->
public void invokeActionHandler(ModelAndView mv , HttpServletRequest request) throws Exception{
String className = mv.getClassName();
String methodName = mv.getMethodName();
//load class
Class controllerClass = cache.loadClass(className);//== Class.forName(className);java反射机制,jvm加载lassName类
Class parentControllerClass = cache.loadClass(baseControllerClass);//class org.bluechant.mvc.controller.Controller
//load method参数1类,创建一个方法为setRequest参数为HttpServletRequest.class的方法与method = clazz.getDeclaredMethod(setRequest, HttpServletRequest.class);与HttpServletRequest的setRequest方法一致的方法.
Method setRequest = cache.loadMethod(parentControllerClass, "setRequest", new Class[] { HttpServletRequest.class }); //HttpServletRequest.class,java的反射机制得到自己的类,能够拥有自己的方法值,(Method setRequest获取成员函数)
Method setModelAndView = cache.loadMethod(parentControllerClass, "setModelAndView", new Class[] { ModelAndView.class });//org.bluechant.mvc.controller.Controller-setModelAndView@6024418 public void org.bluechant.mvc.controller.Controller.setModelAndView(org.bluechant.mvc.controller.ModelAndView)
Method targetMethod = cache.loadMethod(controllerClass, methodName, new Class[]{});
//buiid controller instance and invoke target method以上setRequest,setModelAndView,targetMethod都放在cache(hashMap中)
Object instance = controllerClass.newInstance();//加载className类;//此方法引入数据库连接
//以上刚进入页面的时候instance调用三个类初始化,分别是继承关系DaosupportController ,controller,和BeanFactory
/**//*--->>>其中DaoSupportController类中protected ObjectDao dao = (ObjectDao)BeanFactory.getBean("objectDao");
--->>>调用:getBean 方法public class BeanFactory {
public static ApplicationContext context = new ClassPathXmlApplicationContext("appcontext.xml") ;//此方法引入数据库连接
public static Object getBean(String beanId) {
return context.getBean(beanId);
}
}*/
setRequest.invoke(instance, new Object[] { request });//对带有指定参数的指定对象调用由此 Method 对象表示的基础方法
setModelAndView.invoke(instance, new Object[] { mv });//instance立即为原型指针
// --->>>执行指定的targetMethod方法实则为AccountController的login()登陆方法此时开始调用数据库
targetMethod.invoke(instance, new Object[]{});
//调用instance类中targetMethod这个方法,Object[]{}这个作为参数..
//invoke根据实体获得方法,添加所要造的参数,就是个找实例的方法克隆工厂,由Method获得实例模型,由方法锻造样子,传入参数得出想要结果
}
}