Sunday, November 4, 2012

Spring foundations [EN]

The main fundamentals of Spring are:
  • dependency injection
  • inversion of control (IoC)
  • aspect oriented programing (AOP)
  • integration with third-party frameworks
Dependency injection consists in changing concrete class implementations in a transparent way, without directly modifying the code we are using. This is accomplished through an interfaces based programming.

Suppose we are implementing a service that contains a DAO interface that executes data persistence. Imagine that the implementation of this DAO uses Hibernate for persistence. If at any time we wanted to change the DAO to one that uses i-BATIS, we should remove the previous implementation to put the new one, in other words, inject that dependency. In Spring this can be done very easily with the IoC Container. In this example we should only modify an XML for indicating what is the new class that implements the i-BATIS DAO. But we don’t need change any Java class to get the result.

Inversion of control (IoC) basically assumes that the programmer doesn’t have to create new instances of classes that he needs with the new operator. By using interfaces, we shouldn’t worry about how and when they are created. We just have to define the required beans in an XML file, detailing its concrete implementation and other properties. The Spring container will know when and how to instantiate the implementations needed.

The Spring integration with other frameworks consists in code reuse, especially if it's good. Spring is a very flexible framework and it doesn’t tries to reinvent the wheel, if there are solutions to problems that work developed with different technologies Spring allows to adopt and use them.

For example, imagine that we are working in the persistence layer and we are developing a DAO. Spring provides an abstract class DaoSupport that we could extend to make our own DAO. But if we want to work with a specific data persistence framework, Spring also provides abstract classes for using it. For example, in Spring we have the following abstract classes: HibernateDaoSupportJdbcDaoSupportJpaDaoSupportSqlMapClientDaoSupportTopLinkDaoSupport, etc.

No comments:

Post a Comment