INTRODUCTION TO SPRING FRAMEWORK

spring

In this article we will see what the Spring Framework is, clarify the concept of Framework, introduce the Spring Context essential element in development with this framework, and finally discuss Maven and the role it takes in Spring. The learning part will be accompanied with the development of the first, simple Maven project that will introduce us to Spring Context.

Cos'è spring framework

DEFINITION OF FRAMEWORK

We have said that Spring is an Application Framework, but what are frameworks? Let’s give a definition.

Cosa sono i Framework
Parti Comuni

The big advantage of frameworks is that they allow us to focus on the variable parts of the code, the so-called Business Logic that is part of what we have been commissioned to do. Other parts tend to stay the same so they just need to be adjusted. Spring is for all intents and purposes not just a framework but an ecosystem.

Ecosistema Spring

INTRODUCTION TO SPRING CONTEXT

Let us give a precise definition of what Spring Context is, an essential element in Spring-based projects.

Spring Context

To create a new project using Spring, you use the Apache Maven.

Progetto Maven

FIRST TEST WITH SPRING CONTEXT

Open Spring Tool Suite and let’s go create our first Maven project. In the Workspace create a new folder, I called it Spring Context and open it.

Once the project is created, we would have the structure of what is an Apache Maven-based project. Let’s analyze it.

Struttura progetto Maven
  • Inside the src/main/java folder should be put all the Java sources.
  • In src/main/resources there will go the configuration files.
  • The third group of folders concerns the Unit Test part; in fact, Spring allows us to create easily testable applications.
  • JRE in this case you are using a somewhat outdated version, let’s bring it up to version 17, right click on the file and Properties.
  • A very important file is the pom.xml, right now there is just the metadata that we have entered, but we have to work with the Spring Context; so, we create a dependencies node and go and type in Google “Maven Spring Context.”

Saving the file will download all dependencies within the Maven Dependencies library. At this point we are ready to work with context. We create a new package that we will call main inside the src/main/java folder. We create in this package a Java class that we will call Main and that will be the entry point of our app.

Main

NB: In the video I mistakenly wrote Cliente instead of Clienti.

We need to create a Clienti class, very simple that will contain only the name, instantiate it and put it inside the context. We create a new package, config with a new ConfigApp class inside. When we create a configuration class in Spring, we must decorate it with the @Configuration annotation.

I BEAN

We create a method that simply creates an instance of the Clienti class, sets a call sign, and returns the created instance. We need to make sure that this method is automatically started by Spring, to do this we decorate it with the @Bean notation, this will make sure to start the method in the context creation phase, but where should the context be created? The context should be created in the Main (entry point) class.

Main.java
ConfigApp.java

PROGRAM EXECUTION

To test this, right-click on the project->Run As->Java Application. The name set in the Clienti class setter will be printed on the console.

DOWNLOAD APPLICATION CODE

ITEM CODE

THE JAVA LANGUAGE

THE JAVA LANGUAGE

LINKS TO PREVIOUS POSTS

SPRING FRAMEWORK