Friday, July 27, 2012

Let's start with Spring Framwork- Essentials, Conteneur Leger, IoC et Dependency Injection.


Some Definitions :


- The key of the Spring module are:
  • Inversion of Control
  • Aspect-Oriented Programming: AOP enables behavior tht would otherwise be scattered through different methods to be modularized in a single place (typical concerns are transaction management, logging, failer monitoring)
  • Data access abstraction: spirng encourages a consistent architectural approach to data access, and provides a unique an powerful abstraction to implement it.
  • JDBC simplification
  • Transaction Management: spring provides a transaction abstraction that can sit over "global" transactions (managed by an application server) or "local" transactions using the JDBC, Hibernate...
  • MVC web framework
- Spring addresses each layer :
  • Presentation layer with the Spring MVC framework which is similar to struts
  • Logic layer by using a lighweight IoC container and supporting the AOP (crosscutting aspects such as security, log,...)
  • Persistence layer supporting different ORM.
- When you create a bean definition what you are actually creating is a recipe for creating actual instances of the class defined by the bean definition.
- the scopes supported by spring are :
  •  singleton, prototype
  • request, session and global session : valid in the context of a web-aware Srping ApplicationContext
- The scope of the Spring singleton described as per container and per bean. This means that if you define one bean for a particular class in a single spring container, then the spring container will create one and only one instance of the class defined by this bean definition.
-The prototype scope results in the creation of a new bean instance every time a request for that specific bean is made. You should use the prototype scope for all beans that are stateful, while the singleton scope should be used for stateless beans. The container instantiates, configures, decorates and otherwise assemble a protorype object, hands it to the client and then has no further knowledge of that prototype instance. It is the responsibility of the client code to clean up prototype scoped objects and release any expensive resources that the prototype bean are holding onto.

- The BeanFactory is the actual container which instantiates, configures, and manages a number of beans.
- ApplicationContexts are a subclass of BeanFactory

ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
        new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
// of course, an ApplicationContext is just a BeanFactory
BeanFactory factory = (BeanFactory) appContext;
 
- BeanFactory is lightweight, but if you're going to be using Spring "for real", you may as well go with the ApplicationContext
- The ApplicationContext builds on top of the BeanFactory (it's a subclass) and adds other functionality such as easier integration with Springs AOP features, message resource handling (for use in internationalization), event propagation, declarative mechanisms to create the ApplicationContext and optional parent contexts, and Application-layer specific contexts such as the WebApplicationContext, among other enhancements.
 
 

Inverion of Control:

-Inversion of Control has already been referred to as Dependency Injection. The basic principle is that beans define their dependencies only through constructor arguments, arguments to a factory method, or properties which are set on the object instance after it has been constructed or returned from a factory method. Then, it is the job of the container to actually inject those dependencies when it creates the bean.

- With IoC, framework code invokes application code, coordinating overall workflaw, rather than application code invoking framework code. It follows the Hollywood Principle where the framework code invokes application code, coordinating overall workflow, rather than application code invoking framework code.
- Dependency Injection is a form of push configuration; the container pushes dependencies into application objects at runtime. this is the opposite of traditional pull configuration, in which the application object pull dependencies from the environment.
- The basic IoC container in Spring is called bean factory
- Spring supports a somewhat more advenced bean factory, called the application context
- An application context is a bean factory with the org.springframework.context.Application context interface being a subclass of BeanFactory.
- In Spring, the controller (DispatcherServlet) controls the view and model by facilitating data exchange between them. No one of the two components is aware of the other component. So the model worry only about the data but not the view.



Ref:

http://www.springsource.org/
Professional Java Development with the Spring Framework, by Rod Johnson et al.

Articles les plus consultés