Pages

Spring Boot

Spring Boot
Spring Boot

Spring Boot is a Spring module that provides the RAD (Rapid Application Development) feature to the Spring framework. Spring Boot provides an easier and faster way to set up, configure, and run both Java Based and Web Based Applications. Spring Boot is an opinionated, easy to get-started addition to the Spring platform – highly useful for creating stand-alone, production-grade applications with minimum effort.

Difference Between Spring and Spring Boot
Spring Spring Boot
Spring Framework is a widely used Java EE framework for building applications. Spring Boot Framework is widely used to develop REST APIs.
Spring aims to simplify Java EE development that makes developers more productive. Spring Boot aims to shorten the code length and provide the easiest way to develop Web Applications.
The primary feature of the Spring Framework is dependency injection. The primary feature of Spring Boot is Autoconfiguration. Spring Boot automatically configures the classes based on the requirement.
Spring helps to make things simpler by allowing us to develop loosely coupled applications. Spring Boot helps to create a stand-alone application with less configuration.
The developer writes a lot of code (boilerplate code) to do the minimal task. Spring Boot reduces boilerplate code.
To test the Spring project, we need to set up the sever explicitly. Spring Boot offers embedded server such as Jetty and Tomcat, etc.
Spring does not provide support for an in-memory database. Spring Boot offers several plugins for working with an embedded and in-memory database such as H2.
Developers manually define dependencies for the Spring project in pom.xml. Spring Boot comes with the concept of starter in pom.xml file that internally takes care of downloading the dependencies JARs based on Spring Boot Requirement.

Difference Between Spring MVC and Spring Boot
Spring MVC Spring Boot
Spring MVC is a model view controller-based web framework under the Spring framework. Spring Boot is a module of Spring for packaging the Spring-based application with sensible defaults.
Spring MVC provides ready to use features for building a web application. Spring Boot provides default configurations to build Spring-powered framework.
Spring MVC requires build configuration manually. There is no need to build configuration manually.
A Deployment descriptor is required. There is no requirement for a deployment descriptor.
Spring MVC specifies each dependency separately. Spring Boot avoids boilerplate code and wraps dependencies together in a single unit.
Spring MVC takes more time to achieve the same. Spring Boot reduces development time and increases productivity.

Spring Boot Annotation
Annotation Description
SpringBootApplication We can mark the main class of a Spring Boot application.
EnableAutoConfiguration Spring Boot looks for auto-configuration beans on its classpath and automatically applies.
ConditionalOnProperty We can make conditions on the values of properties.
ConditionalOnBean We want to define conditions based on the presence or absence of a specific bean.
ConditionalOnMissingBean We want to define conditions based on the presence or absence of a specific bean.
ConditionalOnResource We can make Spring to use a definition only when a specific resource is present.
ConditionalExpression Spring will use the marked definition when the SpEL expression is evaluated to true.
ConditionalOnWebApplication We can create conditions based on if the current application is a web application
ConditionalOnNotWebApplication We can create conditions based on if the current application isn't a web application
Conditional We can create a class evaluating the custom condition. We tell Spring to use this custom condition with @Conditional
ConditionalOnClass Spring will only use the marked auto-configuration bean if the class in the annotation's argument is present/absent
ConditionalOnMissingClass Spring will only use the marked auto-configuration bean if the class in the annotation's argument is present/absent
GetMapping GetMapping Annotation is used for mapping HTTP GET requests onto specific handler methods. @GetMapping is a composed Annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET)
PostMapping PostMapping Annotation is used for mapping HTTP POST requests onto specific handler methods. @PostMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.POST)
PutMapping PutMapping Annotation is used for mapping HTTP PUT requests onto specific handler methods. @PutMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.PUT)
PatchMapping PatchMapping Annotation is used for mapping HTTP PUT requests onto specific handler methods. @PutMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.PUT)
DeleteMapping DeleteMapping Annotation is used for mapping HTTP PATCH requests onto specific handler methods. @PatchMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.PATCH)

Spring Boot Starters

Spring Boot provides a number of starters that allow us to add jars in the classpath. Spring Boot built-in starters make development easier and rapid. Spring Boot Starters are the dependency descriptors.

Starters Name Description
spring-boot-starter-thymeleaf It is used to build MVC web applications using Thymeleaf views.
spring-boot-starter-data-couchbase It is used for the Couchbase document-oriented database and Spring Data Couchbase.
spring-boot-starter-artemis It is used for JMS messaging using Apache Artemis.
spring-boot-starter-web-services It is used for Spring Web Services.
spring-boot-starter-mail It is used to support Java Mail and Spring Framework's email sending.
spring-boot-starter-data-redis It is used for Redis key-value data store with Spring Data Redis and the Jedis client.
spring-boot-starter-web It is used for building the web application, including RESTful applications using Spring MVC. It uses Tomcat as the default embedded container.
spring-boot-starter-data-gemfire It is used to GemFire distributed data store and Spring Data GemFire.
spring-boot-starter-activemq It is used in JMS messaging using Apache ActiveMQ.
spring-boot-starter-data-elasticsearch It is used in Elasticsearch search and analytics engine and Spring Data Elasticsearch.
spring-boot-starter-integration It is used for Spring Integration.
spring-boot-starter-test It is used to test Spring Boot applications with libraries, including JUnit, Hamcrest, and Mockito.
spring-boot-starter-jdbc It is used for JDBC with the Tomcat JDBC connection pool.
spring-boot-starter-mobile It is used for building web applications using Spring Mobile.
spring-boot-starter-validation It is used for Java Bean Validation with Hibernate Validator.
spring-boot-starter-hateoas It is used to build a hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS.
spring-boot-starter-jersey It is used to build RESTful web applications using JAX-RS and Jersey. An alternative to spring-boot-starter-web.
spring-boot-starter-data-neo4j It is used for the Neo4j graph database and Spring Data Neo4j.
spring-boot-starter-data-ldap It is used for Spring Data LDAP.
spring-boot-starter-websocket It is used for building the WebSocket applications. It uses Spring Framework's WebSocket support.
spring-boot-starter-aop It is used for aspect-oriented programming with Spring AOP and AspectJ.
spring-boot-starter-amqp It is used for Spring AMQP and Rabbit MQ.
spring-boot-starter-data-cassandra It is used for Cassandra distributed database and Spring Data Cassandra.
spring-boot-starter-social-facebook It is used for Spring Social Facebook.
spring-boot-starter-jta-atomikos It is used for JTA transactions using Atomikos.
spring-boot-starter-security It is used for Spring Security.
spring-boot-starter-mustache It is used for building MVC web applications using Mustache views.
spring-boot-starter-data-jpa It is used for Spring Data JPA with Hibernate.
spring-boot-starter It is used for core starter, including auto-configuration support, logging, and YAML.
spring-boot-starter-groovy-templates It is used for building MVC web applications using Groovy Template views.
spring-boot-starter-freemarker It is used for building MVC web applications using FreeMarker views.
spring-boot-starter-batch It is used for Spring Batch.
spring-boot-starter-social-linkedin It is used for Spring Social LinkedIn.
spring-boot-starter-cache It is used for Spring Framework's caching support.
spring-boot-starter-data-solr It is used for the Apache Solr search platform with Spring Data Solr.
spring-boot-starter-data-mongodb It is used for MongoDB document-oriented database and Spring Data MongoDB.
spring-boot-starter-jooq It is used for jOOQ to access SQL databases. An alternative to spring-boot-starter-data-jpa or spring-boot-starter-jdbc.
spring-boot-starter-jta-narayana It is used for Spring Boot Narayana JTA Starter.
spring-boot-starter-logging It is used for logging using Logback. Default logging starter.
spring-boot-starter-jta-bitronix It is used for JTA transactions using Bitronix.
spring-boot-starter-social-twitter It is used for Spring Social Twitter.
spring-boot-starter-data-rest It is used for exposing Spring Data repositories over REST using Spring Data REST.
spring-boot-starter-actuator It is used for Spring Boot's Actuator that provides production-ready features to help you monitor and manage your application.
spring-boot-starter-cloud-connectors It is used for Spring Cloud Connectors that simplifies connecting to services in cloud platforms like Cloud Foundry and Heroku.
spring-boot-starter-remote-shell It is used for the CRaSH remote shell to monitor and manage your application over SSH. Deprecated since 1.5.
spring-boot-starter-undertow It is used for Undertow as the embedded servlet container. An alternative to spring-boot-starter-tomcat.
spring-boot-starter-jetty It is used for Jetty as the embedded servlet container. An alternative to spring-boot-starter-tomcat.
spring-boot-starter-tomcat It is used for Tomcat as the embedded servlet container. Default servlet container starter used by spring-boot-starter-web.
spring-boot-starter-log4j2 It is used for Log4j2 for logging. An alternative to spring-boot-starter-logging.

Creating a Spring Boot Project
Step 1 : Open the Spring Initializr web site : https://start.spring.io
Step 2 : Provide the Group Name. We have provided Group Name com.jst
Step 3 : Provide the Artifact Name. We have provided Artifact Name spring-boot-addition.
Step 4 : Next, Click on the Generate Button.
Step 5 : Open Download Folder then Extract spring-boot-addition.rar zip file.
Step 6 : Open any IDE like Eclipse, STS, IntelliJ, NetBeans etc..
Step 7 : Click File -> Import -> Maven Existing Project -> Next -> Browse -> Select the Project -> Finish .

Run Spring Boot App using Command Line C:\Java\addition-app\target> java -jar addition-app-0.0.1-SNAPSHOT.jar
C:\Java\addition-app\target> java -jar addition-app-0.0.1-SNAPSHOT.jar --server.port=9080
C:\Java\addition-app\target> java -jar addition-app-0.0.1-SNAPSHOT.jar --server.port=9080 --spring.profile.active=dev
C:\Java\addition-app\target> java -jar -Dspring.config.location = C:\Java\addition-app\src/main/resources\application.properties addition-app-0.0.1-SNAPSHOT.jar
C:\Java\addition-app\target> java -jar -Dspring.config.location = C:\Java\addition-app\src\main\resources\application.properties addition-app-0.0.1-SNAPSHOT.jar