Skip to main content

Posts

简单的MVC就够了吗?浅谈service Layer的引入

MVC是web开发中常见的程序结构。 简单的mvc结构如下: view层:显示层。 control层:业务层,集合了各种action。 model层:模型层,一般和数据打交道。简单的sample:一个表对应一个model类。 其中control层调用model层的方法,实现对数据的访问。 采用这样的结构在一定程度上,可以做到代码清晰,较容易扩展,代码的管理复杂度较低。 但是如果是业务很多,逻辑又很复杂的网站,如果再加上开发人员的水平参差不齐,那必然会导致下面的情况: 1 action中的代码越来越长,逻辑越来越复杂,不同action之间看起来有很多可以重用的代码, 但是真要进行重构的话,又非常困难。 2 model层中包含的方法越来越多,有些方法也过于复杂。甚至在不少方法中还包含了业务逻辑。 3 代码的修改,还是牵一发而动全身。 4 代码难以进行自动化测试。 本来以为引入了mvc,程序的管理复杂度问题就高枕无忧了,但现在又面临了相同的问题了。 以我最近的所学看,在mvc中再引入service层,可以在很大程度上避免或者缓解上述问题。 原有的mvc结构改成如下: 1 view层:显示层。 2 control层:业务层,集合了各种action。 3 service层。 4 DAO层。 原来的model层不见了,增加了service层和DAO层。 DAO,即Data Access Object,数据访问接口,数据访问:顾名思义就是与数据库打交道 。 在这个结构中,control不直接和DAO联系, 需要操作数据的时候,通过service层访问DAO层来实现。 service层做的事情,不仅仅是调用DAO操作数据,还会包含了一定的业务逻辑。整个程序的设计,也变成了针对服务进行设计。 这样做的好处是: 1 control层中的action得以精简,因为action中的一些逻辑,被重构成一个个的服务。 而不同的action也可以重用服务了 。 2 只负责和数据打交道的DAO层,相比之前的model层,也得以精简( DAO层尽量只做最原子的数据操作,不同数据操作之间的联系,这边不考虑,那是service层的事情 )。 3 ser...

Spring MVC 3.2 Preview: Introducing Servlet 3, Async Support

Continuing the Spring 3.0 "simplification series" started by Keith and Chris , I would like to provide a quick overview of simplifications in scheduling and task execution enabled by Spring 3.0. I will be walking through a basic sample application that you can checkout from the spring-samples Subversion repository. It has been designed to be as simple as possible while showcasing both annotation-driven and XML-based approaches to scheduling tasks in Spring 3.0. Let's begin with the annotation-driven approach. You can run it directly via the main() method in AnnotationDemo. If you take a look, you'll see that it's nothing more than a bootstrap for a Spring ApplicationContext: public static void main(String[] args) {      new ClassPathXmlApplicationContext( "config.xml" , AnnotationDemo. class ); } The reason nothing else is necessary is that the ApplicationContext contains an "active" component, which we will...

Apache Camel 框架集成Spring

Apache Camel提供了和Spring的集成,通过Spring容器(ApplicationContext)来管理Camel的CamelContext,这样的话,就不需要写代码来控制CamelContext的初始化,启动和停止了.Camel会随着Spring的启动而启动起来. 本文将Apache Camel框架入门示例(http://blog.csdn.net/kkdelta/article/details/7231640)中的例子集成到Spring中,下面简单介绍一下集成的基本步骤. 1,新建一个Eclipse工程,将Spring3的jar包,和Camel的jar包配置到工程的classpath. 2,Route类要继承RouteBuilde,如下 1 public   class   FileProcessWithCamelSpring  extends   RouteBuilder { 2      @Override 3      public   void   configure()  throws   Exception { 4          FileConvertProcessor processor =  new   FileConvertProcessor(); 5          from( "file:d:/temp/inbox?delay=30000" ).process(processor).to( "file:d:/temp/outbox" );        6      } 7 } 3,Processor仍然和和入门示例的代码相同. 01 public   ...