Some Special Annotations
@Autowired
By declaring all the bean dependencies in a Spring configuration file, the Spring container can atowire relationships between collaborating beans. This is called Spring bean autowiring.
We can use autowiring on properties, setters, and constructors.
Autowiring feature of spring framework enables you to inject the object dependency implicitly. It internally uses setter or constructor injection.
Autowiring can't be used to inject primitive and string values. It works with reference only.
Advantage
It requires the less code because we don't need to write the code to inject the dependency explicitly.
@RestController
We use @RestController annotation to simplify the creation of RESTful web services.
The @RestController annotation in Spring is essentially just a combination of @Controller and @ResponseBody. This annotation was added to remove the redundancy of declaring the @ResponseBody annotation in the controller.
@Controller : specialized for generic stereotype @Component annotation, which allows a class to be recognized as a Spring-managed component.
@ResponseBody : tells a controller that the object returend is automatically serialized into JSON and passed back into the HttpResponse object.
@Service
@Repository
Indicated that the decorated class is a repository. A repository is a mechanism for encapsulating storage, retrieval, and search behavior that emulates a collection of objects.
@Component
The @Component annotation makes a java class as a bean so the component-scanning mechanism of spring can pick it up and pull it into the application context. The @Controller, @Service, @Repository annotations are also specialization of the component annotation.
The Component can act as a Repository, Service or as a Controller and this Component apply on class level.
@Bean
Spring @Bean annotation tells that a method produces a bean to be managed by the Spring container. It is a method-level annotation. The bean name will be the method name by default.
@Configuration
@Configuration annotation indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
@ControllerAdvice
@ControllerAdvice is a specialization of the @Component annotation which allows to handle exceptions across the whole application in one global handling component. It can be viewed as an interceptor of exceptions thrown by methods annotated with @RequestMapping and similar.



0 Comments