So, rather than allowing each component to create new instances of the ones they rely on, Spring’s container creates and maintains an instance of each of the components it hosts. This container then injects an instance of a component into all the other components that need it.

Exploring Spring’s Core Features

The Spring framework has around 20 well-organized modules that play an integral role in its ability to create and manage applications. Based on the primary features of these modules, you can group them into five categories:

Core Container Data Access/Integration Web Aspect Oriented Programming and Instrumentation Test

Spring Container

Spring uses a container to create and manage different components (which it calls beans) of an application. The container is accessible through Spring’s ApplicationContext interface, which allows you to instantiate, configure, and assemble objects (or beans). Spring’s container can perform its function due to the aid of configuration metadata.

This configuration metadata can have several different forms, but its typical form is XML. In addition to beans, Spring’s container also consists of a core, context, and an expression module.

Spring Data Access/Integration

The data access/integration aspect of the spring framework consists of several submodules: JDBC, ORM, OXM, JMS, and transaction. These modules work together to manage any data that your application uses.

Spring Web

The web feature of the Spring application provides web-oriented integration for your application. In addition to web-oriented integration, Spring provides a web servlet module. The web servlet module is responsible for Spring’s model-view-controller (MVC) implementation.

Spring Aspect-Oriented Programming (AOP)

Spring’s AOP framework is another one of its key components. Though the AOP framework is not crucial to Spring’s container function, it does complement the container by being a capable middleware solution. One of the AOP framework’s key features is that it provides declarative enterprise services for your application, namely declaration transaction management.

Declarative transaction management is a desirable approach to software development because it has minimal impact on application code.

Spring Test

Spring places significant importance on application testing. Its test module supports integration testing and unit testing using JUnit or TestNG.

Creating a Spring Application

There are several ways to create the boilerplate for a new Spring application. The various approaches include:

From the web application (or Spring Initializr) at Spring’s official website. Manually creating a project directory structure and build specifications. With the Spring Tool Suite. From the command line with Spring Boot command-line interface. With the Eclipse IDE. With the IntelliJ IDEA IDE.

The most popular approach is the Spring Initializr:

As you can see from the image above, you’ll have several decisions to make. The first is the type of project you want to initialize. Spring provides three options: Gradle for Groovy, Gradle for Kotlin, or Maven. This sample application will use a Maven project.

The other options you must select are the language (Java), the version of Spring Boot you want to use, and the project metadata. Spring already has some dummy data in its five fields that will help you to create a sample application. However, you will need to update these fields with project-specific data, as this metadata will describe your Spring application including Spring beans and their methods.

Next, you’ll need to select the packaging you want your files in (jar or war). Then you can select the version of Java you have on your device. The final decision you’ll need to make is the type of dependencies you want for your application.

Spring provides several dependencies that you can use to add properties to your application. To add a new dependency to your application, simply click the add dependencies button to the right of the Spring Initializr. This action will generate the following overlay on your screen:

For this sample application, the only dependency you will need is Spring Web. This dependency allows you to build web applications using the MVC architecture. After you’ve selected all the dependencies you wish to use in your application, go ahead a click generate. This will download the project boilerplate for you.

Now you have a Spring Boot (which is one of Spring’s libraries) project. The final step is to import the project into your preferred IDE. Your Spring project will have the following project structure:

This is a normal Maven project structure, and there are several important files that you need to become familiar with.

Pom. xml: This contains the Spring application configuration data. OnlineShopaholicsApplication. java: This is the Spring Boot main class, which executes the application. OnlineShopaholicsApplicationTest. java: This is a JUnit test class that ensures that the Spring application context (or container) loads correctly.

Running a Spring Application

Spring’s bootstrap class, which in this case is the OnlineShopaholicsApplication class, has the following initial code:

One of the most important aspects of the code above is the @SpringBootApplication annotation, which is a composite annotation that allows your application to access the features of three other annotations:

@EnableAutoconfiguration: This annotation enables Spring Boot automatic configuration. This means that it tells Spring boot to configure any component that it thinks your application will need to function. @ComponentScan: This annotation allows you to enable component scanning. This feature is important, it allows Spring to automatically discover and register other annotated components for use by the Spring application context (or container). @SpringBootConfiguration: This is a specialized form of the @Configureation annotation, which allows you to register beans (or objects) in the Spring application context.

The other important aspect of the code above is the main() method, it executes your application. When the application executes now, it simply starts the server. So, if you want to add more functionality to the application, you’ll need to update the code:

The Spring Boot application now has three new annotations:

@RestController: This annotation marks the class as a controller, so every method in the class above (apart from the main()) will return a domain object instead of a view. @GetMapping: This annotation maps HTTP GET requests onto specific header methods. So, each time you create a request for “/customer” in the browser, the Welcome() method will handle the request by returning a string value. @RequestParam: This annotation indicates that a method parameter should be bound to a web request parameter.

With the updated code your Spring OnlineShopaholicsApplication now has a customer page that you can view in your browser. Executing the application will start the server and produce output in the console.

There is a lot of important information in the output. It tells you that the server is running, it tells you how long the initialization process took, and it tells you what port the application is running on (8080, by default). Therefore, if you navigate to http://localhost:8080/customer you will see the following output in your browser:

Now You Can Design Your Application View

Another important file in the project structure that you might notice is the templates folder. Ideally, the OnlineShopaholicsApplication class should serve as a controller, which handles requests for different views. Therefore, in a complete application, the controller class should not contain the text that will display in the view of its request.

Instead, you will need to create template files using HTML and place them within the templates folder of your Spring application.