What are the Features of spring boot for web development

Spring Boot just takes away all these pains and let you write the code which matters i.e. application code. All of the Spring Boot features which I mentioned e.g. auto-configuration, Starter POMs or Starter dependency and Spring Boot CLI aims to simplify Java development with Spring.

Now, let's go into a little bit more details on each of these features
1. AutoConfiguration
You might have worked with Spring-based Java web application which connects to a relational database e.g. an in-memory database like H2 and if yes then you might know that you need to declare JdbcTemplate as a bean and also need to configure a DataSource which is a dependency for JdbcTempalte.

In a modern-day Spring application, which uses Java-based configuration, you need to add the following two methods into your Configuration class:
@Bean
public JdbcTemplate jdbcTempalte(DateSource ds){
   return new JdbcTempalte(ds);
}

@Bean
public DataSource dataSource(){
  return new EmbeddedDatabaseBuilder()
     .setType(EmbeddedDatabaseType.H2)
     .addScripts('ddl.sql', 'data.sql')
     .build();
}

This is not really a complex for someone who has done Spring development but if you are starting afresh then it can take hours and days to figure out this.

But, more importantly, this is a piece of code which many of us have written irrespective of our application. I mean, this code is not unique and every single Spring application which works with JDBC will need it.

That's where Spring Boot AutoConfiguration comes into the picture. It detects the presence of certain Class in the Classpath and then automatically configure it for you.


For example, if you have added JdbcTempalte into your classpath and also H2.jar then Spring Boot can automatically configure an in-memory database for you and a JdbcTempatle which is ready to use. You don't need to write above code to use JdbcTemplate in your DAO layer.

This is just an example. Spring Boot auto-configuration makes more than 200+ such decision and automatically configure many functionalities by examining JAR dependencies. For example, if spring-mvc.jar is present then it can automatically configure DispatcherServlet, InternalViewResolver etc.

If JPA and Hibernate are present then it can configure that as well and if you have spring-security.jar then it can even configure basic security to protect your application.

Btw, when it comes to relying on auto-configuration, as in-depth knowledge is required to properly protect your application. If you are interested in Spring Security, check Spring Security MasterClass by Eugen Paraschiv.

The Auto-Configuration feature is by default disabled and you need to enable it by using @EnableAutoConfiguration or @SpringBootApplication annotation on your Configuration class. I normally annotated the Main class, which I am going to run with an embedded Tomcat server.

It's recommended to use @SpringBootApplication annotation from Spring Boot 1.2 onwards as it combines a couple of other annotations to make your code more readable. See Learn Spring Boot - Rapid Spring Application Development to learn more about Spring Boot annotations.

In short, The auto-configuration feature of Spring Boot saves a lot of work and reduce the development time and I strongly recommend to use auto-configuration whenever you use Spring Boot.



2. Starter POMs
While AutoConfiguration takes away the pain of configuring common functionalities, the Starter POMs take away pain by finding and adding common dependencies in your project.

In order to build a simple Spring MVC based REST application which supports Jackson and to run it an embedded container, you would at least need following dependencies e.g.

spring-core.jar
spring-web.jar
spring-webmvc.jar
jackson-databind.jar
tomcat-embed-core.jar
tomcat-embed-el.jar
tomcat-embed-logging-juil.jar

By using Spring Boot Starter POMs or starter dependency feature, you can get all of these by just adding spring-boot-starter-web dependency in your pom.xml

So, instead of adding all these dependencies and worrying about their compatible version, you just need to add one. You will also be more confident that tried and tested versions of libraries are used and there won't be any incompatibility issue in future.

Another subtle benefit of starter POMs feature is that you don't need to remember or search dependencies. If you are building web application you can add a 'web' starter, if you are building JPA application you could add 'jpa' starter, by aggregating common dependencies by functionalities Spring Boot has made them easy to remember and use.

Btw, if you are wondering how Starter POMs feature works internally then let me tell you all the magic comes from Maven or Gradle's transitive dependency feature.

 It's Maven or Gradle which pulls the right version of libraries, Starter POMs just declare them. If you want to learn more, I suggest you check out Dan Vega's Rapid Application development with Spring Boot course.

In short, Starter POMs or starter dependency is another awesome feature of Spring Boot which really helps to simplify Spring application development. It's like a close cousin of auto-configuration and you will frequently use them together.



3. Spring Boot CLI
In the first paragraph of this article, I said that it's now possible to create a Java web application which can fit in a tweet and it happens because of Groovy and Spring Boot CLI.

The Spring Boot CLI is a command line interface provided by Spring Boot framework which allows you to create Spring based web application using Groovy programming language.

Actually, Groovy and Spring Boot nicely complement each other, Groovy aims to make Java development simpler while Spring Boot aims to make Spring application development simpler and both benefit from each other's simplicity.

While auto-configuration and starter dependencies are an integral feature of Spring Boot, Spring CLI is an optional one, you also need to install Spring CLI in order to use it.

Here is a simple HelloWorld RESTful Web Service in Groovy and Spring Boot CLI and it works you can just run even without compiling as shown below:
@RestController
class HelloSpringBootController{

  @RequestMapping("/")
  def hello() {
    return "Hello Spring Boot CLI"
   }
}

That's it, you can run it on an embedded container which comes with Spring Boot CLI, no web.xml, no configuration, and no server setup.

If you are wondering how these whole things work i.e. how does Groovy knows about @RestController and @RequestMapping annotations then let me tell you that Spring Boot CLI leverages auto-configuration and starter POMs feature to let you focus on only writing application code?

Spring Boot CLI detect that @RestController and @RequestMapping are in use and it knows which starter dependencies are required to add into classpath to make it work.

Once it downloads those series of dependencies, auto-configuration automatically kicks-in and configured it for use e.g. once spring-boot-web-starter comes into the picture it downloads spring-mvc.jar and then auto-configuration automatically configure DispatcherServlet and enable Spring MVC.

This whole thing looks like magic but it's a reality. If you like this model of development I suggest you go through Spring Boot in Action by Craig Walls to learn Spring CLI in depth. Craig has covered this topic really nicely.

Related blog:

[Java training in chennai](http://candidjava.com/training/advanced-java-training-in-chennai/)