In most typical applications, we have distinct layers like data access, presentation, service, business, etc.
- @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
@Component
1
2
3
|
@Component public @interface Service { } |
1
2
3
|
@Component public @interface Repository { } |
@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
@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.
"employees") (
public class EmployeeController {
Employee employee = new Employee();
value = "/{name}", method = RequestMethod.GET, produces = "application/json") (
public Employee getEmployeeInJSON( String name) {
employee.setName(name);
employee.setEmail("employ1@toto.com");
return employee;
}
No comments:
Post a Comment