Spring Boot Example

Spring Boot automates the routine work that developers do while starting a project or during the development of applications. The routine work that spring boot automates includes dependent libraries addition, object decency management, configuration, embedded server setup and executable jars creation.

Spring Boot provides defaults so that you can start developing projects fast without having to manually setup or configure lots of things that you usually do while setting up a project or packaging and running applications.

Spring Boot can be used with any java IDEs with widely used build tools such as maven or gradle.

Spring Boot Example

I’ll show how to use Spring Boot taking a simple example which inserts data into and retrieves it from in-memory database. What makes developing this example easy and fast is that you don’t need to worry about adding dependencies, spring configuration and database configuration as spring boot automatically configures them.

The example uses eclipse with maven. First make sure that you have Java 1.8 or later version and install eclipse. Then create maven project.

Creating Spring Boot Project from Scratch in Eclipse

To create maven project, open eclipse, go to file menu, click new and others and then select maven project.

spring boot maven project

Follow the maven project creation wizard, select quickstart maven archetype and enter group and artifact ids and click finish.

Open the pom.xml file, and then add spring boot parent and jpa starters. For information about spring boot starters, please see Spring Boot starter tutorial.

Since we are going to use H2 in-memory database, add the dependency to it. And also, we need to add spring-boot-maven-plugin, which creates executable package jar or war, to pom.xml.

Create Spring Boot Project Using Spring Initializr

Instead of creating project from scratch and adding spring starters yourself, you can create it using spring initializr and import it to eclipse. This way you don’t need to worry about finding latest version of spring-boot-starter-parent starter and exact names of other starters.

spring boot h2 jpa initializer


Here is the pom xml of our example.

<project xmlns="" xmlns:xsi=""







After spring boot starters have been added to pom, you can update the project by right clicking it and selecting Maven and update project. Now you can see all the dependencies added to the project due to the spring boot starters.

spring boot h2 jpa dependencies

Spring Boot with H2 DB and JPA Example

Now let’s see how spring boot auto configuration works. This example uses H2 in-memory database and JPA to store and retrieve data. We don’t need to do anything to configure H2 database as spring boot provides default configuration. We need to just define entities and repository interfaces.

Spring boot automatically configures the application based on the jar dependencies used in the project. To enable auto configuration and inform spring about main class, we need to annotate our main class with EnableAutoConfiguration annotation. The main method should call run method of SpringApplication in order to start spring and its auto configuration features.


Create entity by annotating the class with Entity annotation and defining primary key and other column fields.

@Table(name = "product")
public class Product {

	private long id;
	private String name;
	private String brand;
	private String cat;
	private double price;

	public Product() {


	public Product(Long id, String name, String brand, String cat, double price) { = id; = name;
		this.brand = brand; = cat;
		this.price = price;

	public long getId() {
		return id;

	public void setId(long id) { = id;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public String getBrand() {
		return brand;

	public void setBrand(String brand) {
		this.brand = brand;

	public String getCat() {
		return cat;

	public void setCat(String cat) { = cat;

	public double getPrice() {
		return price;

	public void setPrice(double price) {
		this.price = price;



Define JpaRepository interface by extending JpaRepository interface and defining query methods. Spring generates implementation for JpaRepository interface.

public interface ProductRepository  extends JpaRepository<Product, Long> {	
	public List<Product> findProductsByCat(String cat);	

Application Main

Since this is a non-web application example, the main class implements CommandLineRunner to run the application which inserts a record to database and queries the database.

public class ProductApp implements CommandLineRunner {
	private ProductRepository productRepository;
	private ProductApp productApp;

	private final Logger log = LoggerFactory.getLogger(this.getClass());

	public static void main(String[] args) {, args);


	public void products() {

	public void saveProducts() {
		Product p = new Product();
		p.setName("Excellent Tennis Shoe Nike");

	public void printProducts(String category) {
		List<Product> prods = productRepository.findProductsByCat(category);"no of products in " + category + " : " + prods.size());
		for (Product p : prods) {;

	public void setProductRepository(ProductRepository productRepository) {
		this.productRepository = productRepository;

	public void run(String... args) throws Exception {

H2 DB Default Configuration Provided By Spring Boot


If you want to change the configuration provided by spring boot, you can do so by adding properties to file which needs to be created under src/amin/resources folder.

Running Spring Boot Example

You can simply run the application in eclipse by clicking Run menu and Run submenu item.

running spring boot example

Creating Executable Jar

To create executable jar using spring boot, first make sure that spring-boot-maven-plugin is added to pom, then create maven run configuration with package as goal and run it. Executable jar will be created in target folder.

creating executable jar in eclipse using maven and spring boot

You can run executable jar by using java -jar your-app-SNAPSHOT.jar command.