Spring Framework Fundamentals in Examples

Artem Diashkin
Apr 7 · 6 min read

Let’s take a look at fundamentals of a Spring Framework with some basic Spring annotations. This artile will guide you through the basic steps of how you can create an application with use of a Spring Framework.

If you don’t want to create a new project from scratch, you can use this repository as a reference (It is a finished project. This story will guide you step-by-step at how it was created):

Create a new maven project

Before we begin our journey, let’s create a maven project. I will use JDK 1.8:

Next, type your project’s name, GroupId and ArtifactId (it does not really matter what you type for demonstration purposes)

After we’ll click Finish we will see an empty project without any java classes, but with a pom.xml file (if you are familiar with a Node.js projects — this is like a package.json file):

New maven project’s pom.xml file:

Adding a starting point

Now we are ready to add our first class and tests that everything works as expected without any issues:

Application.java file:

After running our application we should get a string output at our console:

Cool 🎉. Now we can add Spring dependency to our project.

Adding Spring dependency

To be able to use Spring Framework in our project we will need to add this dependency to our pom.xml file:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.5</version>
</dependency>

Don’t forget to update your project after adding spring-context dependency.

Now our pom.xml file will look like this:

If you are interested in where this dependency info was getting from with all those groupId and version data, you can check by this link to Maven reposytory.

After updating our project you will notice new external libraries in your project:

Now we are ready to add some code.

Add model, repository, and service

We will create our demo project like you would do in production, with Models, Repositories, and Services.

We will create a simple User model — a simple Java class with two fields: firstName, lastName with getters and setters:

Our repositories folder will contain two Java classes:

  • Repository implementation (we will not connect to any databases, we will use simple mocked data);
  • Repository interface;

Let’s create two Users and return them as a List:

The interface will look like this, nothing scary:

Services will be mich alike to repositories classes:

This is a representation of business logic (maybe we would like to do something with those users from a database… but we will not):

UserService interface:

App Configuration

Now, after adding all those classes we are ready to create our Spring Configuration class:

@Configuration annotation is a starting point of a Spring Framework application and defined@Beans will be injected by the Spring IoC container:

⚠️ NOTE: Each Spring Bean is a singleton by default. It means that it doesn't matter how many times you will call “getUserRepository()” method → Spring will return the exact same instance of a HibernateUserReposytoryImp

⚠️ NOTE: We could remove all this @Bean with use of @ComponentScan and @Autowire but we will cover that later in this story

Example:

As you can see Spring will return exact same object (that hash value is a simple reference to memory, it is not changed, so it means that it is the same object) You can change this behavior by changing a @Scope of a Bean from a singleton to a prototype:

⚠️ NOTE: There are three additional scopes (5 in total) in Spring Framework, but those are valid for a Spring application with web pages: Request, Session, and Global .

Now, let’s add a starting point of our Spring Framework application. We will need to update our Application class with a main() method:

Let’s re-run our application:

Console output:

Congratulations, Spring is configured and running 🎉.

Constructor injection

Previously we used Setter injection, but now let’s use a Constructor injection. For that, we will need to add a constructor to the UserServiceImpl class and refactor AppConfiguration class:

If we re-run our application we will get the same result.

Autowired annotation

With the use of the @Autowired annotation, we could get rid of our Setter/Constructor injections.

Add @Autowired annotation to the “setRepository” method:

Now Spring will automatically inject UserRepository into the UserService:

Result:

Stereotype Annotations. ComponentScan

Spring also provides to us stereotype annotations → “Annotations denoting the roles of types or methods in the overall architecture (at a conceptual, rather than implementation, level)”, and with use of the @ComponentScan Spring will automatically scan our declared folder for Components like @Service and @Repository :

Now, at step 4 we can get rid of all our previous configuration and Spring Framework we do the rest for us:

Just a reminder of what we had in a previous version:

And what we got now:

If we will remove @Autowired annotation from a setter and set it to the constructor…

…we will get this result:

LITSLINK

LITSLINK’s team is sharing their experience

Sign up for LITSLINK Newsletter

By LITSLINK

It's a test newsletter! Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Artem Diashkin

Written by

Java, Spring, Node.js, AdonisJs, React.js and Flutter developer

LITSLINK

LITSLINK

Hands-on up to date experience sharing of the most skilled & talented software developers right here, right now. Subscribe to learn & understand more about the Software World.

Artem Diashkin

Written by

Java, Spring, Node.js, AdonisJs, React.js and Flutter developer

LITSLINK

LITSLINK

Hands-on up to date experience sharing of the most skilled & talented software developers right here, right now. Subscribe to learn & understand more about the Software World.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store