Spring Vs Spring Boot

What is Spring Boot? What is Spring Framework? What are their goals? How can we compare them? There must be a lot of question in your mind. We will discuss each and every question here. At the end of blog you will get to know about all these questions. There is one interesting fact that you will get to know that they both are not competing each other for specific point. Actually they both solves a different types of problem.

What is Spring? Why we use Spring ? What are the core problem Spring solves?

Spring Framework is one of the most popular application development frameworks for Java. One of the great feature that the spring have is Dependency Injection (DI) or Inversion Of Control (IOC) with the help of which we can develop loosely coupled applications. And loosely coupled applications can be easily unit tested.

Let’s consider a simple example:

Example without Dependency Injection

Consider the example below: MyController depends on MyService for certain task. SO, to get the instance of MyService , we will do like

MyService service = new MyService();.

we have now created the instance for MyService and we see both are tightly coupled. so, If I create an mock for MyService in a unit test for MyController, How do I make MyController use the mock? Its bit difficult. Isn’t it?

@RestController
public class MyController {
    private MyService service = new MyService();

    @RequestMapping("/welcome")
    public String welcome() {
        return service.retrieveWelcomeMessage();
    }
}

Same example with DI

With the help of only two annotations only we can get the instance of MyService soeasily which is not tightly coupled . The Spring Framework do the hard work to make things simpler.

  •  @Component is simply used to say  Spring Framework that this is a bean that you need to manage within your own BeanFactory (An implementation of Factory pattern).
  • @Autowired is simply used to say Spring Framework to find the correct match for this specific type and autowire it in

So, Spring framework will create a bean for MyService and autowire it into MyController.

In a unit test, I can ask the Spring framework to auto-wire the mock ofMyService into MyController.

@Component
public class MyService {
    public String retrieveWelcomeMessage(){
       return "Welcome to InnovationM";
    }
}
@RestController
public class MyController {

    @Autowired
    private MyService service;

    @RequestMapping("/welcome")
    public String welcome() {
        return service.retrieveWelcomeMessage();
    }
}

Spring Framework has many other features, which are divided into twenty modules to solve many such common problems. Few of them are:

  • Spring JDBC
  • Spring MVC
  • Spring AOP
  • Spring ORM
  • Spring JMS
  • Spring Test
  • Spring Expression Language (SpEL)

Aspect Oriented Programming(AOP) is another strong side of Spring Framework. The key unit  in Object Oriented Programming is the class, whereas in AOP the key unit is the aspect. for example : if you want to add the security in your project , logging etc etc , you can just use AOP and keep these cross cutting concern away from your main business logic. You can perform any action after a method call , before a method call, after a method returns , after the exception arises.

Spring Framework does not have its own ORM but it provides a very good integration with ORM like Hibernate , Apache iBATIS etc.

In short we can say that ,Spring Framework provides decoupled way of developing web applications. Web applications development becomes easy with help of these concepts in spring like Dispatcher Servlet, ModelAndView and View Resolver.

Then there must a question arising in your mind , if spring sort out so many problem , then why there is the need of Spring boot?

Now , if you have already worked on Spring , think about the problem that you faced while developing full-fledged spring application with all functionalities.Not able to think? ok let me tell you, There was lot of difficulty to setup hibernate Datasource, Entity Manager, Session Factory, Transaction Management etc? It takes a lot of time for a developer to set up a basic project using spring MVC with minimum functionality.

  <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
  </bean>
  <mvc:resources mapping="/webjars/**" location="/webjars/"/>
<servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/my-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

When we use Hibernate, we have to configure these things like datasource , EntityManager etc

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="${db.driver}" />
        <property name="jdbcUrl" value="${db.url}" />
        <property name="user" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean>
    <jdbc:initialize-database data-source="dataSource">
        <jdbc:script location="classpath:config/schema.sql" />
        <jdbc:script location="classpath:config/data.sql" />
    </jdbc:initialize-database>
    <bean
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        id="entityManagerFactory">
        <property name="persistenceUnitName" value="hsql_pu" />
        <property name="dataSource" ref="dataSource" />
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>

 

How Spring Boot Solves the Above Problem?

  1. Spring boot does all of those using AutoConfiguration and will take care of all the internal dependencies that your application needs what you just need to do is just run your application. Spring boot will auto-configure with Dispatcher Servlet if Spring jar is in class path. It will auto-configue to datasource if hibernate jar is in class path. SpringBoot gives us a pre-configured set of Starter Projects to be added as a dependency in our project.
  2. During web-application development,  we would need the jars we want to use, which versions of jars to use and how to connect them together. All web application have similar needs.  For example- Spring MVC, Jackson Databind , Hibernate core and Log4j (for logging). So, we had to choose the compatible versions of all these jars. In order to decrease the complexity , spring boot has introduced what we call Spring boot Starters

Dependency for Spring Web Project

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>4.2.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.3</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.0.2.Final</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

 

Starters are set of convenient dependencies that you can include in your spring boot application.  For using spring and hibernate we just have to include the spring-boot-starter-data-jpa dependency in the project.

Dependency for Spring Boot Starter Web

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

The following screenshot shows the different packages under a single dependency that are added into our application.

There are other more packages you will see , once you will add that starter dependency. Spring Boot Starter Web comes pre-packaged with these all. As a developer, we would not need to worry about these dependencies and their compatible versions.

Spring Boot Starter Project Options

These are few starter projects help us in quickly getting started with developing specific types of applications.

  • spring-boot-starter-web-services: SOAP Web Services
  • spring-boot-starter-web: Web and RESTful applications
  • spring-boot-starter-test: Unit testing and Integration Testing
  • spring-boot-starter-jdbc: Traditional JDBC
  • spring-boot-starter-hateoas: Add HATEOAS features to your services
  • spring-boot-starter-security: Authentication and Authorization using Spring Security
  • spring-boot-starter-data-jpa: Spring Data JPA with Hibernate
  • spring-boot-starter-cache: Enabling Spring Framework’s caching support
  • spring-boot-starter-data-rest: Expose Simple REST Services using Spring Data REST

Leave a Reply