Home » Enterprise Java » spring » Spring Framework Example

About Santosh Balgar

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.

Spring Framework Example

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.

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

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

It is time to make our hands dirty with code.

2. Getting Started

Create new maven project springdemo 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. This bean is loaded into the Spring context and the method is consumed in different caller objects.

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,

  • Java based configuration can be triggered by running SpringHelloWorldJavaConfigDemo.java

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


3. How to load Spring beans from context?

In a standalone Spring application, Spring beans can be loaded from the configuration by making use of 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.

4. Download The Source Code

In this article, we have learned how a simple Spring program can be created. The example can be downloaded in the below link:

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

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Leave a Reply


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

Notify of