You are on page 1of 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