September 20th, 2022
SpringBoot Tutorial – Bootstrap a Simple Application
1. Overview

Spring Boot is an opinionated addition to the Spring platform, focused on convention over configuration — highly useful for getting started with minimum effort and creating standalone, production-grade applications.

This tutorial is a starting point for Boot, in other words, a way to get started in a simple manner with a basic web application. We'll go over some core configuration, a front-end, quick data manipulation, and exception handling.

2. Setup
				
					<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.2</version>
    <relativePath />
</parent>
				
			

The initial dependencies are going to be quite simple:

				
					<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>
				
			
3. Application Configuration

Next, we'll configure a simple main class for our application:

				
					@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
				
			

Notice how we're using @SpringBootApplication as our primary application configuration class. Behind the scenes, that's equivalent to @Configuration, @EnableAutoConfiguration, and @ComponentScan together.

Finally, we'll define a simple application.properties file, which for now only has one property:

				
					server.port=8081
				
			
4. Simple Persistence

Let's start by defining our data model, a simple Book entity:

				
					@Entity
public class Book {
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(nullable = false, unique = true)
    private String title;

    @Column(nullable = false)
    private String author;
}
				
			

and its repository, making good use of Spring Data here:

				
					public interface BookRepository extends CrudRepository<Book, Long> {
    List<Book> findByTitle(String title);
}
				
			

Finally, we need to of course configure our new persistence layer:

				
					@EnableJpaRepositories("com.baeldung.persistence.repo") 
@EntityScan("com.baeldung.persistence.model")
@SpringBootApplication 
public class Application {
   ...
}
				
			

Note that we're using the following:

  • @EnableJpaRepositories to scan the specified package for repositories
  • @EntityScan to pick up our JPA entities

To keep things simple, we're using an H2 in-memory database here. This is so that we don't have any external dependencies when we run the project.

Once we include H2 dependency, Spring Boot auto-detects it and sets up our persistence with no need for extra configuration, other than the data source properties:

				
					spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:bootapp;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=
				
			

Leave a Reply

Your email address will not be published. Required fields are marked *