Thursday, 24 March 2016

Why use Spring Data?

Spring wants to rule the entire enterprise development life-cycle, from being non-invasive framework which worked without getting your code being aware about it, to littering the code with @Autowire to @EnableMVC, Spring now is in every java source file. The dependency on spring is so much that there is no way of getting spring out of your enterprise code once it gets there.


So here comes Spring to provide you an improved way to access data with in your application by providing modules like;

    Spring Data Commons
    Spring Data JPA
    Spring Data MongoDB
    Spring Data Redis
    Spring Data KeyValue
    Spring Data Solr
    Spring Data REST
    Spring Data Neo4j (community module)
    Spring Data Cassandra (community module)
    Spring Data Couchbase (community module)
    Spring Data Elasticsearch (community module)

This covers most of the data sources you will find out there, and simplifies the configuration you will need to write down to access these data sources greatly.



The   Hibernate paging feature comes to you with the Repository.


Now just to make life a bit easier for the developers, Spring Data allows you to write queries specific to the underlying data data store. You use the common spring way to access the database, with REST module  added you can call methods using Restful call from any client like JavaScript.


The same POJO which represents the model can easily provide you access to the data using the @Repository.


Spring Data takes this simplification one step forward and makes it possible to remove the DAO implementations entirely – the interface of the DAO is now the only artifact that need to be explicitly defined.


Spring Data amazingly removes the Data access object out of the equation. You just provide the Repository interface and Spring Data does the job perfectly.


The magic of Spring Data can be leveraged by using a small set of Annotations like below,


@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.repositories"})
public class AppConfig{
}

So you repository interface just needs to implement JpaRepository,

and the implementing classes will be found automatically by Spring. These methods can be exposed as webservice by just including the Spring Jars in the class path.






No comments:

Post a Comment