Spring
Framework for building enterprise-level applications in Java.
Spring Annotations
Spring relies on annotations heavily to configure its behavior. A lot of it can also be done via XML and other ways. But annotations are the most straightforward way.
Multi-value/array configuration
You can provide multiple values to annotation attributes in form of an array.
@RequestMapping(value = "/", method = { RequestMethod.GET, RequestMethod.POST })
Dependency Injection
Bean
Objects which are instantiated, assembled and managed by Spring. These objects are added to application context and Spring is responsible for managing their lifecycle.
Spring Container
Core component of Spring which is responsible for managing bean lifecycle.
Injection
Annotations
@Lazy
Instantiates bean only when required.
...
@Autowired
public A(@Lazy B b) {
this.b = b;
}
...
@DependsOn
Specified dependency explicitly to control order of instantiation.
@Component
@DependsOn("b")
public class A {
private final B b;
}
Dependency#Cyclic Dependency
What does Spring do?
When a cyclic dependency is detected, Spring automatically uses Dependency#Early Reference Resolution to partially initialize beans by injecting a proxy, and injecting the actual dependency later.
What can you do using Spring features?
In case of circular dependencies, the behavior depends on the kind of Dependency Injection mechanism you use.
-
Use Dependency Injection#Setter Injection – Recommended
In such cases, Dependency Injection#Constructor Injection causes
BeanCurrentlyInCreation
Exception - should be avoided.Dependency Injection#Setter Injection or Dependency Injection#Field Injection allow Spring to break down the injection into a two-step process to resolve the dependencies:
- Instantiation: Spring instantiates the beans without the dependencies.
- Injection: Spring injected the (already initialized) dependencies.
Dependency Injection#Field Injection is anyway discouraged, so use Dependency Injection#Setter Injection as a best practice.
-
@Lazy
annotation: Can help manage dependencies by delaying the initialization until the bean is actually needed. -
@DependsOn
annotation: Doesn't necessarily resolve circular dependency but can help in specifying order of instantiation.
Handling it yourself (recommended)
Stereotypes
Define purpose of a class.
Although, these are not just indicators. These annotations also trigger additional behavior and configurations. These additional behaviors and configurations are based on common application patterns.
Some standard ones which are part of org.springframework.stereotypes
:
-
@Component
- Marks the class as a general-purpose bean (Spring-managed component).
- Main influence here is to make the class eligible for component scanning. This adds the class to application context.
- Also allows using
@Autowired
for Dependency Injection.
-
@Service
- Marks the class as service.
- Transaction management can automatically be applied to methods in service class.
-
@Repository
- Marks a class as repository.
- Enables DAO and persistence related exceptions under Spring's
DataAccessException
hierarchy.
-
@Configuration
Spring MVC
Submodule within Spring web, which provides specific capabilities for implementing Architecture#Model-View-Controller.
Provides out-of-the-box Distributed Systems#REST service capabilities.
Defining web service using annotations
Stereotypes
-
@Controller
^b7d43f- Makes Controllers capable of handling HTTP requests.
- Works along with
@RequestMapping
annotation to map HTTP handler methods.
-
@RestController
@Controller
+@ResponseBody
- Special type of Controller, which combines
@Controller
and@RequestBody
. - => Return values of handler methods are automatically serialized into JSON or XML and returned as response.
- NOTE: For the below annotations to work, it is important that the class is annotated with
@Controller
or@RESTController
. Otherwise, it won't work, there won't be any errors, it simply wouldn't work.
Endpoints and Web#HTTP methods
@RequestMapping
@GetMapping
@PostMapping
@PutMapping
@PatchMapping
@DeleteMapping
Request, response and parameters
Payload and response
Can be specified by @RequestBody
and @ResponseBody
(not required if you're using REST Controller) annotation.
URI (Path) Params and Query (Request) Params
@PathVariable
and @RequestParam
/users/{userId}/orders/{status}?search=keyword&page=1
@GetMapping("/users/{userId}/orders/{status}")
public String getItemUser(@PathVariable String userId, @PathVariable String status, @RequestParam("search") String searchString, @RequestParam int page){
...
}
Consuming Web Service
Spring JPA
Uses JPA to provide ORM capabilities integrated with other Spring functionalities.
References
-
Spring @RequestParam vs @PathVariable #read-later