CodeGym /Java Course /Module 5. Spring /Development with Spring Boot

Development with Spring Boot

Module 5. Spring
Level 15 , Lesson 1
Available

This section goes into more detail about how to use Spring Boot. It covers topics such as build systems, autoconfiguration, and application execution. We'll also look at some of the best practices for working with Spring Boot. While there's nothing special about Spring Boot (it's just another library you can use), there are a few best practices that can make your development process a little easier.

Build systems

It's highly recommended that you choose one a build system that supports dependency management and that can consume artifacts published in the "Maven Central" repository. We recommend choosing Maven or Gradle. It is possible to make Spring Boot work with other build systems (such as Ant), but they are not particularly well supported.

Dependency Management

Each release of Spring Boot provides a list of supported dependencies. In practice, there is no need to specify the version of any of these dependencies in your build configuration, since Spring Boot will do this for you. If you upgrade Spring Boot itself, then these dependencies will also be upgraded in a consistent manner.

You can still specify a version and override the recommended values for Spring Boot. if you need it.

This carefully curated list contains all the Spring modules that can be used with Spring Boot, as well as a refined list of third-party libraries. This list is available as a standard content pack (spring-boot-dependencies) that can be used with both Maven and Gradle.

Each Spring Boot release is bundled with a base version of the Spring Framework. We strongly recommend that you do not specify its version.

Maven

To learn about using Spring Boot with Maven, please refer to the Spring Boot Plugin for Maven documentation:

Gradle

To learn about using Spring Boot with Gradle, please refer to the documentation for the Spring Boot plugin for Gradle:

Ant

You can build a project in Spring Boot using Apache Ant+Ivy. There is also an "AntLib" module spring-boot-antlib, which is designed to help Ant create executable jar files.

To declare dependencies, a typical file is ivy.xml looks something like the following example:

<ivy-module version="2.0">
    <info organisation="org.springframework.boot" module="spring-boot-sample-ant" />
    <configurations>
        <conf name="compile" description="everything needed to compile this module" />
        <conf name="runtime" extends="compile" description="everything needed to run this module" />
    </configurations>
    <dependencies>
        <dependency org="org.springframework.boot" name="spring-boot-starter"
                    rev="${spring-boot.version}" conf="compile" />
    </dependencies>
</ivy-module>

A typical build.xml looks like this:

<project
        xmlns:ivy="antlib:org.apache.ivy.ant"
        xmlns:spring-boot="antlib:org.springframework.boot.ant"
        name="myapp" default="build">
    <property name="spring-boot.version" value="2.7.5" />
    <target name="resolve" description="--> retrieve dependencies with ivy">
        <ivy:retrieve pattern="lib/[conf]/[artifact]-[type]-[revision].[ext]" />
    </target>
    <target name="classpaths" depends="resolve">
        <path id="compile.classpath">
            <fileset dir="lib/compile" includes="*.jar" />
        </path>
    </target>
    <target name="init" depends="classpaths">
        <mkdir dir="build/classes" />
    </target>
    <target name="compile" depends="init" description="compile">
        <javac srcdir="src/main/java" destdir="build/classes" classpathref="compile.classpath" />
    </target>
    <target name="build" depends="compile">
        <spring-boot:exejar destfile="build/myapp.jar" classes="build/classes">
            <spring-boot:lib>
                <fileset dir="lib/runtime" />
            </spring-boot:lib>
        </spring-boot:exejar>
    </target>
</project>

Starter packages

Starter packages are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies you need, without having to hunt through code examples and copy and paste a bunch of dependency descriptors. For example, if you want to start using Spring and JPA for database access, include the spring-boot-starter-data-jpa dependency in your project.

Starter packages contain many dependencies needed to get your project up and running quickly with a consistent, compatible set of manageable transition dependencies.

What does the name mean?

All official starter packages have the same name: spring-boot-starter-*, where * is a specific type of application. This naming structure is intended to help if you need to find a starter package. Maven integration in many IDEs allows you to search for dependencies by name. For example, if you have the appropriate plugin for Eclipse or Spring Tools installed, you can press ctrl-space in the POM editor and type "spring-boot-starter" to get the full list.

Third-party starter packages should not start with spring-boot, as this name is reserved for official Spring Boot artifacts. Rather, a third-party starter package usually starts with the name of the project. For example, a third party starter project called thirdpartyproject is usually called thirdpartyproject-spring-boot-starter.

The following starter packages for applications, Spring Boot is provided within the org.springframework.boot group:

Table 1. Starter packages for Spring Boot applications
Name Description

spring-boot-starter

Basic starter package, including support for autoconfiguration, logging and YAML

spring-boot-starter-activemq

Starter package for JMS messaging using Apache ActiveMQ

spring-boot-starter-amqp

Starter package for using Spring AMQP and Rabbit MQ

spring-boot-starter-aop

Starter package for aspect-oriented programming using Spring AOP and AspectJ

spring-boot-starter-artemis

Starter package for JMS messaging using Apache Artemis

spring-boot-starter-batch

Starter package for using Spring Batch

spring-boot-starter-cache

Starter package for using caching support in the Spring Framework

spring-boot-starter-data-cassandra

Starter package for using the distributed database Cassandra and Spring Data Cassandra

spring-boot-starter-data-cassandra-reactive

Starter package for using a distributed database of Cassandra and Spring Data Cassandra Reactive

spring-boot-starter-data-couchbase

Starter package for using the document-oriented database Couchbase and Spring Data Couchbase

spring -boot-starter-data-couchbase-reactive

Starter package for using Couchbase document-oriented database and Spring Data Couchbase Reactive

spring-boot-starter-data-elasticsearch

Starter package for using the search and analytical engine Elasticsearch and Spring Data Elasticsearch

spring-boot-starter-data-jdbc

Starter package to use Spring Data JDBC

spring -boot-starter-data-jpa

Starter package for using Spring Data JPA with Hibernate

spring-boot-starter-data-ldap

Starter package for using Spring Data LDAP

spring-boot-starter-data-mongodb

Starter package for using MongoDB document-oriented database and Spring Data MongoDB

spring-boot-starter- data-mongodb-reactive

Starter package for using MongoDB document-oriented database and Spring Data MongoDB Reactive

spring-boot-starter-data-neo4j

Starter package for using the Neo4j graph database and Spring Data Neo4j

spring-boot-starter-data-r2dbc

Starter package for using Spring Data R2DBC

spring-boot-starter-data -redis

Starter package for using the Redis key-value data store via Spring Data Redis and the Lettuce client

spring-boot-starter-data-redis-reactive

Starter package for using Redis key-value data store via Spring Data Redis reactive store and Lettuce client

spring-boot-starter-data-rest

Starter package for opening Spring Data repositories on top of REST using Spring Data REST

spring-boot-starter-freemarker

Starter package for building MVC web applications using FreeMarker views

spring-boot-starter-graphql

Starter package for creating GraphQL applications using Spring GraphQL

spring-boot-starter-groovy-templates

Starter package for creating a web MVC Applications Using Views Groovy Templates

spring -boot-starter-hateoas

Starter package for building a RESTful hypermedia-based web application via Spring MVC and Spring HATEOAS

spring-boot-starter-integration

Starter package for using Spring Integration

spring-boot-starter-jdbc

Starter package for using JDBC with HikariCP connection pool

spring-boot-starter-jersey

Starter package for creating RESTful web applications using JAX-RS and Jersey.

spring-boot-starter-jooq

Starter package for using jOOQ to provide access to databases SQL data using JDBC.

spring-boot-starter-json

Starter package for reading and writing json

spring-boot-starter-jta-atomikos

Starter package for JTA- transactions using Atomikos

spring-boot- starter-mail

A starter package for using Java Mail and the Spring Framework email support facilities

spring-boot-starter-mustache

Starter package for building web applications using Mustache views

spring-boot-starter-oauth2-client

Starter package for using Spring Security OAuth2/OpenID Connect client features

spring-boot-starter-oauth2 -resource-server

Starter package for using OAuth2 resource server features in Spring Security

spring-boot-starter-quartz

Starter package for using the Quartz scheduler

spring-boot-starter-rsocket

Starter package for creating RSocket clients and servers

spring-boot-starter-security

Starter package to use Spring Security

spring-boot- starter-test

A starter package for testing Spring Boot applications using libraries including JUnit Jupiter, Hamcrest and Mockito

spring-boot-starter-thymeleaf

Starter package for building MVC web applications using Thymeleaf views

spring-boot-starter-validation

Starter package for using Java Bean Validation with Hibernate Validator

spring-boot-starter-web

Starter package for creating web applications, including RESTful, using Spring MVC. Uses Tomcat as default embedded container

spring-boot-starter-web-services

Starter package for using Spring Web Services

spring-boot-starter-webflux

Starter package for building WebFlux applications using Reactive Web support in the Spring Framework

spring-boot-starter-websocket

Starter package for creating WebSocket applications using WebSocket support in the Spring Framework< /p>

In addition to application starter packs, the following starter packs can be used to add production-ready functionality:

Table 2. Spring Boot production starter packages
Name Description

spring-boot-starter-actuator

A starter package for using Actuator from Spring Boot, which provides features for production deployment , which help you monitor and manage your application

Finally, Spring Boot also contains the following starter packages that you can use if you want to exclude or swap certain technical aspects:

Table 3. Spring Boot technical starter packages
Name Description

spring-boot-starter-jetty

Starter package for using Jetty in as an embedded servlet container.

spring-boot -starter-log4j2

Starter package for using Log4j2 for logging purposes.

spring-boot-starter-logging

Starter package for logging using Logback. Default logging starter package

spring-boot-starter-reactor-netty

A starter package for using Reactor Netty as a built-in reactive HTTP server.

spring-boot-starter-tomcat

Starter package for using Tomcat as an embedded servlet container. The default starter package for the servlet container used by spring-boot-starter-web

spring-boot-starter-undertow

Starter package for using Undertow as an embedded servlet container .

For a list of additional community-contributed starter packs, see README file in the spring-boot-starters module on GitHub.

Code Structuring

Spring Boot does not require any specific code structure to work. However, there are some best practices that are useful.

Using the "default" package

If a class does not contain a package declaration, it is considered to be in " package by default." Using the "default package" is generally not recommended and should be avoided. This may cause specific problems for Spring Boot applications that use the @ComponentScan, @ConfigurationPropertiesScan, @EntityScan or @SpringBootApplication as each class is read from each jar file.

We recommend following the recommended Java package naming conventions and using the reverse domain name ( for example, com.example.project).

Placing the main application class

It is generally recommended to place the main application class in the root package above other classes. The @SpringBootApplication annotation is often placed in the main class, and it implicitly defines a base "search package" for certain elements. For example, if you are writing a JPA application, the class package marked with the @SpringBootApplication annotation is used to find elements with the @Entity annotation. Using a root package also allows you to apply component scanning exclusively to your project.

If you don't want to use the @SpringBootApplication annotation The @EnableAutoConfiguration and @ComponentScan that the package imports define the same logic, so you can use them instead.

The list below shows typical structure:

com
 +- example
     +- myapplication
         +- MyApplication.java
         |
         +- customer
         |   +- Customer.java
         |   +- CustomerController.java
         |   +- CustomerService.java
         |   +- CustomerRepository.java
         |
         +- order
             +- Order.java
             +- OrderController.java
             +- OrderService.java
             +- OrderRepository.java

In the file MyApplication.java the main method together with the base @SpringBootApplication will be declared as follows:

Java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
Kotlin

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class MyApplication
fun main(args: Array<String>) {
    runApplication<MyApplication>(*args)
}
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION