How to run the project

  1. Extract the project ZIP package that we created in the previous topic and open Eclipse.
  2. We are going to import our project into Eclipse IDE. To start the import process, select the File | Import menu and the import wizard will be opened. The following screenshot shows the first page of the wizard:
  1. In the first phase, you should select Existing Maven Projects from the list under the Maven folder, and then go to the next phase by pressing the Next button. The following screenshot shows the second step of the import wizard:
  1. In this phase, select the extracted project folder by pressing the Browse... button. Then, Eclipse finds the pom.xml file from the root of your project folder and shows it inside the Projects section of the window.

 

  1. Press the Finish button to finalize the import. If everything went correctly, you should see the cardatabase project in Eclipse Project Explorer. It takes a while when the project is ready because all dependencies will be loaded by Maven after import. You can see the progress of the dependency download at the bottom-right corner of Eclipse. The following screenshot shows Eclipse Project Explorer after successful import:

The Project Explorer also shows the package structure of our project, and now at the beginning there is only one package called com.packt.cardatabase. Under that package is our main application class, called CardatabaseApplication.java.

  1. Now, we don't have any functionality in our application, but we can run it and see whether everything has started successfully. To run the project, open the main class by double-clicking on it and then pressing the Run button in the Eclipse toolbar, or select the run menu and press Run as | Java Application:

You can see the Console view opening in Eclipse, and that contains important information about the execution of the project. This is the view where all log texts and error messages appear, and it is therefore really important to check the content of the view when something goes wrong.

Now, if the project was executed correctly, you should see the text Started CardatabaseApplication in... at the end of the console. The following screenshot shows the content of the Eclipse console after our Spring Boot project has been started:

In the root of our project there is the pom.xml file, that is the Maven configuration file for our project. If you look at the dependencies inside the file, you can see that there are now dependencies that we selected on the Spring Initializr page. There is also a test dependency included automatically without any selection. In the next chapters, we are going to add more functionality to our application, and then we will add more dependencies manually to the pom.xml file:

  <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

Let's look at the Spring Boot main class more carefully. At the beginning of the class, there is the @SpringBootApplication annotation. It is actually a combination of multiple annotations, such as, the following:

Annotation Description
@EnableAutoConfiguration Enables Spring Boot automatic configuration. Spring Boot will automatically configure your project based on dependencies. For example, if you have the spring-boot-starter-web dependency, Spring Boot assumes that you are developing a web application and configures your application accordingly.
@ComponentScan Enables the Spring Boot component scan to find all components from your application.
@Configure Defines the class that can be used as a source of bean definitions.

 

The following code shows the Spring Boot application's main class:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CardatabaseApplication {

public static void main(String[] args) {
SpringApplication.run(CardatabaseApplication.class, args);
}
}

The execution of the application starts from the main method, as in standard Java applications.

It is recommended to locate the main application class in the root package above other classes. Quite a common reason for an application to not work correctly is due to a situation where Spring Boot can't find some critical classes.
..................Content has been hidden....................

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