mysql学习笔记
2013-05-17 12:51
197 查看
<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog336280.html" frameborder="0" width="336" scrolling="no" height="280"></iframe> 由于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/
用户定义的所有的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/
相关文章推荐
- mysql帮助文档学习笔记
- mysql学习的笔记A
- Mysql学习笔记(三)运算符和控制流函数
- MySQL学习笔记(1)
- SQL Window - oracle 与 MySQl的学习笔记(一)
- 21.笔记 MySQL学习——MyISAM存储引擎
- MySQL 学习笔记--引擎
- 学习笔记 php mysql apache 的安装
- Mysql学习笔记二十三——事务
- MySQL 学习笔记 一
- Mysql学习笔记三
- MySQL高性能学习笔记
- 【数据库学习笔记】MySQL_02_查询,约束
- PHP学习笔记之1--开发环境搭建IIS +PHP +Mysql
- mysql学习笔记
- MySQL学习笔记5:修改表(alter table)
- Mysql存储过程学习笔记--建立简单的存储过程
- MySQL学习笔记——备份和恢复
- lighttpd+PHP(FAST-CGI)+mysql的学习笔记(转)
- 【MySQL】《高性能MySQL》 学习笔记,第一章