Tag: Spring Annotations

Spring

Spring @RequestParam Annotation

Introduction: Spring @RequestParam annotation can be used to extract the query parameters in a handler method. In this quick tutorial, we’ll learn its usage. @RequestParam Annotation: Let’s first expose an API which returns a list of users with a given first name and age:

And now, let’s use cURL to quickly test this out:

[…]

Be the First to comment. Read More
Spring

Spring @Primary Annotation

Introduction: Spring @Primary annotation is used to give a higher preference to the marked bean when multiple beans of the same type exist. Spring, by default, auto-wires by type. And so, when Spring attempts to autowire and there are multiple beans of the same type, we’ll get a NoUniqueBeanDefinitionException:

To solve this, we can […]

Be the First to comment. Read More
Spring

Spring @Lazy Annotation

Introduction: The Spring framework, by default, loads and eagerly initializes all beans at the application startup itself. In our application, we might have some pretty resource-intensive beans. We’ll prefer to load such beans on a need basis. We can achieve this using the Spring @Lazy annotation. In this tutorial, we’ll learn how to use @Lazy […]

Be the First to comment. Read More
Spring

Spring @Order Annotation

Introduction: The Spring @Order annotation was first introduced in Spring 2.0. It was then used only to define the order among the AspectJ advices. Later in Spring 4.0, the implementation of this annotation was further improved a bit. Since then, it also supports ordering of Spring components or beans in a collection like a Java […]

Be the First to comment. Read More
Spring

Spring @Value Annotation

Introduction: Spring @Value annotation is used to inject values into variables and method arguments. We can either read spring environment variables or system variables. It also supports SpEL. In this quick tutorial, we’ll explore how to work with Spring @Value annotation. Setup: Let’s start by first defining a few properties in our app.properties file:

[…]

Be the First to comment. Read More
Spring

Spring MVC Annotations

Introduction: Spring 2.5 onwards, we can use annotations to mark our Spring components. One way of doing so is to use a <component-scan> tag in our appConfig.xml:

The Spring container will then recursively scan all components in the given package & its sub-packages. In this quick tutorial, we’ll discuss the most commonly used Spring […]

3 comments Read More
Spring

Using @ResponseStatus for Http Status in Spring

Introduction: In Spring MVC, we can set the status of the HttpResponse in several ways. In this tutorial, we’ll achieve it using the @ResponseStatus annotation. We can use @ResponseStatus to mark a method or an exception class with a status code and reason that should be returned. On invoking the marked handler method or when […]

Be the First to comment. Read More
Spring

@Component vs @Repository vs @Service in Spring

Introduction: With Spring’s auto-scanning feature, it automatically detects various beans defined in our application. We usually annotate our beans using one of the available Spring annotations – @Component, @Repository, @Service, @Controller. On detecting the bean, Spring simply registers it into the ApplicationContext. In this quick tutorial, we’ll look at the difference between @Component, @Repository, and, […]

Be the First to comment. Read More
Spring

Spring Core Annotations

Introduction: Spring annotations present in the org.springframework.beans.factory.annotation and org.springframework.context.annotation packages are commonly known as Spring Core annotations. We can divide them into two broad categories: DI-Related Annotations & Context Configuration Annotations:     In this tutorial, we’ll explore all of these Spring Core annotations. DI-Related Annotations: 1. @Autowired: We use @Autowired to mark the dependency which […]

One comment Read More
Spring

Spring MVC @Controller vs @RestController Annotation

Introduction: In this tutorial, we’ll look at the difference between @Controller and @RestController annotations in Spring MVC. The @Controller annotation has been part of the Spring framework from the very beginning. As its name suggests, it defines a controller: an entry-point for a Spring Web application. Spring 4 introduced a @RestController annotation. It is a […]

One comment Read More