Spring boot cheatsheet

Annotations

Spring Boot and the rest of the Spring framework can be effectively configured using Annotations, starting from Java5. Below is a list of commonly used and/or powerful annotations to get up and running with Spring boot.

Spring Framework Annotations

Spring uses the below annotations to create and inject beans into the application context. These annotations are available across all types of Spring boot applications and components.

ANNOTATION DESCRIPTION

LEVEL

C
L
A
S
S
F
I
E
L
D
C
O
N
S
T
R
U
C
T
O
R
M
E
T
H
O
D
P
A
R
A
M
E
T
E
R
@Autowired Annotation @Autowired is used to inject object dependency implicitly for a constructor, field or method. This is known as “autowired by type” since the object to be injected is discovered by its type. The items declared @Autowired need not have to be public.
@Configurable Used on classes to inject properties of domain objects. Types whose properties are injected without being instantiated by Spring can be declared with @Configurable annotation.
@Qualifier It can be used to create more than one bean of the same type and wire only one of the types with a property. It provides greater control on the dependency injection process and can be used with @Autowired annotation.
@Required Used to mark class members that are mandatory. The Spring auto-configuration fails if a particular property specified with this annotation cannot be injected.
@ComponentScan Make Spring scan the package for the @Configuration clases.
@Configuration It is used on classes that define beans.
@Bean It indicates that a method produces a bean which will be mananged by the Spring container.
@Lazy Makes a @Bean or @Component to be initialized only if it is requested.
@Value It is used to inject values into a bean’s attribute from a property file. @Value annotation indicates a default value expression for the field or parameter.

 

Spring-boot web Annotations

Spring uses the below component stereotypes and MVC related annotations to configure web services and web applications.

ANNOTATION DESCRIPTION LEVEL
C
L
A
S
S
F
I
E
L
D
C
O
N
S
T
R
U
C
T
O
R
M
E
T
H
O
D
P
A
R
A
M
E
T
E
R
@SpringBootApplication This annotation is used to qualify the main class for a Spring Boot project. The class used with this annotation must be present in the base path. @SpringBootApplication scans for sub-packages by doing a component scan.
@EnableAutoConfiguration Based on class path settings, property settings, new beans are added by Spring Boot by using this annotation.
@Controller Allows detection of component classes in the class path automatically and register bean definitions for the classes automatically.
@RestController Used in controllers that will behave as RESTful resources. @RestController is a convenience annotation that combines @Controller and @ResponseBody.
@ResponseBody Makes Spring to convert the returned object to a response body. This is useful for classes exposed as RESTful resources.
@RequestMapping Used to map web requests to specific handler classes and methods, based on the URI.
@RequestParam This annotation is used to bind request parameters to a method parameter in your controller.
@PathVariable This annotations binds the placeholder from the URI to the method parameter and can be used when the URI is dynamically created or the value of the URI itself acts as a parameter.

Spring-boot Testing Annotations

Spring uses the below annotations to create and test Spring applications.

ANNOTATION DESCRIPTION LEVEL
C
L
A
S
S
F
I
E
L
D
C
O
N
S
T
R
U
C
T
O
R
M
E
T
H
O
D
P
A
R
A
M
E
T
E
R
@AfterTransaction Annotation used to identify which method needs to be invoked after a transaction is completed.
@BeforeTransaction Used to identify the method to be invoked before a transaction starts executing.
@ContextConfiguration Declares the annotated classes which will be used to load the context for the test. The location of the configuration file has to be povided to Spring.
@DirtiesContext This annotation indicates the test(s) modify or corrupt the SpringApplicationContext and that it should be closed. Hence, context is reloaded before the next test is executed.
@ExpectedException The test method is expected to throw a particular exception, else the test fails.
@WebAppConfiguration Used to create web version of the application context.
@Repeat Specifies the test method to be executed multiple times.
@Transactional Describes transaction attributes on a method or class.
@Rollback Indicates if the transaction of a test method must be rolled back after the execution of the test completed.
@Commit Indicates that the transaction of a test method must be committed after the execution of the test completed.
@Timed Indicates the time limit for the test method. If the test has not completed execution before the time expires, the test fails.
@TestPropertySource Annotation specifies the property sources for the test class.
@Sql Annotation declares a test class/method to run SQL scripts against a database.

Workflow and tools

Below, is an overview of some of the commonly used workflow and tools to develop Spring applications.

CATEGORY TOOL DESCRIPTION AND USAGE
Development Spring Tool Suite (STS) STS is an Eclipse-based development environment that can be used to develop Spring applications easily.
Development Eclipse IDE used to develop Java applications. It has a plugin for develping Spring applications. Useful when working simultaneously on Spring and non-Spring based apps.
Development IntelliJ IDEA Provides a smooth environment

and user experience for developing Spring applications.

Provides a comprehensive view of the project forspeedy navigation, error highlighting, plugins for numerous purposes, code completion.

Unit Testing JUnit JUnit is a regression Testing Framework used to implement unit testing in Java with an enhanced speed and quality.
Unit testing

Mockito

It is an open source mocking framework to create mock classes and interfaces providing realistic tests to predict the behaviour of the application.
Unit testing

JaCoCo

Provides support for code-coverage measurement and generates a detailed test report.
API Testing

JMeter

Apache’s JMeter is an open source testing tool which includes load, functional, regression and performance tests.
API Testing Postman Postman is a powerful tool used to test web services. It provides a feature to create test collections and can be used for API testing.
API Testing REST-Assured Makes API tesing in Java simple by providing behaviour driven development. It can integrate with any existing Java

automation framework.

Monitoring spring-boot-actuators Enables monitoring and managing Spring boot application by shipping production ready featues like health, metrics, HTTP tracing etc. .
Monitoring Micrometer Micrometer is an application-neutral facade or abstraction for reporting application metrics which integrates with many monitoring systems, e.g., Graphite, New Relic and Statsd. Micrometer integrates with the metrics endpoint of spring-boot-actuators.

Spring boot tips and recipes

Below is a short list of articles published in javagists.com about Spring boot applications.

Title Description
RESTFul webservices with Jersey And Spring Create RESTFul webservices with Jersey & Spring. Step-by-step tutorial using STS.
Spring boot Actuator Example – Integration with rest service Learn about Spring boot Actuator and integrate it with an existing web service.
REST – Spring Boot Create REST service using SpringBoot.
Spring Boot Devtools Using Spring boot Devtools to accelerate the development cycle. Leverage Live-Reload and auto-restart for local and remote applications.
REST Service Using Spring Learn how to build a RESTful service with Spring. Step-by-step tutorial with eclipse.
REST API Interview Questions Review top 15 REST API interview questions and answers.

 

3 thoughts on “Spring boot cheatsheet”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.