Other important Spring annotations

Spring provides a great deal of flexibility in defining beans and managing the life cycle of a bean. There are a few other important Spring annotations that we will discuss in the table, as follows:

Annotations

Use

@ScopedProxy

Sometimes, we will need to inject a request or a session-scoped bean into a singleton-scoped bean. In such situations, the @ScopedProxy annotation provides a smart proxy to be injected into singleton-scoped beans.

@Component, @Service, @Controller, @Repository

@Component is the most generic way of defining a Spring bean. Other annotations have more specific contexts associated with them.

  • @Service is used in the business service layer
  • @Repository is used in the data access object (DAO)
  • @Controller is used in presentation components

@PostConstruct

On any spring bean, a post construct method can be provided using the @PostConstruct annotation. This method is called once the bean is fully initialized with dependencies. This will be invoked only once during a bean lifecycle.

@PreDestroy

On any spring bean, a predestroy method can be provided using the @PreDestroy annotation. This method is called just before a bean is removed from the container. This can be used to release any resources that are held by the bean.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset