To build the development environment of Spring Boot, and to further understand its characteristics, can better have a more in-depth introduction to Spring Boot. However, we need to build the Spring Boot project first anyway.

Build the Spring Boot development environment

To use Spring Boot, you first need to build a rapid development of the engineering environment. There are many ways to create a Spring Boot project, but due to the widespread use of Eclipse and IntelliJ IDEA, this book only introduces the construction of these two IDES.

                                                  

Build the Eclipse development environment

Start by going to the Eclipse menu Help→Eclipse Marketplace, which opens to a new window, and then select the TAB Popular to find plug-ins for Spring Tool Suite (STS), as shown in Figure 1-1.

By clicking on the STS plug-in, you can easily introduce the Spring Boot starter, and the starter will introduce the corresponding dependency packages and servers, which will help us quickly set up the development environment.

Let’s use it to create a project. First click the familiar menu File→New→Project, then input Spring to filter some irrelevant content, then select Spring Starter Project, click Next to create a Project, as shown in Figure 1-2.

It opens a new dialog box, as shown in Figure 1-3.

The box in Figure 2-3 is where I customized it to my own needs, and I chose to use packaging in the form of A War, which means a project with A JSP project will be used. In practice, readers need to define them according to their own circumstances. Once that’s done, you can click Next to proceed to the Next step, which will bring up another window, as shown in Figure 1-4.

             

Figure 1-1 Installing the STS plug-in

           

Figure 1-2 Creating a Spring Boot project

         

Figure 1-3 Configuring the Spring Boot project

         

Figure 1-4 Selecting a dependent starter

The choice of AOP and the Web here is just the simplest project, so it doesn’t introduce much. In the real development, may also need to choose NoSQL development tools, such as Redis, MongoDB, and database, such as MySQL, as well as persistent layer Hibernate or MyBatis and other project dependence, these are often used in the development. When you have selected the packages you need, click Finish and a new Spring Boot project is ready, as shown in Figure 1-5.

Figure 1-5 New Spring Boot project

As you can see from Figure 1-5, it is a Maven project where the pom.xml file is already built and gives us the chapter2Application.java file with the main method and the ServletInitializer.java file that initializes the Servlet. Here you can run the Spring Boot project with Chapter2Application. Now open the pom.xml file in the project again and you’ll see the code, as shown in Listing 1-1.

The code is the pom.xml file in the project in Listing 1-1

                                               

                                         

This code is created by the STS plug-in based on your chosen starter dependency, and the Eclipse setup is complete. To start the Spring Boot project, simply run Chapter2Application as a Java Application.

Set up IntelliJ IDEA development environment

Start the IntelliJ IDEA development environment and then select Create New Project to see a New window. We selected Spring Initializr and switched the JDK to the version you want, as shown in Figure 1-6.

Figure 1-6 Using IntelliJ IDEA to create a Spring Boot project

Clicking Next will also bring up another window that will allow us to do some configuration, as shown in Figure 1-7.

Figure 1-7 Configuring the Spring Boot project

Again, the boxes in the figure are the ones I modified to suit my own needs. Note that the War package is selected again, and then click Next to return to the starter selection window, as shown in Figure 1-8.

Figure 1-8 Selecting the corresponding starter

Similar to Eclipse, you can select a starter based on your requirements. IntelliJ IDEA also builds a project for you, as shown in Figure 1-9.

You can also see a pom.xml file for the built Chapter2Application, ServletInitializer, and Maven classes. Run Chapter2Application to start the Spring Boot project, and pom. XML configures your selected starter dependency so you can develop the Spring Boot project based on IntelliJ IDEA.

Figure 1-9 Intellij IDEA Creating a Spring Boot project

At this point, if you want to use Jetty or Undertow as the server, or if you want to switch trademarks in the background log, you can refer to the appendix.

Spring Boot

Dependency and automatic configuration

Now that you’ve seen how to set up the Spring Boot project in the previous section, you need to discuss why it works with very little configuration.

The following uses the most commonly used Spring MVC as an example. First open Maven’s local repository and find the corresponding Spring Boot folder. You can see the directory shown in Figure 1-10.

Figure 1-10 Maven repository for Spring Boot

We’ll talk about spring-boot-start-Web first, and we’ll talk about the spring-boot-Autoconfigure folder in the future, so we’ve added boxes to Figure 1-10. Open the spring-boot-start-web folder and you’ll see a file named spring-boot-starter-web-2.0.0.release.pom. Open it and you’ll see the code shown in Listing 1-2.

Listing 1-2 The pop.xml file for spring-boot-starter-web

                                                 

I added the Chinese comments in the code. As you can see, when spring-boot-starter-Web is added, it loads the corresponding resources into our project via Maven, so that dependencies can be formed. But that’s not enough to run the Spring MVC project, to run it you need to configure Spring MVC to produce the objects that Spring MVC needs in order to enable Spring MVC, so it needs to be explored further.

Spring-boot-autoconfigure = spring-boot-autoconfigure = spring-boot-autoconfigure = Spring-boot-autoconfigure = Spring-boot-autoconfigure = Spring-boot-autoconfigure = Spring-boot-autoconfigure

Spring – the boot – autoconfigure – 2.0.0. RELEASE – sources. The jar package. It is a source code package, and when you unzip it and open the subdirectories in its directory, you can see a number of configuration classes, as shown in Figure 2-11.

Figure 1-11 Default configuration classes of Spring Boot

Here you can see there are a lot of classes, one plus class DispatcherServletAutoConfiguration box is to automatically configure the DispatcherServlet classes. Since this is not a source code analysis book, annotations will not be discussed in depth, Captured only DispatcherServletAutoConfiguration source of an inner class DispatcherServletConfiguration to Spring the Boot automatically configured to do the most basic, as shown in listing 1-3.

Code Listing 1-3 part of the source code analysis

                                      

Note the bold comments in the code above. I added the Chinese comments to better illustrate Spring Boot’s auto-configuration capabilities. As you can see from the above code, Spring Boot already does a lot of automatic configuration for the DispatcherServlet. The @ EnableConfigurationProperties can also under the condition of the content of the reading configuration automatically generated classes needed for the Spring MVC, the discussion of the content you can refer to the appendix. At this point, it should be clear why Spring Boot can be used to start the Spring MVC project with almost no configuration. Spring Boot uses Maven dependencies to find the corresponding JAR packages and embedded servers, and then uses default auto-configuration classes to create the default development environment. But sometimes, we need to modify these default environment to meet the personalized requirements, in the Spring in the Boot is also very simple, as @ EnableConfigurationProperties annotation, it allows to read in the configuration file to customize the content of the automatic initialization required content, The next section explores this question.

Use custom configuration

The previous section discussed the existence of autowiring components and custom configurations for Spring Boot, which give developers default convention configuration items. About the content, can be announced on its web site to see all of the configuration items, http://docs.spring.io/spring- boot/docs/current – the SNAPSHOT/reference/htmlsingle / # appendix. These configuration items are as many as more than 300, so it is very complicated. Fortunately, we do not need to configure all of them. We just introduce the corresponding starter according to the needs of our own project and carry out the necessary configuration for it.

This book will not list these configuration items as a running list, because they are not interesting or necessary, but will only discuss the corresponding configuration items according to the needs of the explanation, introducing the corresponding stater. The corresponding configuration items will be discussed in the future when we discuss databases, NoSQL, and so on. The important thing to keep in mind here is that the configuration of these conventions allows you to largely customize your development environment to suit your real needs. This is the philosophy of Spring Boot, where configuration is as simple as possible and there are conventions, shielding the internal details of Spring so that it can be easily configured out of the box and used by developers for rapid development, deployment, and testing.

If you create a new project using Eclipse or IntelliJ IDEA as described above, you can find that it will also create a properties file for you, application.properties, as shown in Figure 2-12.

It is a default configuration file that you can customize according to your needs. For example, if port 8080 is currently occupied and we want to start Tomcat using port 8090, we simply add a line to the file:

Figure 1-12Spring Boot configuration file

Run Chapter2Application as a Java Application to see the Tomcat startup log bound to Spring Boot:

                     

Note that Tomcat is started on port 8090, as you can see from the bold log line. That is, we only need to modify the configuration file to change the development default configuration to a custom configuration.

In fact, Spring Boot parameters can be configured using yML files, etc. in addition to properties files, which are loaded in the following priority order:

Command line arguments;

JNDI properties from Java :comp/env;

Java System properties (system.getProperties ());

Operating system environment variables;

RandomValuePropertySource configuration of the random. * attribute value;

Application -{profile}. Properties or application. Yml (with spring.profile) configuration file outside the JAR package;

Application -{profile}. Properties or Application.ym (with spring.profile) configuration file inside jar package;

Properties or application.yml (without spring.profile) configuration file outside the JAR package;

Properties or application.ym (without spring.profile) configuration files inside jar packages;

@Configuration annotation class @propertysource;

In fact, the configuration of yML files and properties files are just shorthand and indentation differences, so the differences are not significant, so this book uses properties files for configuration. For readers who want to use YML files, only minor changes are required.

Develop your own Spring Boot project

Above we changed the server’s startup port, and sometimes the Spring MVC ViewResolver. The main purpose of the View parser in Spring MVC is to locate the view. When the controller returns a logical name, there is no way to directly locate the view, which requires the view parser to resolve. One of the most commonly used views in real development is JSP. For example, if a string “index” is now returned in the controller, we want it to correspond to the /WEB-INF/ JSP /index.jsp file of the development project. It doesn’t matter if you’re not familiar with Spring MVC, we’ll talk about it in the future, but the code here is very simple and you just need to follow the rules to get a taste of running a Spring Boot project. The main task below is how to do this via Spring Boot. First we need to add the JSP and JSTL dependencies to Maven’s POM.xml, as shown in Listing 1-4.

Listing 1-4 adds the Maven dependency configuration for JSP and JSTL

                       

To configure the ViewResolver, modify the application.properties file as shown in Listing ‘-5.

Listings 1-5 define the view prefix and suffix

                       

Prefix and spring.mvc.view.suffix are the view prefix and suffix configuration agreed by Spring Boot with us, which means to find the JSP file with the suffix.jsp under the folder/web-INF/JSP /. There is obviously a missing file name between the prefix and suffix, which in the Spring MVC mechanism is given by the Controller, so create a new Controller, IndexController, as shown in Listing 1-6.

Listing 1-6 develops the controller

                       

Here we define a path that maps to /index, and then the method returns “index”, so that it combines the prefix and suffix configured earlier to find the corresponding JSP file. To do this, we also need to develop a corresponding JSP file, To do this, we’ll create another /webapp/WEB-INF/ JSP /index.jsp file, as shown in Listing 1-7.

Listing 1-7 Developing the view (/webapp/WEB-INF/ JSP /index.jsp)

                       

So we have a simple controller and let the view parser find the view’s functionality. To define a view parser from above, in Spring Boot you only need to define the prefix and suffix of the view parser through the configuration file, without any code. This is because Spring Boot gives us custom configuration items, and it reads these custom configuration items and generates the view parser for us in Spring MVC. Configure the Spring development environment as much as it promises, and then see that you are about to run the chapter2Application.java file, as shown in Listing 1-8.

Listing 1-8Spring Boot runs the file Chapter2Application

                      

The annotation @SpringBootApplication here indicates that this is a SpringBoot starter file. The code in bold runs the Spring Boot project using the Chapter2Application class as the configuration class, and Spring Boot will run based on the dependencies you loaded in Maven. Next we run the Chapter2Application class as a Java Application to see the Tomcat run logs. Because the port has been revised to 8090, enter http://localhost:8090/index after open the browser, can see the results of operation as shown in figure 1 to 13.

                                                

Figure 1-13 Testing the view parser

In this way, we have built the development environment of Spring Boot. Because Spring Boot is based on the Spring principle, it is necessary to introduce the technical principle of Spring when discussing Spring Boot, so as to understand the reason and why. These are tasks for subsequent chapters.

                                                       

​Spring Boot 2.x

Yang open vibration

The Spring framework is the de facto standard for Java EE development, and Spring Boot is the industry’s most popular microservices development framework. This book starts with a simple project to explain Spring Boot enterprise development, Its content includes Fully annotated Spring IoC and AOP, database programming, database transactions, NoSQL technology, Spring MVC, Spring 5 new generation of responsive framework WebFlux, REST style and Spring Cloud distributed development, etc.

                                               

Scan for us

If you reply “follow” in the background of “Asynchronous Community”, you can get 2000 online video courses for free