Spring annotations

 
Remember that using the "new" operator to create objects bypasses the Spring dependency Injection mechanism.
In most typical applications, we have distinct layers like data access, presentation, service, business, etc.
In each layer we have various beans and to detect them automatically, Spring uses classpath scanning annotations.
 
Then, it registers each bean in the ApplicationContext.
Here’s a quick overview of a few of these annotations:
  • @Component is a generic stereotype for any Spring-managed component
  • @Service annotates classes at the service layer
  • @Repository annotates classes at the persistence layer, which will act as a database repository
You can annotate your component classes with @Component, but by annotating them with @Repository, @Service, or @Controller instead, your classes are more properly suited for processing by tools or associating with aspects (Spring AOP). For example, these stereotype annotations make ideal targets for pointcuts.
 

@Component

We can use @Component across the application to mark the beans as Spring’s managed components. Spring only pick up and registers beans with @Component  and doesn’t look for @Service and @Repository in general.
They are registered in ApplicationContext because they themselves are annotated with @Component:
1
2
3
@Component
public @interface Service {
}
1
2
3
@Component
public @interface Repository {
}
@Service and @Repository are special cases of @Component. They are technically the same but we use them for the different purposes.
 

@Repository

@Repository’s job is to catch persistence specific exceptions and rethrow them as one of Spring’s unified unchecked exception.

Teams implementing traditional Java EE patterns such as "Data Access Object" may also apply this stereotype to DAO classes.

A class thus annotated is eligible for Spring DataAccessException translation when used in conjunction with a PersistenceExceptionTranslationPostProcessor. The annotated class is also clarified as to its role in the overall application architecture for the purpose of tooling, aspects, etc.

@Service

We mark beans with @Service to indicate that it’s holding the business logic. So there’s no any other specialty except using it in the service layer.
 

@Controller and @RestController

@Controller is used to mark classes as Spring MVC Controller.

@RestController s a convenience annotation that does nothing more than adding the @Controller and @ResponseBody annotations. The controller simply returns the object and the object data is written directly to the HTTP response as JSON/XML. By using this annotation, we won't need to use the @ResponseBody annotations on the class methods.

Example:
 
@RestController
 
@RequestMapping("employees")
 
public class EmployeeController {
 
 
    Employee employee = new Employee();
 
 
    @RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = "application/json")
 
    public Employee getEmployeeInJSON(@PathVariable String name) {
 
 
       employee.setName(name);
 
       employee.setEmail("employ1@toto.com");
 
 
       return employee;
 
 
    }

No comments:

Post a Comment