Introduction to Spring Core Annotations API

Otavio Santana
xgeeks
Published in
4 min readMay 20, 2021

Spring is a popular Java framework with several features and resources to make a Java developer’s life easier. This tutorial will help you get started on its annotations.

The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications — on any deployment platform. It was born as a container of IoC. Still, nowadays, Spring is a platform that provides several tools to make the Java developer easier, such as Security, Data, etc. Those features have in common the Spring core. This tutorial will talk about the basic annotations to start to use Spring.

Spring is an annotation driver framework. Thus, it uses annotations to identify the components to inject and to use. The first step in this tutorial is a list of the most trivial annotations in Spring.

  • Component: Indicates that an annotated class is a 'component.' Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.
  • Repository: Indicates that an annotated class is a 'Repository,' originally defined by Domain-Driven Design (Evans, 2003) as 'a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects.'
  • Service: Indicates that an annotated class is a 'Service,' originally defined by Domain-Driven Design (Evans, 2003) as 'an operation offered as an interface that stands alone in the model, with no encapsulated state.'
  • Qualifier: This annotation may be used on a field or parameter as a qualifier for candidate beans when autowiring. It may also be used to annotate other custom annotations that can then in turn be used as qualifiers.
  • ComponentScan: Configures component scanning directives for use with @Configuration classes. Provides support parallel with Spring XML's <context:component-scan> element.
  • SpringBootApplication: Indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning.
  • Configuration: 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.
  • Bean: Indicates that a method produces a bean to be managed by the Spring container.
  • Primary: Indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. If exactly one 'primary' bean exists among the candidates, it will be the autowired value.
  • Lazy: Indicates whether a bean is to be lazily initialized.

To create a class observable in Spring, we need to use annotation. The basic ones are Component annotations and their specialization, such as Repository and Service.

Spring has support to polymorphism as well, where you can handle with several implementations, such as given a payment we have several options. There is the primary annotation to define a ‘default’ option, and there is a Qualifier where you can choose the implementation to select.

Spring has the option to us, as a developer, teach him how to create an instance. The idea is like the Produces annotation in CDI. So, you can use this class through Spring.

Usually, especially in Java, we create an application to communicate to another application through the HTTP request. Indeed, Spring supports it, and we did with the core annotations, below the basic’s annotations with Spring.

  • SpringBootApplication: Indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning.
  • ResponseStatus: Marks a method or exception class with the status code and reason that should be returned.
  • RequestMapping: Annotation for mapping web requests onto methods in request-handling classes with flexible method signatures.
  • GetMapping: Annotation for mapping HTTP GET requests onto specific handler methods.
  • RequestParam: Annotation which indicates that a method parameter should be bound to a web request parameter.
  • PathVariable: Annotation which indicates that a method parameter should be bound to a URI template variable.
  • PostMapping: Annotation for mapping HTTP POST requests onto specific handler methods.
  • RequestBody: Annotation indicating a method parameter should be bound to the body of the web request.
  • PutMapping: Annotation for mapping HTTP PUT requests onto specific handler methods.
  • DeleteMapping: Annotation for mapping HTTP DELETE requests onto specific handler methods.

That is it; in this tutorial, we could cover a brief introduction about Spring and its annotations in both core and the rest controller. The goal is to help you start your project with Spring frameworks because this project has a bright future and will make your life easier as a Java developer.

References:

If you enjoy working at a large scale in projects with global impact and if you like a real challenge with Java, feel free to reach out to us at xgeeks! We are growing our team, especially in Java and you might be the next one to join this group of talented people 😉

Check out our social media channels if you want to get a sneak peek of life at xgeeks! See you soon!

--

--

Otavio Santana
xgeeks
Editor for

Empowering developers worldwide to deliver better software in the Cloud. Staff Eg xgeeksio