Getting familiar with Spring Boot

Let's take a look at the structure of the initial program files for this Spring Boot application. The following is the screenshot of the structure of your Spring Boot application files:

All source files are contained in the src directory. This directory contains the core application program files as well as the test programs that are written for the application. Core application program files should be put in the src/main directory and test programs are located in src/test. The main directory contains two subdirectories. These are the kotlin directory and the resources directory. All packages and main source files will be placed in this directory over the course of this chapter. More specifically, our program files and packages will be placed within the com.example.messenger.api package. Let's have a quick look at the MessengerApiAplication.kt file:

The MessengerApplication.kt file contains the main function. This is the entry point of every Spring Boot application. This function is called when the application starts. Once it is called, the function runs the Spring application by calling the SpringApplication.run() function. This function takes two arguments. The first argument is a class reference and the second is the arguments to be passed to the application upon start.

In the same file, there's a MessengerApiApplication class. This class is annotated with the @SpringBootApplication annotation. The use of this annotation is equivalent to the combined use of the @Configuration, @EnableAutoConfigurationand @ComponentScan annotations. Classes annotated with @Configuration are sources of bean definitions.

A bean is an object that is instantiated and assembled by a Spring IoC container.

The @EnableAutoConfiguration attribute tells the Spring Boot that you want your Spring application to be automatically configured based on the jar dependencies that you have provided. The @ComponentScan annotation configures component scanning directories for use with the @Configuration classes.

Over the course of developing Spring Boot applications, it will be necessary to use several annotations for varying reasons. Using these annotations may be overwhelming at first, but with time they will become second nature.

Besides MessengerApplication.kt, another important file is the application.properties file, located in src/main/resources. This file is used for configuring Spring Boot applications properties. Upon opening this file, you will discover that it has no content. This is because we have not yet defined any application configurations or properties. Let's go ahead and add a couple of configurations. Input the following into the application.properties file:

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop

The spring.jpa.generate-ddl property specifies whether the database schema should be generated upon startup of the application. When this property is set to true, the schema is generated on application startup, otherwise the schema is not generated. The spring.jpa.hibernate.ddl-auto property is used to specify the DDL mode. We use create-drop because we want the schema to be created upon application startup and destroyed upon the termination of the application.

We have utilized properties to define the schema of our database but have yet to create an actual database for the messenger-api. If you installed pgAdmin along with PostgreSQL, you can easily create a database with the software. If you didn't install pgAdmin, fear not, we can still easily create a database for our application by using PostgreSQL's createdb command. Navigate to your terminal and enter the following command:

createdb -h localhost --username=<username> --password messenger-api

The -h flag is used to specify the host name of the machine on which the database server is running. The --username flag specifies the username to connect to the server with. The --password flag forces a prompt for the specification of a password. The messenger-api is the name we are giving to the database being created. Substitute <username> with your server username. After you've input the command, click the enter key to run the command. Input a desired password when prompted to do so. A database named messenger-api will be created in PostgreSQL.

Now that we have out database set up, we need to connect the Spring Boot application to the database. We can do this with the use of the spring.datasource.url, spring.datasource.username, and spring.datasource.password properties. Add the following configurations to the application.properties file:

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:postgresql://localhost:5432/messenger-api
spring.datasource.username=<username>
spring.datasource.password=<password>

The spring.datasource.url property specifies the JDBC URL via which Spring Boot will connect to the database. The  spring.datasource.username and spring.datasource.password are the properties used to specify the server username and the password correlating with the specified username. Replace <username> and <password> with your username and password.

Once you have these properties set up, you are ready to start the Spring Boot application. 

Once you have these properties set up, you are ready to start the Spring Boot application. You can run the messenger-api application by clicking on the Kotlin logo next to the main function in MessengerApiApplication.kt and selecting the Run option, as shown in the following screenshot:

Wait for a moment for the project to build. Once the project build process is complete, the application will be started on a Tomcat server.

Let's continue exploring our project files. Locate a pom.xml file in the root directory of the project. POM stands for Project Object Model. On the Apache Maven site, the following is said about the POM: A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. Once you have located this file, open it. Straightforward, right? That’s all fine and dandy, but, for the sake of clarity, here's a brief description of Maven.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.180.43