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.
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.
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.
The following starter packages for applications, Spring Boot is provided within the
org.springframework.boot
group:
Name | Description |
---|---|
Basic starter package, including support for autoconfiguration, logging and YAML |
|
Starter package for JMS messaging using Apache ActiveMQ |
|
Starter package for using Spring AMQP and Rabbit MQ |
|
Starter package for aspect-oriented programming using Spring AOP and AspectJ |
|
Starter package for JMS messaging using Apache Artemis |
|
Starter package for using Spring Batch |
|
Starter package for using caching support in the Spring Framework |
|
Starter package for using the distributed database Cassandra and Spring Data Cassandra |
|
Starter package for using a distributed database of Cassandra and Spring Data Cassandra Reactive |
|
Starter package for using the document-oriented database Couchbase and Spring Data Couchbase |
|
Starter package for using Couchbase document-oriented database and Spring Data Couchbase Reactive |
|
Starter package for using the search and analytical engine Elasticsearch and Spring Data Elasticsearch |
|
Starter package to use Spring Data JDBC |
|
Starter package for using Spring Data JPA with Hibernate |
|
Starter package for using Spring Data LDAP |
|
Starter package for using MongoDB document-oriented database and Spring Data MongoDB |
|
Starter package for using MongoDB document-oriented database and Spring Data MongoDB Reactive |
|
Starter package for using the Neo4j graph database and Spring Data Neo4j |
|
Starter package for using Spring Data R2DBC |
|
Starter package for using the Redis key-value data store via Spring Data Redis and the Lettuce client |
|
Starter package for using Redis key-value data store via Spring Data Redis reactive store and Lettuce client |
|
Starter package for opening Spring Data repositories on top of REST using Spring Data REST |
|
Starter package for building MVC web applications using FreeMarker views |
|
Starter package for creating GraphQL applications using Spring GraphQL |
|
Starter package for creating a web MVC Applications Using Views Groovy Templates |
|
Starter package for building a RESTful hypermedia-based web application via Spring MVC and Spring HATEOAS |
|
Starter package for using Spring Integration |
|
Starter package for using JDBC with HikariCP connection pool |
|
Starter package for creating RESTful web applications using JAX-RS and Jersey. |
|
Starter package for using jOOQ to provide access to databases SQL data using JDBC. |
|
Starter package for reading and writing json |
|
Starter package for JTA- transactions using Atomikos |
|
A starter package for using Java Mail and the Spring Framework email support facilities |
|
Starter package for building web applications using Mustache views |
|
Starter package for using Spring Security OAuth2/OpenID Connect client features |
|
Starter package for using OAuth2 resource server features in Spring Security |
|
Starter package for using the Quartz scheduler |
|
Starter package for creating RSocket clients and servers |
|
Starter package to use Spring Security |
|
A starter package for testing Spring Boot applications using libraries including JUnit Jupiter, Hamcrest and Mockito |
|
Starter package for building MVC web applications using Thymeleaf views |
|
Starter package for using Java Bean Validation with Hibernate Validator |
|
Starter package for creating web applications, including RESTful, using Spring MVC. Uses Tomcat as default embedded container |
|
Starter package for using Spring Web Services |
|
Starter package for building WebFlux applications using Reactive Web support in the Spring Framework |
|
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:
Name | Description |
---|---|
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:
Name | Description |
---|---|
Starter package for using Jetty in as an embedded servlet container. |
|
Starter package for using Log4j2 for logging purposes. |
|
Starter package for logging using Logback. Default logging starter package |
|
A starter package for using Reactor Netty as a built-in reactive HTTP server. |
|
Starter package for using Tomcat as an embedded servlet container. The default starter package for
the servlet container used by |
|
Starter package for using Undertow as an embedded servlet container . |
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.
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.
@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:
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);
}
}
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class MyApplication
fun main(args: Array<String>) {
runApplication<MyApplication>(*args)
}
GO TO FULL VERSION