Category: Spring

Spring

Spring Dependency Injection

Introduction: In a well-designed Java application, the classes should be as independent as possible. Such a design promotes reusability of components. It also makes it easier to unit test the various components. The concept of dependency injection promotes loose coupling among Java objects. In this tutorial, we’ll talk about the dependency injection in Spring framework. […]

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 […]

Be the First to comment. Read More
Spring

Spring Boot Exit Codes – Create Custom Exit Code

Introduction: When running a Spring Boot application, we get a system exit code of 0, when everything goes fine.  For any unhandled exceptions, the application returns with an exit code 1. It’s possible for us to return a custom exit code from our Spring Boot application. In this tutorial, we’ll learn to do so. Implementing […]

Be the First to comment. 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 […]

Be the First to comment. Read More
Spring

Spring : Injecting Prototype Beans Into Singleton Instance

Introduction: In the Spring framework when we inject a prototype bean into a singleton bean, it still behaves as a singleton. It’s known as prototype-bean injection problem. In this tutorial, we’ll learn ways in which we can inject a prototype instance into a singleton bean in Spring. It’s a scenario we’re most likely to encounter […]

One comment Read More
Spring

Spring Http Request Logging

Introduction: Logging the Http requests in a web application is a very common requirement. In this tutorial, we’ll learn to log all our incoming Http requests in Spring framework. To follow along with the tutorial, we assume that the reader has an understanding of spring-core and Logback. Using CommonsRequestLoggingFilter: A pretty simple way to log […]

Be the First to comment. Read More