Posts

Showing posts with the label Spring

Spring Bean Scopes Explained With Code Samples

Image
The Spring Framework allows defining scopes for a Spring Bean. Spring Bean scopes define the lifetime of a Spring bean. In this article, we will take a look at bean scopes. What is Spring Bean Scope? A Spring Bean scope specifies the lifetime of a Spring bean. So it specifies when the bean will be created and how long it will exist in the system. Edit What are the different bean scopes? Spring supports the following bean scopes:   Singleton This is the default bean scope. As per this scope, only one instance of a bean is created. Each time the bean is requested, the same instance is returned. Prototype As per this scope, a new instance of a bean is created each time the bean is requested. Request This bean scope is valid only for a web application. As per this bean scope, a new instance of the bean is created for each HTTP request. The bean stays alive until the HTTP request completes.  If you use this bean scope for a standalone application, your code will result in an...

How Spring Works under the Hood

Image
In my earlier article , I gave an introduction to Spring. I covered how the Spring Framework helps in wiring together the objects used in an application. In this article, I will be covering how exactly spring achieves this. Introduction Just to recap , the Spring framework consists of the Spring container. The Spring Container is responsible for creating the objects required in an application and providing them to the necessary classes.  It uses Inversion of Control via Dependency Injection to achieve this. Let us now see how exactly this happens. Edit High Level Overview In order for Spring to be able to wire up the application, it requires developers to provide the following: Beans – The objects in an application are known as beans in Spring terminology. These are simple POJO classes. Spring configures objects of these classes Configuration metadata – Information on how to instantiate the beans and wire them together. The Spring container is also known as the Sp...

Spring Annotation-Based Configuration Example

In this article, I will be demonstrating how to configure a standalone Spring application using annotation-based configuration . I will be using Eclipse and Maven . In order to get a basic introduction to Spring , you can check out this article. In order to understand how to configure a standalone Spring application using XML configuration, you can refer to this article. In order to understand how to configure a standalone Spring application using Java configuration, you can refer to this article. Project Set-up Step 1 – Create a new Maven Project in Eclipse. You can refer to this article on how to create a Maven project in Eclipse. Step 2 – Add Spring dependencies to the maven pom file. You can add the following: <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.2.RELEASE</version> </dependency> <dependency> <gro...

Spring XML Configuration Example

In this article, I will be demonstrating how to configure a standalone Spring application using XML configuration . I will be demonstrating both setter and constructor injection . I will be using Eclipse and Maven . In order to get a basic introduction to Spring, you can check out this article. Project Set-up Step 1 – Create a new Maven Project in Eclipse. You can refer to this article on how to create a Maven project in Eclipse. Step 2 – Add Spring dependencies to the maven pom file. You can add the following: <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.2.RELEASE</version> </dependency> </dependencies> Edit ...

Spring framework - What and Why

The Spring framework is widely used for developing different types of applications. In this article, I will dive into the details of what is the spring framework and the advantages that it provides. Before Spring A typical Java application consists of many objects. For example if you are building an application that interacts with the database, your application would have objects corresponding to the POJO class, DAO class, etc. The problem with Java was that there was no way to wire together these objects. So your main class or the starting point of the application would somehow need to create all the required objects and the other parts of the application would use these as required. Design patterns like Factory Pattern, Abstract Factory Pattern helped to ease this slightly, but still the responsibility of creating all the necessary objects still lay with the application code. Java then introduced the EJB framework which eased this to some extent. The problem with EJB framework was th...

Spring Modules Explained

The Spring framework is widely used for different types of applications right from web applications, to even standalone applications. Spring framework provides a wiring mechanism, so you do not have to configure your dependencies manually, Spring does this automatically for you.  In this article, I will be giving an overview of the modules within the Spring framework. I will be explaining how many modules are there in the Spring framework and also be giving an overview of the modules. Introduction The Spring Framework consists of several modules. Each module performs a different functionality. Depending on your requirements, you can add the dependencies for the necessary modules. Spring modules are grouped into different categories as explained below. Edit Spring Core This contains the spring-core, spring-beans, spring-context, spring-context-support, and spring-expression modules . These are the main modules that make up the Spring framework.  Any Spring application needs t...

JAX-RS vs Spring REST - Differences

In this article, I will be covering the differences between JAX-RS/Jersey and Spring REST. What is REST? REST stands for Representational State Transfer.  It is an architectural style that can be used for web services. A REST server exposes functionality as REST endpoints which are simply URLs. A REST client simply uses the services exposed by the REST server by accessing the corresponding URL. Edit What is JAX-RS JAX-RS stands for Java API for RESTful Web Services (JAX-RS). It is the standard Java API for developing RESTful web services. The latest version of JAX-RS is JAX-RS 2.0.  It is part of Java EE 7, so it does not need to be included separately. It is just a specification, it does not provide an implementation. Jersey is the reference implementation of JAX-RS specification. There are other implementations of JAX-RS like RESTEasy, etc   What is Spring REST The Spring framework also provides REST support. The Spring MVC module can be used to develop a REST service....

Spring Exception Handling With Examples

Spring  In this article, I will be covering the various exception handling mechanisms in Spring . Using an ExceptionHandler The first approach is the ExceptionHandler approach. In this approach, you can designate a method in your controller with the @ExceptionHandler annotation. You can specify some exceptions with this annotation. When those exceptions occur, the code in the method will be executed. The following code demonstrates this: @Controllerpublic class PersonController {//controller methods@ExceptionHandler(RuntimeException.class)public void onRunTimeException(RuntimeException e){//exception handling code for RuntimeException}} Edit Here, the code specifies the @ExceptionHandler annotation on the onRuntimeException class. So this method is designated as an exception handling method. Whenever a RuntimeException occurs within any method in the PersonController, the code invokes this method. You can specify any type and any number of arguments to a method that has the ...

Spring REST client using RestTemplate in Eclipse and Maven

Image
In this article, I will be demonstrating how you can create a Spring REST client using RestTemplate. In order to see how you can create a simple Spring REST service, you can refer to this blog post. What is Spring RESTTemplate? Spring provides a class called RestTemplate . This is the starting point in creating a REST client application.  It uses the URI and the HTTP method to invoke in order to connect to a REST service. It has several methods that allow you to invoke the HTTP methods on the specified URI Edit Project Creation Step 1 – Create a Maven Project in Eclipse . You can refer to this blog post. You will get a project as follows:   Step 2 – Add dependencies to POM file. Add the following to your pom.xml file: <properties> <spring.framework>5.1.3.RELEASE</spring.framework> <spring.web>5.1.3.RELEASE</spring.web> </properties> <dependencies> <dependency> <groupId>org.springframewo...

Spring @ExceptionHandler annotation explained

In this article, I will be explaining the Spring @ExceptionHandler annotation and how it can be used for exception handling in Spring . Introduction You can specify the @ExceptionHandler annotation on a method in a controller class and mark the method as an exception handling method. Along with the annotation you need to specify the exceptions that the method handles. So when an exception of the specified type occurs, Spring uses the exception handler method to handle the exception.  Within the exception handler method, you can specify any exception handling code like returning an error code or redirecting to an error page, etc. Edit The following code sample demonstrates this: @RestControllerpublic class PersonController { @GetMapping(value = "/person/{personId}", produces = { "text/plain" }) public Person getPersonWithId(@PathVariable Integer personId) { // Returns hardcoded data, a real world application would return from // the database re...

Important Spring MVC Annotations Explained

In this blog post, I will be explaining some of the important Spring MVC Annotations used in Spring applications. Controller You can use the Controller annotation to designate a Controller class in an MVC application. Normally in such scenarios, the code maps each method in the controller to a URL specified via the  RequestMapping annotation. When the UI requests a particular URL, the code invokes the appropriate method. This executes the business logic and then redirects the user to the appropriate JSP page. Edit RestController Spring 4.0 introduced the  RestController annotation. So the  RestController annotation is a specialization of the Controller annotation that can be used to designate a REST service. It combines the behavior of the  Controller and  ResponseBody annotations. So when a method in a class is designated with this annotation, it returns the REST response directly to the client instead of redirecting the user to an HTML or JSP page ResponseBody The  Response...

Spring RequestMapping, GetMappin, PostMapping annotations

In this blog post, I will be explaining the RequestMapping, GetMapping and PostMapping annotations in Spring. In order to know more about other Spring annotations, you can refer to this article. In order to learn about the Spring framework from scratch, I recommend that you enroll in this Spring masterclass . You can use the RequestMapping, GetMapping and PostMapping annotations to map a URL to a particular Controller method. However, there are some differences between them. Let’s check them out. Edit RequestMapping The RequestMapping annotation can be used to map both GET and POST requests. Consider the following code: @RequestMapping(value="/person/{personId}",method=RequestMethod.GET) public Person getPersonWithId(@PathVariable Integer personId){ } Here, the RequestMapping annotation is used on the getPersonWithId method. It maps to the /person/{personId} URL. The method attribute is also specified. This indicates that this method maps to an HTTP GET request...

Creating a Spring REST application in Maven and Eclipse

Image
In this article, I will be demonstrating how to create a REST application via Spring using Maven and Eclipse. Create a Project and And Maven Support Step 1 – Follow the steps in this article to create a web project in Eclipse with Maven Support. You should see a project as follows: Edit Step 2 – Add the Maven Dependencies for Spring to the pom file as follows: <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.1.3.RELEASE</version> ...