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.
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 {
List 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:
Leave a Reply