Java for Spring and Spring Boot
I am about to start a new journey working with Swisscom, the largest employer in the country, and therefore need to optimize my skillset to suit the position of a Java Software Engineer (to add some context: API management and systems integration for client software in the Swiss banking industry). Although I have studied and worked with Java previously new frameworks and technologies have come up since then. Spring Boot is replacing Spring. JMS, Kafka, Azure, Docker and Kubernetes are now being fervently used in the industry.
These are all buzzwords to me and the volume of information is massive! So I got to buck up and study! To start with I have to devoted myself to learning Spring and Spring Boot. To this end I picked the online platform Codecademy since the course content seemed perfect to me! Codecademy has an interactive learning engine that lets you just type and get immediate feedback in the browser. You learn new concepts and program simple exercises as you learn them.
Fast forward 4 weeks I am done with the course. In this repo I am sharing a summary of the most important theoretical concepts I have learnt.
Let's get started!
HTTP and TCP
HTTP stands for Hypertext Transfer Protocol and is used to structure requests and responses over the internet. HTTP requires data to be transferred from one point to another over the network.
The transfer of resources happens using TCP (Transmission Control Protocol). In viewing a webpage, TCP manages the channels between your browser and the server. TCP is used to manage many types of internet connections in which one computer or device wants to send something to another. HTTP is the command language that the devices on both sides of the connection must follow in order to communicate.
Back-end and front-end
In order to deliver the front-end of a website or web application to a user, a lot needs to happen behind the scenes on the back-end! Understanding what makes up the back-end can be overwhelming because the back-end has a lot of different parts and different websites or web applications can have dramatically different back-ends.
What is Spring? What is Spring Boot?
Spring is an open-source Java framework that is useful, among other things, for building Java-based enterprise applications. Spring contains templates for many different kinds of applications, or "Spring Projects", (including Spring Cloud, Spring Web Services, Spring Security, etc.). Spring Boot is one of the easiest ways to build Spring web projects, enabling applications to run with minimal configuration and code.
Spring Boot projects can be instantiated under Spring initializr which allows you to define your Java version, build tool and dependencies, and download a starter template for your Spring project. Maven is a developer tool that simplifies building and running a Java project, as well as managing dependencies. To view a Maven project's configuration, build profiles, and dependencies you must look into the pom.xml or Project Object Model file. Spring projects are often organized using the MVC framework. Spring projects contain annotations (e.g. @GetMapping, @PostMapping, @RestController). These annotations are built-in units of Spring code that make complex functionality readily available in Spring applications.
So how do all of these different files and annotations work together to build and run a Spring application? To put it simply, Spring Boot checks pom.xml and downloads its various dependencies, reads through all the annotations to determine how the code should function, and looks for the main method in the [ProjectName]Application.java file to run the application.
Recommended by LinkedIn
What is REST?
Going forward, the Spring framework is an incredible resource to quickly build RESTful applications. The core functionality of these applications lies in their ability to receive requests, route them accordingly and provide a proper response. Spring implements all of these key operations with simple, built-in annotations.
The term REST stands for REpresentational State Transfer and it describes the architectural technique used to facilitate communication between different systems via the web. Stateless requests are sent from a client to either retrieve or make changes to a resource stored on a server. The server provides a response based on the information received in the request. More specifically, the server maps the request to an endpoint like localhost:4001/restaurants and sends or updates data in response to requests sent to those endpoints.
In Spring, this is handled using a class and its methods. Such a class is called a controller and each of its methods is associated with an endpoint. If the endpoint is requested over HTTP the respective method is called to formulate a response to this particular request.
Dependency Injection and Inversion of Control
Spring implements a way for the objects needed by another object to be provided as beans for others to reference. This process is known as dependency injection. Our classes no longer have to instantiate their own dependencies.
The Spring framework defines a Spring bean as an object managed by the Spring Inversion of Control (IoC) container. Normally, instantiating an object from a class requires the use of the new keyword. Spring does away with this repetitive task where we are able to declare and use an instance without instantiating it. How? We mark our dependent classes as Spring beans, which allows the IoC container to instantiate them and inject them into the class where they are needed. Therefore, we can say the control of dependencies has been inverted back to the container, and this is why we call it an Inversion of Control (IoC) container.
This is done using the @Component annotation on the Spring bean classes. We remove the actual instantiation code, instead using the @Autowired annotation. When our application starts up the container scans our code for components from which beans should be instantiated. The container takes a look at our classes and, depending on the method you choose, instantiates beans from the referenced objects before it instantiates a bean from the outer classes.
Data with JPA
Once you have developed an understanding of how Spring and Spring Boot work there are lots of ways to build more functionality into your API or web application. One of the most common and powerful enhancements you can make to a simple application is to add a database. There are many different databases that Spring Data JPA can work with. H2 is one of those. It differs from the other databases in that it can run in-memory, meaning that developers don't have to set up and connect to a separate database to get their app working.
With Spring, adding a database is easy to do with the help of a library called Spring Data JPA. JPA stands for Java Persistence API. The JPA facilitates Java development against a database, by providing a framework to map Java classes to an underlying database table. When developers build APIs that interact with or manage an underlying data model, there are usually some common functionalities that they want to enable. An API that manages a data model should be able to Create, Read, Update and Delete instances of the model. For this reason, these kinds of APIs are called CRUD APIs. Since this kind of functionality is so common Spring Data JPA comes with a special kind of repository interface that gives you full CRUD functionality for your model.
The data model emerges from your design process of representing the important objects, their attributes, and their relationships with one another, which then helps inform you on your database design.
Closing remarks
I recommend this course for the same reason that I took it: learning the basics. Spring 5.0 was released in 2017. Whenever a new Spring framework version is released, it will trigger a new Spring Boot release. This will in turn trigger a new Spring release. These are two recent trends with no applications in academia. Therefore, no relevant courses are taught at university and there only remains the option of self-study. Your interest will develop the more you understand, starting with the fundamentals that are taught in this very course. The descriptions are informative and the illustrations very helpful. You are also more likely to look for flaws in your program at the right place and be efficient if you understand things from the ground up. All the best!