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