Show Menu

Spring Framework 4 Cheat Sheet by

Common usages of Spring Framework annotations and features
java     spring     springboot     springframework

Modules

Core
spring-core
spring-beans
spring-context
spring-expression
AOP and Instrumentation
spring-aop
spring-aspects
spring-instrument
spring-instrument-tomcat
Messaging
spring­-me­ssaging
Data Access­/In­teg­ration
spring-jdbc
spring-tx
spring-orm
spring-oxm
spring-jms
Web
spring-web
spring-webmvc
spring-webmvc-portlet
spring-websocket
Test
spring­-test

Spring MVC - Controller

@Controller
Annotation to indicate that the class is a controller class.
@RestController
A conven­ience annotation that is itself annotated with @Cont­roller and @Resp­ons­eBody. Used in contro­llers that will behave as RESTful resour­ces.
@RequestMapping
Annotation to be used on methods in @Rest­Con­tro­ller classes. You can provide an URI to be served as RESTful service.
@ModelAttribute
Annotation used to bind values present in views.

Config­uration

@Configuration
Annotation used to provide conf­igu­rat­ions.
@Bean
Annotation that acts like a prov­ider where you can define how the bean is inst­ant­iated when a inje­ction of that type is requested. Instances of @Bean annotated methods will act as sing­let­ons.

Properties Evaluation Sequence

Command-line arguments
java -Dproj­ect.na­me=Test -jar app.jar
System properties
Syste­m.g­etP­rop­ert­ies()
Enviro­nment Variable
export PROJEC­T_N­AME­=Test
External proper­tie­s/yml file
proje­ct.n­am­e=Test
Internal proper­tie­s/yml file
proje­ct.n­am­e=Test
The default proper­tie­s/yml files are appl­ica­tio­n.p­rop­ert­ies and appl­ica­tio­n.yml and they are located in /src/­res­our­ces.

Spring Boot Initia­lizer

http://start.spring.io
Web service that allows the user to specify the project metadata and depend­encies as well as download the initial structure.
Spring CLI
A CLI tool that interacts with http­://­sta­rt.s­pr­ing.io service to scaffold a new project.
Spring Tool Suit
Eclips­e-based IDE that also interacts with http­://­sta­rt.s­pr­ing.io to scaffold a new project.
Intellij IDEA
Intellij also provides a way of creating a new project via http­://­sta­rt.s­pr­ing.io.

Spring Boot - Auto Config­uration

@ConditionalOnClass
@Cond­iti­ona­lOn­Cla­ss(­Tom­cat.cl­ass)
Only available if the Tomcat class is found in the classpath.
@ConditionalOnProperty
@Cond­iti­ona­lOn­Pro­per­ty(name = "­tom­cat.ve­rsi­on", matchI­fMi­ssing = true)
Only available if the property tomca­t.v­ersion is set to true.
Auto config­uration is just the combin­ation of @Conf­igu­ration and @Cond­iti­onal* annota­tions in order to correctly register beans.
 

Archit­ecture

Dependency Injection

@Resource
Annotation used to inject an object that is already in the Appl­ication Context. It searches the instance by name. It also works on setter methods.
@Autowired
Annotation used to inject objects in many possible ways, such as: instance variable, constr­uctor and methods. It does not rely on name as @Reso­urce, so, for multiple concrete implem­ent­ations, the @Qual­ifier annotation must be used with it.
@Qualifier
Annotation used to dist­ing­uish between mult­iple concrete implem­ent­ations. Used alongside with @Auto­wired annotation that does not rely on name.
@Primary
Annotation used when no name is provided telling Spring to inject an object of the annotated class first. Used along with @Comp­onent.
@Component
Generic stereotype annotation used to tell Spring to create an instance of the object in the Appl­ication Context. It's possible to define any name for the instance, the default is the class name as camel case.
@Contr­oller
Stereotype annotation for presen­tation layer.
@Repos­itory
Stereotype annotation for persis­tence layer.
@Service
Stereotype annotation for service layer.

Profile

spring.profiles.active
Property to be set in appl­ica­tio­n.p­rop­ert­ies in order to tell Spring what profiles are active.
@Profile("!dev")
Annotation used to define which profile can execute the annotated method.

Spring Boot - Basics

@SpringBootApplication
Initial annotation that comprises the following annota­tions: @Spri­ngB­oot­Con­fig­ura­tion, @Enab­leA­uto­Con­fig­ura­tion and @Comp­one­ntS­can.
@Spri­­ngB­­oo­t­C­on­­fig­­ur­a­tion
Indicates that a class provides Spring Boot applic­ation @Conf­igu­rat­ion.
@Enab­­leA­­ut­o­C­on­­fig­­ur­a­tion
Enable auto-c­onf­igu­ration of the Spring Applic­ation Context, attempting to guess and configure beans that you are likely to need.
@Comp­­one­­nt­S­can
Configures component scanning directives for use with @Conf­igu­ration classes.
Most of the time you will need only to declare the @Spri­ngB­oot­App­lic­ation annota­tion.

Spring Boot - Example

Download the Spring Framework 4 Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/danielfc_spring-framework-4.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

mohanreddy.exp mohanreddy.exp, 06:29 10 Aug 18

not able to download pdf.

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Selenium WebDriver Cheat Sheet Cheat Sheet
          Eclipse Cheat Sheet