What is Spring Boot and why it is a preferred framework for creating Micro Services
Why create Micro Services using Spring Boot?
Spring boot is a well known preferred framework in Java landscape for creating Micro Services . Using Spring boot framework, Java applications can be created very easily without much efforts. We will see now some of the features of Spring boot framework which makes it ideal contender for creating Micro Services.
1.Let’s first see sample Spring app Maven file.
If you look at Maven XML file in picture above, Hibernate is used as an ORM framework and H2 is the underlying in memory database. If you closely watch here, all database as well as ORM related dependencies needs to be explicitly specified in the xml file , though it then in turn resolves all its interconnected dependencies due to Maven. What if, we get hold of some framework/tool due to which we don’t have to specify above dependencies which can save a lot of developer time and resources. To make this feature a reality Spring has come up with a framework called Spring Boot.
2. What is @Conditional Annotation of Spring framework?
If you look at HibernateJpaAutoConfiguration Class coming from spring-boot-autoconfigure jar in above picture , you will find @Conditional Annotation (Please see arrow mark), which looks for HibernateEntityManagerCondition.
If you take a look at HibernateEntityManagerCondition class in picture above, you will see that HibernateEntityManagerCondition is satisfied only if either of (please see arrow mark) org.hibernate.ejb.HibernateEntityManager or org.hibernate.jpa.HibernateEntityManager is present in the classpath. Thus auto configuration of beans within HibernateJpaAutoConfiguration will occur only if the above conditions are met.
3. Spring boot`s auto configuration feature is based on @Conditional Annotation.
If you look at SpringBootApplication class in above picture which is used for annotating all spring boot application Main classes, you will notice that it has @EnableAutoConfiguration Annotation (please see arrow mark) which does the work of auto configuration of beans. This is how the all magic of auto configuration is realized by making use of Spring Boot. So in general spring boot provides following features:
3.1 Easy dependency Management
If you look at above picture, you will see a list of dependencies starting with spring-boot-starter-*. Now let’s take a specific example of spring-boot-starter-data-jpa dependency. This extracts all spring data jpa dependencies .so manually we don’t have to state all jpa dependencies like HIbernate etc in Maven xml file. So as a developer we are saved from a lot of time and resources on manually doing dependency management
3.2 Auto Configuration
As seen above there is no need for us to define EntityManagerFactory bean definitions etc. explicitly in spring bean xml file.If you have DB drivers like oracle, Sybase in the class path, it automatically creates a Data Source bean by looking at datasource.properties file in the classpath and connects it with the underlying database such as Sybase, Oracle etc.
3.3 Embedded Web Server support
If you a look at Spring Boot App Maven xml file, spring-boot-starter-web dependency has been included
as well.This Spring-boot-starter-tomcat in turn adds spring-boot-starter-tomcat and then once SpringApplication.run method is executed from main class, it initiates an embedded tomcat container on which our spring boot application is finally deployed.
4. Why to use spring boot in creation of a Micro Service?
As we have already seen that spring boot through its dependency management and auto configuration eases and simplifies the application development process. Now we see how exactly spring boot helps in creation of a micro service. To understand it better, let’s first try to get exact definition of Micro Services from Wikipedia.
In computing, microservices are small, independent processes that communicate with each other to form complex applications which utilize language-agnostic APIs.These services are small building blocks, highly decoupled and focused on doing a small task, facilitating a modular approach to system-building.The microservices architectural style is becoming the standard for building continuously deployed systems.
Now if we try to understand the above lines, it states that micro services are tiniest business processes which communicate with each other to form complex business applications.As we know business processes are highly agile and they keep on changing as well as growing as the complexity of a business grows.So ideally to tackle this complexity there should be a technology provision wherein changes as well new processes additions in the business should be integrated seamlessly with very less downtime and what better than using Spring Boot for creating Micro Services as we have already seen its merits above.
In this article we have seen advantages of using Spring Boot for creating Micro Services. In next part of this series, we will take a look at other components which are required along with Spring Boot Framework in order to create a robust micro services architecture.