`
izuoyan
  • 浏览: 8936231 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

超轻量级MVC框架的设计和实现 (3)

阅读更多

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

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

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

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

chain.doInterceptor(action);

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

下面是几个简单的示例:

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

package com.javaeedev.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.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接口,在DispatcherServlet中实例化该Module的时候就可以传入ServletContext对象:

package com.javaeedev.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/

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics