Spring Framework 4 Cheat Sheet

A Programming › Java cheat sheet in English by danielfc

Common usages of Spring Framework annotations and features

88,879 views 22,662 downloads 9 Dec 2015 History
4.3 avg · 6 ratings

Modules

Core
spring­-core
spring­-beans
spring­-co­ntext
spring­-ex­pre­ssion
AOP and Instru­men­tation
spring-aop
spring-aspects
spring­-in­str­ument
spring­-in­str­ume­nt-­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

@Contr­oller
Annotation to indicate that the class is a controller class.
@RestC­ont­roller
A conven­ience annotation that is itself annotated with @Contr­oller and @Respo­nseBody. Used in contro­llers that will behave as RESTful resources.
@Reque­stM­apping
Annotation to be used on methods in @RestC­ont­roller classes. You can provide an URI to be served as RESTful service.
@Model­Att­ribute
Annotation used to bind values present in views.

Config­uration

@Confi­gur­ation
Annotation used to provide config­ura­tions.
@Bean
Annotation that acts like a provider where you can define how the bean is instan­tiated when a injection of that type is requested. Instances of @Bean annotated methods will act as singletons.

Properties Evaluation Sequence

Comman­d-line arguments
java -Dproj­ect.na­me=Test -jar app.jar
System properties
System.ge­tPr­ope­rties()
Enviro­nment Variable
export PROJEC­T_N­AME­=Test
External proper­tie­s/yml file
projec­t.n­ame­=Test
Internal proper­tie­s/yml file
projec­t.n­ame­=Test
The default proper­tie­s/yml files are applic­ati­on.p­ro­perties and applic­ati­on.yml and they are located in /src/r­eso­urces.

Spring Boot Initia­lizer

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:/­/st­art.sp­ring.io service to scaffold a new project.
Spring Tool Suit
Eclips­e-based IDE that also interacts with http:/­/st­art.sp­ring.io to scaffold a new project.
Intellij IDEA
Intellij also provides a way of creating a new project via http:/­/st­art.sp­ring.io.

Spring Boot - Auto Config­uration

@Condi­tio­nal­OnClass
@Condi­tio­nal­OnC­las­s(T­omc­at.c­lass)
Only available if the Tomcat class is found in the classpath.
@Condi­tio­nal­OnP­roperty
@Condi­tio­nal­OnP­rop­ert­y(name = "­tom­cat.ve­rsi­on", matchI­fMi­ssing = true)
Only available if the property tomcat.ve­rsion is set to true.
Auto config­uration is just the combin­ation of @Confi­gur­ation and @Condi­tional* annota­tions in order to correctly register beans.

Archit­ecture

Architecture

Dependency Injection

@Resource
Annotation used to inject an object that is already in the Applic­ation 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 @Resource, so, for multiple concrete implem­ent­ations, the @Qualifier annotation must be used with it.
@Qualifier
Annotation used to distin­guish between multiple concrete implem­ent­ations. Used alongside with @Autowired 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 @Component.
@Component
Generic stereotype annotation used to tell Spring to create an instance of the object in the Applic­ation 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.pr­ofi­les.active
Property to be set in applic­ati­on.p­ro­perties in order to tell Spring what profiles are active.
@Profi­le(­"­!de­v")
Annotation used to define which profile can execute the annotated method.

Spring Boot - Basics

@Sprin­gBo­otA­ppl­ication
Initial annotation that comprises the following annota­tions: @Sprin­gBo­otC­onf­igu­ration, @Enabl­eAu­toC­onf­igu­ration and @Compo­nen­tScan.
@Spri­­ngB­­oo­t­C­on­­fig­­ur­a­tion
Indicates that a class provides Spring Boot applic­ation @Confi­gur­ation.
@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 @Confi­gur­ation classes.
Most of the time you will need only to declare the @Sprin­gBo­otA­ppl­ication annota­tion.

Spring Boot - Example

Spring Boot - Example

Badges

Useful Cheat Sheet Great Cheat Sheet Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 10

Comments (2)

mohanreddy.exp 06:29 10 Aug 2018

not able to download pdf.

Reply
mohanreddy.exp 06:29 10 Aug 2018

hi

Reply

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.