annotations cheat sheet
Spring Boot and Web annotations
Use annotations to configure your web application.
@SpringBootApplication - uses @Configuration,@EnableAutoConfiguration and @ComponentScan.@EnableAutoConfiguration - make Spring guess theconfiguration based on the classpath.
@Controller - marks the class as web controller, capableof handling the requests. @RestController - a convenience
annotation of a @Controller and @ResponseBody.
@ResponseBody - makes Spring bind method’s returnvalue to the web response body.
@RequestMapping - specify on the method in thecontroller, to map a HTTP request to the URL to this method.
@RequestParam - bind HTTP parameters intomethod arguments.
@PathVariable - binds placeholder from the URIto the method parameter.
Spring Framework annotations
Spring Cloud annotations
Make you application work well in the cloud.
@EnableConfigServer - turns your application intoa server other apps can get their configuration from.
Use spring.application.cloud.config.uri in the client
@SpringBootApplication to point to the config server.@EnableEurekaServer - makes your app an Eurekadiscovery service, other apps can locate services through it.
@EnableDiscoveryClient - makes your app register in theservice discovery server and discover other services through it.
@EnableCircuitBreaker - configures Hystrix circuitbreaker protocols.
@HystrixCommand(fallbackMethod =“fallbackMethodName”) - marks methods to fall back
to another method if they cannot succeed normally.
Spring uses dependency injection to configure and bind
your application together.
@ComponentScan - make Spring scan the packagefor the @Configuration classes.
@Configuration - mark a class as a source ofbean definitions.
@Bean - indicates that a method produces a bean to bemanaged by the Spring container.
@Component - turns the class into a Spring bean at theauto-scan time. @Service - specialization of the
@Component, has no encapsulated state.@Autowired - Spring’s dependency injection wiresan appropriate bean into the marked class member.
@Lazy - makes @Bean or @Component be initializedon demand rather than eagerly.
@Qualifier - filters what beans should be used to@Autowire a field or parameter.@Value - indicates a default value expression forthe field or parameter, typically something like
“#{systemProperties.myProp}”
@Required - fail the configuration,if the dependency cannot be injected.
For more awesome cheat sheets
visit rebellabs.org!
Legend
- class
- field annotation
- constructor annotation
- method
- parameter
@SpringBootApplicationHTTP Responses
@RestController@AutowiredService service;@RequestMappingpublic Map serveRequest()
@Configuration@Beanpublic MyBean
provideBean()
@Service@Componentprovides
beans
like these
beans
are
injected
Cloud
Configuration
server
Service
discovery
asks
configuration
receives
properties
asks services
location
receives URL
registers itself
as a service
HTTP Requests
TFCMPFCMFCMFCMT M
FCMTTMTTTMTTTTMTPTTTTMP