0% found this document useful (0 votes)
729 views

Spring Boot Cheatsheet

This document provides a cheat sheet for essential Spring Boot concepts. It outlines how to start a Spring Boot application using the Spring Initializr and setting properties. It describes testing with JUnit and annotations like @SpringBootTest. It also covers logging configuration and production features like health checks and metrics exposed by Spring Boot Actuator.

Uploaded by

Robin Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
729 views

Spring Boot Cheatsheet

This document provides a cheat sheet for essential Spring Boot concepts. It outlines how to start a Spring Boot application using the Spring Initializr and setting properties. It describes testing with JUnit and annotations like @SpringBootTest. It also covers logging configuration and production features like health checks and metrics exposed by Spring Boot Actuator.

Uploaded by

Robin Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Sold to

Spring Boot CheatSheet


[email protected]

Essentials

Start Setting Properties

Use the Spring Initializr to bootstrap. Set properties with -D on commandline or in

application.properties
application.yml

Configuration Test

Start application with Uses JUnit, includes Mockito, Hamcrest and more

@SpringBootApplication @RunWith(SpringRunner.class) : Enabled Spring Test features in JUnit

public class CommentStoreApp {


public static void main(String[] args) @SpringBootTest : Mark as a Spring Boot test; will start full server by default

throws Exception {
SpringApplication.run(CommentStoreApp.class, args); @DataJpaTest : Marks as a Spring Data JPA test and only auto-configures that part

}
}
@WebMvcTest : Marks as a Spring MVC test and only auto-configures that part

@SpringBootApplication is a shortcut annotation for


@MockBean : On TYPE adds Mocks to Context, On FIELD creates and injects Mock for

use in test

@Configuration : Marks class as a config class

@EnableAutoConfiguration : Configure application based on dependencies

@ComponentScan : Scans classpath for Spring Components and auto registers them

Logging Production Features

By default, uses SLF4J with Logback Enable by adding:

private static final Logger LOGGER = <dependency>


LoggerFactory.getLogger(ReadController.class) <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
logging.file: names the logfile, relative or absolute </dependency>

logging.path : if file is not absolute, path where the logfile is stored Endpoints available under / or /application (Spring Boot 2) Change path with
management.context-path
logging.level.root
property
: set log level for application; replace root with your package to fine

grained control /health: shows a health status

/metrics: metrics of your application

/info: put app and build information here

codeboje.de

You might also like