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

Spring Core Cheat Sheet: Container, Dependency and IOC

Spring is an open source framework that provides dependency injection and inversion of control to ease the development of well-structured Java applications. It handles object lifecycles and wiring of objects together without requiring applications to look up or instantiate dependencies directly. Beans can be defined using annotations like @Component, @Service, @Repository, and @Controller. Dependencies are injected via autowiring fields, setters, or constructors. Beans have different scopes like singleton, prototype, request, session, and application that control their lifecycle. Properties can be externalized and accessed using the @Value annotation or @PropertySource.

Uploaded by

Rambla Magalie
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)
554 views

Spring Core Cheat Sheet: Container, Dependency and IOC

Spring is an open source framework that provides dependency injection and inversion of control to ease the development of well-structured Java applications. It handles object lifecycles and wiring of objects together without requiring applications to look up or instantiate dependencies directly. Beans can be defined using annotations like @Component, @Service, @Repository, and @Controller. Dependencies are injected via autowiring fields, setters, or constructors. Beans have different scopes like singleton, prototype, request, session, and application that control their lifecycle. Properties can be externalized and accessed using the @Value annotation or @PropertySource.

Uploaded by

Rambla Magalie
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/ 2

 

SPRING CORE CHEAT SHEET


Container, Dependency and IOC

Spring 5.x - Date : August 2018


DEPENDENCY INJECTION
WHAT IS SPRING ? De ne a bean : @Bean

Open source (Apache 2 licence, sources on github) @Configuration


Light public class MyAppConfig {
Doesn't force to use an application server @Bean
Not invasive public DummyService dummyService(){
return new DummyServiceImpl();
Container
}
Application objects doesn't have to look for their }
dependencies
Handles objects life cycle
De ne a bean : @Component
Framework
Ease integration and communication with third- @Component specializations :
party libraries
@Service : Heart of an app
MINIMAL DEPENDENCIES @Repository : Handles data persistence
@Controller : Handles requests and reponses

<dependency>
<groupId>org.springframework</groupId> @Component
<artifactId>spring-context</artifactId> public class DummyServiceImpl implements
</dependency> DummyService {
}

APPLICATION CONFIGURATION Dependency injection : @Autowired

Java Via a class eld


Annotations Via a setter
XML (still available but heavy) Via the constructor (prefer for easy test)

@ComponentScan("fr.sii.cheatsheet.spring") @Component
public class MyApp { public class FooServiceImpl implements FooService
{
public static void main(String[] args) {
ApplicationContext app = new @Autowired
AnnotationConfigApplicationContext(MyApp.class); private DummyService service;

DummyService helloWorld = @Autowired


app.getBean(DummyService.class); public FooServiceImpl(DummyService
helloWorld.getMessage(); dummyservice) {
} this.service = dummyService;
} }

@Autowired
Prefer use of a con guration class
public DummyService
setDummyService(DummyService dummyService) {
@Configuration this.service = dummyService;
public class MyAppConfig { }
// @Bean, ...
} }

groupe-sii.github.io/cheat-sheets www.groupe-sii.com blog.groupe-sii.com


   
   

SPRING CORE CHEAT SHEET

BEANS SCOPES PROPERTIES

Scope Description /**


singleton (default) A single bean instance * File 'foo.properties' loaded by Spring
*/
prototype A bean instance per usage @Configuration
@PropertySource("classpath:foo.properties")
request A bean instance per HTTP request
public class MyAppConfig {
session A bean instance per HTTP session
/**
A bean instance per Servlet * Property max in file foo.properties.
application
Context */
private Integer max;
websocket A bean instance per WebSocket
}
CUSTOMIZING A BEAN
Use a property : @Value("${message}")
Initialization method
@Value("${message:Default message}")
private String message;
@PostConstruct

Destroy method GO DEEPER

@PreDestroy https://spring.io/guides
https://spring.io/projects/spring-framework
Spring Core documentation
SPEL : SPRING EXPRESSION LANGUAGE Spring Boot cheat sheet

Access a system properties

@Value("#{ systemProperties['user.home'] }")


private String defaultHome;

Access a bean property

@Value("#{myBean.myValue}")
private String myValue;

Parse a string

@Value("#{myBean.myValue.substring(0,1)}")
private String myValue;

Operations

@Value("#{myBean.myValue.length() > 2}")


private String myValue;

groupe-sii.github.io/cheat-sheets www.groupe-sii.com blog.groupe-sii.com

You might also like