---
title: "超轻量级MVC框架的设计和实现 (3)"
author: "mmdev"
date: "2007-11-27T17:22:00Z"
tags: []
slug: "mvc-3"
draft: false
lastmod: "2007-11-27T17:22:00Z"
id: 1006595
url: "https://mmdev.iteye.com/blog/1006595"
---

由于MVC框架的设计，必须保证配置IoC容器时指定正确的Scope，否则无法正常工作：

用户定义的所有的Action类必须是Prototype域，即Guice默认的作用域，如果定义为Singleton会导致并发请求的数据混乱；

用户定义的所有的Interceptor，ViewResolver和ExceptionResolver应该是Singleton的，MVC框架会保持其引用，即时定义为Prototype也是无效的。

在实现Interceptor时需要注意，如果要继续处理请求，就必须调用

chain.doInterceptor(action);

否则请求不会被继续处理。多个Interceptor的排列顺序很重要，用@Order指定，小的在前，即优先级高。

下面是几个简单的示例：

一个处理事务的Interceptor，能保证每个Action的执行都在事务范围内：

package com.[javaeedev](http://www.javaeedev.com/).lightweight.mvc.sample;

@Singleton  
@Order(0)  
public class TransactionInterceptor implements Interceptor {

@Inject TransactionManager transactionManager;

public void intercept(Action action, InterceptorChain chain) throws Exception {  
Transaction tx = transactionManager.beginTransaction();  
try {  
chain.doInterceptor(action);  
tx.commit();  
}  
catch(Exception e) {  
tx.rollback();  
throw e;  
}  
}

}

一个处理所有异常的ExceptionHandler：

package com.[javaeedev](http://www.javaeedev.com/).lightweight.mvc.sample;

@Singleton  
public class ExceptionHandler implements ExceptionResolver {

public void init(ServletContext context) throws ServletException {  
}

public ModelAndView handleException(Action action, Exception e) throws Exception {  
if(e instanceof NeedSignonException) {  
// let user sign on:  
return new ModelAndView("/signon.html");  
}  
throw e;  
}

}

很多时候，组件的初始化需要依赖当前Web应用的ServletContext对象，在IoC容器中如何传入ServletContext？对于Guice来说，需要编写一个Module告诉容器各组件的信息，因此，让Module实现一个[ServletContextAware](http://lightweight-mvc.googlecode.com/svn/trunk/lightweight-mvc/src/com/javaeedev/lightweight/mvc/ServletContextAware.java)接口，在DispatcherServlet中实例化该Module的时候就可以传入ServletContext对象：

package com.[javaeedev](http://www.javaeedev.com/).lightweight.mvc.sample;

public class MyModule implements Module, ServletContextAware {

private ServletContext context;

public void setServletContext(ServletContext context) {  
this.context = context;  
}

public void configure(Binder binder) {  
... // init all beans here...  
}

}

传入ServletContext的目的是为了让组件能从中获得当前Web应用目录下的资源文件。

关于这个MVC框架的设计就介绍到此，感兴趣的朋友可以从以下地址下载：

[http://code.google.com/p/lightweight-mvc/](http://code.google.com/p/lightweight-mvc/)