Monday, 13 February 2017

Spring MVC vs Spring Boot

Spring.

  • Application Framework(This helps us to build Enterprise based java applications)
  • Dependency Injection Framework(where one object supplies dependency of another object :: Setter/Constructor Injections)
  • Manage Life Cycle of Java Classes.(beans lifecycle)
  • Boiler plate configuration-: 'Programmer writes a lot of code for minimal task.'
  • Takes time to have a spring application up and running.
Spring Boot.
  • A suite of pre-configured frameworks and technologies.(that is used to remove boiler plate configuration)
  • The shortest way of up and running spring application.
  • Opinionated
  • Production Ready
  • Stand Alone

Spring Bean LifeCycle

Before looking into Bean Life Cycle let us try to understand IOC-:

A brief introduction to IOC-:

"IOC is a design patter which is used to remove dependency from your code."

For Example-:

Say you application has a text editor component and you want to provide spell checking your standard code will look like this.

public class TextEditor{
        private SpellChecker spellChecker;
        public TextEditor(){
            this.checker = new SpellChecker();
   }
}

What we have done here is create a dependency between the TextEditor and the SpellChecker. In a IOC Scenario we would do something like this

 public class TextEditor{
        private ISpellChecker checker;
        public TextEditor(ISpellChecker checker){
            this.checker = checker;
   }
}

In the first code snippet we are instantiating SpellChecker(this.checker = new SpellChecker();)
which means that TextEditor class directly depends on SpellChecker Class.

In the second one we are creating an abstraction by having the TextEditor constructor signature as the SpellChecker dependency class (not initializing dependency in class) This allow us to call the dependency then pass it to the TextEditor class like so

SpellChecker sc = new SpellChecker(); //dependency
TextEditor text = new TextEditor(sc);

Now the client creating the TextEditor class has the control over which SpellChecker implementation to use. We are injecting the TextEditor with dependency.

===============================================Bean Life Cycle-:

Spring Beans are managed by IOC Containers. Spring bean exist within the container as long as they are needed by the application. There are various life-cycle interfaces and methods that will be called by the IoC Container.

A Spring bean represent a POJO component performing some useful operation. 

List of activity that takes place between the time of Bean Instantiation and hand over of the bean reference to the client application-:

  1. The bean container finds the definition of the Spring Bean in the configuration file.
  2. The bean container creates an instance of the Bean using java reflection API.
  3. After instance creation dependency will be injected(DI)
     "If Bean class implements any of the below highlighted interface then corresponding method will be invoked in below order point"
  1. If the bean class implements the BeanNameAware interface then the setBeanName() method will be called by passing the name of the bean.
  2. If the bean class implements the BeanClassLoaderAware interface then the method setBeanClassLoader() method will be called by passing an instance of the ClassLoader object that loaded this bean.
  3. If the bean class implements the BeanfactoryAware interface then the method setBeanfactory() will be called by passing and instance of a BeanFactory object.
  4. If there are any BeanPostProcessors object associated with the BeanFactory that loaded the bean then the method postProcessBeforeInitialization() will be called even before properties of that bean are called.
  5. If the bean class implements InitializingBean interface then the method afterPropertiesSet() will be called once all the bean defined in the properties files has been set.
  6. If the bean definition in the configuration file contains 'init-method' attribute then the values for the attribute will be resolved to a method name in the bean class and that method will be called.
  7. The postProccessAfterInitilization() method will be called if there are any Bean Post Processors attached for the bean factory object.
  8. If the bean class implements the DisposableBean interface then the method destroy()will be called when the application no longer needs the bean reference.
  9. If the bean definition in the configuration file contains a 'destroy-method' attribute then the corresponding method definition in the bean class will be called.

Basic Overview of Spring Boot

Introducing Spring Boot-:

Spring Boot is a Framework from 'The Spring Team' to ease the bootstrapping and development of new Spring Application.

Spring Boot provides defaults for code and annotation configuration to quick start new Spring projects within no times. It follows 'Opinionated Defaults Configuration' Approach to avoid lot of boilerplate code and configuration to improve Development, Unit testing, and Integration test process.

or in easy way we can say "Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. "


Why Spring Boot?

  • To ease the Java base application Developments, Unit Test and Integration Test process.
  • To reduce Development, Unit Test and Integration test time by providing some defaults
  • To increase productivity
Advantages of Spring Boot-:

  1. Its very easy to develop Spring Based Application using Java or Groovy.
  2. Its reduces lot of development time and increases productivity.
  3. It avoids writing lots of boilerplate code, annotation and XML configuration
  4. Its very easy to integrate with Spring Security, Spring JDBC, Spring ORM, Spring Data.
  5. It follows 'Opinionated Defaults Configuration' approach to reduce developers effort.
  6. It provides embedded HTTP Servers like Tomcat, Jetty. to develop and test our web applications
  7. Its provide BOOT CLI command line interface to develop and test application
  8. It provide lot of easy plugin using MAVEN & GRADLE build tools. 



Accessing Data using Data Repository JPA


Consuming a restful webservice


Building a restful web service


Spring Boot Gradle Project


Spring Boot Maven Project


Using Spring Initializr to create Maven/Gradle Project