Spring framework implements and promotes the principle of control inversion (IOC) or dependency injection (DI) and is in fact an IOC container.
Traditionally, Spring allows a developer to manage bean dependencies by using XML-based configuration.
There is an alternative way to define beans and their dependencies. This method is a Java-based configuration.
Unlike the XML approach, Java-based configuration allows you to manage bean components programmatically. That’s why Spring annotations were introduced.
This is a compilation of most commonly used Spring Annotations.
@Configuration - mark a class as a source of bean definitions.
@Bean - indicates that a method produces a bean to be managed by the Spring container.
@Component - turns the class into a Spring bean at the auto-scan time.
@Service - specialization of the @Component, has no encapsulated state.
@Autowired - Spring’s dependency injection wires an appropriate bean into the marked class member.
@Lazy - makes @Bean or @Component be initialized on demand rather than eagerly.
@Qualifier - filters what beans should be used to @Autowire a field or parameter.
@Value - indicates a default value expression for the field or parameter, typically something like
“#{systemProperties.myProp}”
@Required - fail the configuration, if the dependency cannot be injected.
@EnableAutoConfiguration - make Spring guess the configuration based on the classpath.
@Controller - marks the class as web controller, capable of handling the requests.
@RestController - a convenience annotation of a @Controller and @ResponseBody.
@ResponseBody - makes Spring bind method’s return value to the web response body.
@RequestMapping - specify on the method in the controller, to map a HTTP request to the URL to this method. @RequestMapping variants ---> @GetMapping
@PostMapping
@PutMapping
@PatchMapping
@DeleteMapping
@RequestParam - bind HTTP parameters into method arguments.
@PathVariable - binds placeholder from the URI to the method parameter.
Use spring.application.cloud.config.uri in the client @SpringBootApplication to point to the config server.
@EnableEurekaServer - makes your app an Eureka discovery service, other apps can locate services through it.
@EnableDiscoveryClient - makes your app register in the service discovery server and discover other services through it.
@EnableCircuitBreaker - configures Hystrix circuit breaker protocols.
@HystrixCommand(fallbackMethod = “fallbackMethodName”) - marks methods to fall back
to another method if they cannot succeed normally.
I have listed here most of the important annotations, but there are a lot more of them for specific tasks. If you are not clear with these quick definitions, you can find more detailed information on each of these annotations at https://dzone.com/articles/a-guide-to-spring-framework-annotations.
Traditionally, Spring allows a developer to manage bean dependencies by using XML-based configuration.
There is an alternative way to define beans and their dependencies. This method is a Java-based configuration.
Unlike the XML approach, Java-based configuration allows you to manage bean components programmatically. That’s why Spring annotations were introduced.
This is a compilation of most commonly used Spring Annotations.
********Spring Framework annotations********
@ComponentScan - make Spring scan the package for the @Configuration classes.@Configuration - mark a class as a source of bean definitions.
@Bean - indicates that a method produces a bean to be managed by the Spring container.
@Component - turns the class into a Spring bean at the auto-scan time.
@Service - specialization of the @Component, has no encapsulated state.
@Autowired - Spring’s dependency injection wires an appropriate bean into the marked class member.
@Lazy - makes @Bean or @Component be initialized on demand rather than eagerly.
@Qualifier - filters what beans should be used to @Autowire a field or parameter.
@Value - indicates a default value expression for the field or parameter, typically something like
“#{systemProperties.myProp}”
@Required - fail the configuration, if the dependency cannot be injected.
********Spring Boot and Web annotations********
@SpringBootApplication - uses @Configuration, @EnableAutoConfiguration and @ComponentScan.@EnableAutoConfiguration - make Spring guess the configuration based on the classpath.
@Controller - marks the class as web controller, capable of handling the requests.
@RestController - a convenience annotation of a @Controller and @ResponseBody.
@ResponseBody - makes Spring bind method’s return value to the web response body.
@RequestMapping - specify on the method in the controller, to map a HTTP request to the URL to this method. @RequestMapping variants ---> @GetMapping
@PostMapping
@PutMapping
@PatchMapping
@DeleteMapping
@RequestParam - bind HTTP parameters into method arguments.
@PathVariable - binds placeholder from the URI to the method parameter.
********Spring Cloud annotations********
@EnableConfigServer - turns your application into a 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 Eureka discovery service, other apps can locate services through it.
@EnableDiscoveryClient - makes your app register in the service discovery server and discover other services through it.
@EnableCircuitBreaker - configures Hystrix circuit breaker protocols.
@HystrixCommand(fallbackMethod = “fallbackMethodName”) - marks methods to fall back
to another method if they cannot succeed normally.
I have listed here most of the important annotations, but there are a lot more of them for specific tasks. If you are not clear with these quick definitions, you can find more detailed information on each of these annotations at https://dzone.com/articles/a-guide-to-spring-framework-annotations.
No comments:
Post a Comment