How to run the project

Perform the following steps to run a Maven project in the Eclipse IDE:

  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 the 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.
  2. Press the Finish button to finalize the import. If everything went correctly, you should see the cardatabase project in the Eclipse Project Explorer. It takes a while before the project is ready because all the dependencies will be loaded by Maven after importing. You can see the progress of the dependency download at the bottom-right corner of Eclipse. The following screenshot shows the Eclipse Project Explorer after a 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 open 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, so it is really important to check the content of the view when something goes wrong.

Now, if the project was executed correctly, you should see the Started CardatabaseApplication in... text 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, which 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 following 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 This 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 This enables the Spring Boot component scan to find all the components of your application.
@Configure This 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 that you locate the main application class in the root package above other classes. A common reason for an application to not work correctly is due to Spring Boot being unable to 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
18.191.194.123