Category: Spring

Spring

Spring Boot YAML Configuration

Introduction: In this quick tutorial, we’ll learn how to use a YAML file to configure properties of a Spring Boot application. What is YAML File? Instead of having an application.properties in Spring, we can use the application.yml as our configuration file. YAML is a superset of JSON and we can use it for configuring data. The […]

Be the First to comment. Read More
Spring

Spring ClassPathXmlApplicationContext

Introduction: Spring provides two types of containers: BeanFactory: It supports bean instantiating and wiring ApplicationContext: It extends the BeanFactory and so provides all those features as that a BeanFactory. Additionally, it provides automatic registration of BeanPostProcessor, internationalization, and many more features The Spring container is responsible for instantiating and managing the lifecycle of Spring beans. […]

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

One 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

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

One comment Read More