Show Topics
Hide Topics

 

Core Spring Annotations

These annotations are used by Spring to guide creation and injection of beans.

ANNOTATION USE DESCRIPTION
@Autowired Constructor, Field, Method Declares a constructor, field, setter method, or configuration method to be autowired by type. Items annotated with @Autowired do not have to be public.
@Configurable Type Used with <context:springconfigured> to declare types whose properties should be injected, even if they are not instantiated by Spring. Typically used to inject the properties of domain objects.
@Order Type, Method, Field Defines ordering, as an alternative to implementing the org. springframework.core.Ordered interface.
@Qualifier Field, Parameter, Type, Annotation Type Guides autowiring to be performed by means other than by type.
@Required Method (setters) Specifies that a particular property must be injected or else the configuration will fail.
@Scope Type Specifies the scope of a bean, either singleton, prototype, request, session, or some custom scope.

 

Example:

  • @Autowired annotation is auto wire the bean by matching data type.
  • @Autowired can be applied on setter method, constructor or a field.in above we applied at 3 places, we need to place at one of the places.

 

To activiate Spring core annotations in our application, we have to configure AutowiredAnnotationBeanPostProcessor bean in SpringConfig.xml

 

 

 

The @Qualifier annotation us used to control which bean should be autowire on a field. For example, bean configuration file with two similar person beans.

 

 

Stereotyping Annotations

ANNOTATION USE DESCRIPTION
@Component Type Generic stereotype annotation for any Spring-managed component.
@Controller Type Stereotypes a component as a Spring MVC controller.
@Repository Type Stereotypes a component as a repository. Also indicates that SQLExceptions thrown from the component’s methods should be translated into Spring DataAccessExceptions.
@Service Type Stereotypes a component as a service.

 

 

 

Spring MVC Annotations

These were introduced in Spring 2.5 to make it easier to create Spring MVC applications with minimal XML configuration and without extending one of the many implementations of the Controller interface.

ANNOTATION USE DESCRIPTION
@Controller Type Stereotypes a component as a Spring MVC controller.
@InitBinder Method Annotates a method that customizes data binding.
@ModelAttribute Parameter, Method When applied to a method, used to preload the model with the value returned from the method. When applied to a parameter, binds a model attribute to the parameter. table
@RequestMapping Method, Type Maps a URL pattern and/or HTTP method to a method or controller type.
@RequestParam Parameter Binds a request parameter to a method parameter.
@SessionAttributes Type Specifies that a model attribute should be stored in the session.

 

 

 

AOP Annotations

ANNOTATION USE DESCRIPTION
@Aspect Type Declares a class to be an aspect.
@After Method Declares a method to be called after a pointcut completes.
@AfterReturning Method Declares a method to be called after a pointcut returns successfully.
@AfterThrowing Method Declares a method to be called after a pointcut throws an exception.
@Around Method Declares a method that will wrap the pointcut.
@Before Method Declares a method to be called before proceeding to the pointcut.
@DeclareParents Static Field Declares that matching types should be given new parents,that is, it introduces new functionality into matching types.
@Pointcut Method Declares an empty method as a pointcut placeholder method.

 

 

Spring Batch

In any enterprise application we facing some situations like we wanna execute multiple tasks per day in a specific time for particular time period so to handle it manually is very complicated. For handling this type of situation we make some automation type system which execute in particular time without any man power.

Spring Batch provides reusable functions that are essential in processing large volumes of records, including logging/tracing, transaction management, job processing statistics, job restart, skip, and resource management.

Spring batch contains above 3 components

  • Application−contains all the jobs and the code we write using the Spring Batch framework.
  • Batch Core− contains all the API classes that are needed to control and launch a Batch Job.
  • Batch Infrastructure–contains readers, writers, and services used by both application &Batch components.

Sample SpringBatch Applications

  • Spring Batch application read XML data and Writer to MySQL.
  • Spring Batch application read CSV data and Writer to XML file.
  • Spring Batch application read MySQL data and Writer to XML file.
  • Spring Batch application read MySQL data and Writer to TEXT file.