Spring

Framework for building enterprise-level applications in Java.

Attribute Oriented Programming (@OP) / Spring Annotations

Stereotype Annotations

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 aaplication patterns.

@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.

@Controller

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.

Multi-value/array configuration

You can provide multiple values to annotation attributes in form of an array.

@RequestMapping(value = "/", method = { RequestMethod.GET, RequestMethod.POST })

References


Children
  1. Dependency Injection