Spring Framework Example (with video)

Spring Framework is unarguably the most in-demand framework in the Java Enterprise development ecosystem. In this article, I am going to showcase a simple example using Spring Framework.

You can also check this tutorial in the following video:

Java Spring Boot Tutorial – video

If you are new to Spring, I encourage you to get a basic understanding of the concepts behind the Spring framework. One of my previous articles about what is Spring Framework can be a good starting point.

You can find more spring tutorials here.

1. Prerequisites

In this section, I am going to discuss what do you need to start with:

  • Java 8 or higher (JRE isn’t enough. Install Java SDK)
  • IDE (I am using IntelliJ community edition)
  • Basic knowledge of Spring

2. Why Spring Framework?

Spring framework surely makes it easy for the Java developers. Now, we are going to know more about the benefits of Spring framework.

  1. Spring framework is an extremely powerful framework, and thus automatically becomes the first choice for the developers. It successfully addresses the problems posed in Java EE.
  2. Another advantage of the Spring framework is modularity. Because of modularity, the developers easily choose to work on the specific modules. Examples of such modules are Spring JDBC, Spring MVC, and Spring ORM.
  3. Spring framework leads to ease in application testing. Testing occurs outside the enterprise container, resulting in the ease in testing.
  4. In addition, Spring framework supports the enterprise application development using POJO – Plain Old Java Object. POJO removes the need of importing very heavy enterprise containers.
  5. It is extremely easy to integrate Spring with any other frameworks like Struts, Hibernate, etc. Spring does not have any restriction on the imposition of one framework with the other.
  6. Spring is quite lightweight. This is a benefit for developing applications on systems with less memory and resources.

3. Spring Framework Architecture

Spring framework is a layered architecture and has many modules. Each module has its own functionality. Let us discuss each of the layers in detail. The image below shows the different modules in each of the layers.

Spring Framework Architecture
Spring Framework Architecture

3.1 The Core Container

The core container comprises of the core, bean, contexts, and the expression modules. The core and beans primarily give the fundamental base of the framework, including the dependency injection features. While the context builds onto the core and beans module, it is also a means to access the objects in a “framework-style” manner. The EL – expression language – helps in manipulating and querying the object graph at the runtime.

3.2 Test

This module aids in the testing of different components of Spring with either JUnit or TestNG. It makes sure that the SoringApplicationContexts are consistently loaded, and also cached.

3.3 Data Access/Integration

This layer consists of the JDBC, ORM, OXM, JMS, and Transaction modules. The JDBC modules help the developer to drive away without doing the tedious coding for JDBC. While the ORM provides integration layers for the object-relational mapping APIs including the like of Hibernate and JPA, the OXM module is used for giving an abstraction layer – supporting the XML or Object mapping implementations. JMS – also known as Java Messaging services – contains the code for creating as well as consuming messages. At last, the transaction module supports classes that implement interfaces and all the Plain Old Java Objects.

3.4 Web

The Web layer comprises of different modules like Web, Web-servlet, Web-Struts, and Web-Portlet. The Web module gives the basic web integration features such as multipart file-upload functionality. Web-Servlet is most commonly known, and it contains the Spring MVC (i.e. Model View Controller) implementation. Whereas the Web-Struts contain the basic support classes so that it can easily integrate a Struts web within the Spring application. Last, but not the least – Web Portlet provides the MVC implementation specifically to be used in a portlet environment, and provides similar functionality like that of Web-Servlet.

3.5 AOP/Instrumentation

AOP stands for ‘Aspect-Oriented programming’. Spring AOP module provides AOP’s implementation to the developers. You can use the source-level metadata functionality in order to use the behavioral information into your code. While the Instrumentation module is specifically for the class support as well as the class loader implementations that are particularly used for application servers.

That’s all about the Spring architecture. Now, we can code it all out.

4. Get Started with Spring

Create a new maven project sprint demo and add below dependencies to your project:

Spring Framework Example - maven configuration
maven configuration

I am going to show both XML based configuration and Java-based configuration.

Create a Java bean class HellowWorld under the package com.jcg.beans as below,

Spring Framework Example - HelloWorld

HelloWorld is a simple Java bean class with a single method. The bean then loads into the Spring Context. And different called objects consume the methods.

To load the HelloWorld bean into Spring Context using Java based configuration, create a config file ApplicationConfig under com.jcg.config package. Refer the below screenshot,

Spring Framework Example - ApplicationConfig

Each of the Java beans is annotated with @Bean annotation. By default, beans are identified by their type. However, for more specific identification, they can be followed by a name.

Java beans can also be loaded to context using XML based configuration. Below is the sample XML configuration,

Spring Framework Example - Spring Beans XML configuration
Spring Beans XML configuration

Java beans are configured in <bean/> tag. spring-beans.xml file has to be created in resources folder, that way ClassPathXmlConfiguration class can pick it up without the absolute path.

To run the program,

  • XML based configuration can be triggered by running SpringHelloWorldXMLConfigDemo.java.

Above class loads, the Spring XML configuration from the classpath and the output of the program is as below,

  • To trigger the Java-based configuration, run SpringHelloWorldJavaConfigDemo.java

Above class loads, the beans using annotations and output is as below,


5. How to load Spring beans from context?

In a standalone Spring application, Spring beans can be loaded from the configuration by making use of the below classes,

  • ClassPathXmlApplicationContext – Open and read xml based application context configuration.
  • AnnotationConfigApplicationContext – If you choose to use Java-based configuration instead of XML configuration, you need to use this class.

6. Additional knowledge

7. Download The Source Code

In this article, we have learned how a simple program can be created using the Spring framework.

You can download the full source code of this example here: Spring Framework Example

Last updated on May 17th, 2021

Santosh Balgar

He is a Software Engineer working in an industry-leading organization. He has completed his bachelors from Visweswaraya Technological University. In his career, he has worked in designing and implementing various software systems involving Java/J2EE, Spring/ Spring Boot, React JS, JQuery, Hibernate and related database technologies. He loves to share his knowledge and always look forward to learning and explore new technologies. He loves to spend his free time with his family. He enjoys traveling and loves to play cricket.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button