Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build SpringBoot quickly

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to build SpringBoot quickly. It is very detailed and has certain reference value. Friends who are interested must finish reading it.

Overview of Spring Boot

Build Anything with Spring Boot:Spring Boot is the starting point for building all Spring-based applications. Spring Boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of Spring.

The above is a quote from the official website, which roughly says: Spring Boot is the starting point for all Spring-based projects. Spring Boot is designed to allow you to run Spring applications as fast as possible and to minimize your configuration files.

What is Spring Boot?

It uses the concept of "habit over configuration" (there are a lot of configurations in the project, and a habitual configuration is built in so that you don't have to) to get your project up and running fast.

It is not a new framework, but many frameworks are configured by default. Just as Maven integrates all jar packages, Spring Boot integrates all frameworks (quoted from: springboot (1): introduction-Pure smile -% E5%85%A5%E9%97%A8%E7%AF%87.html))

What are the benefits of using Spring Boot

Reviewing our previous SSM project, the building process is rather tedious, which requires:

1) configure web.xml to load spring and spring mvc

2) configure database connection and configure log files

3) Configurator reads the configuration file and opens the comments

4) configure mapper file

... ..

Using Spring Boot to develop a project requires only a few configurations to build a Web project, and using IDEA can automatically generate generation, which is simply too cool.

Focus: build the project simply, quickly and conveniently; integrate the mainstream development framework without configuration; greatly improve the efficiency of development and deployment.

The first step of Spring Boot Quick Building: create a new project

Select Spring Initializr, then select the default url and click [Next]:

Then modify the project information:

Check the Web template:

Select the location of the project, and click [Finish]:

If this is the first time to configure Spring Boot, you may need to wait for IDEA to download the corresponding dependency package. The project structure created by default is as follows:

The project structure still looks refreshing, with a lot less configuration files, so let's take a look at what is generated by default:

SpringbootApplication: a class with the main () method that starts the application

SpringbootApplicationTests: an empty Junit test that loads a Spring application context that uses the Spring Boot dictionary configuration feature

Application.properties: an empty properties file that can add configuration properties as needed

Pom.xml: Maven build instruction file

Step 2: HelloController

Create a new [HelloController] under the [cn.wmyskxz.springboot] package:

Package cn.wmyskxz.springboot;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * Test Controller * * @ author: @ I don't have three hearts * @ create: 2018-05-08-16:46 * / @ RestControllerpublic class HelloController {@ RequestMapping ("/ hello") public String hello () {return "Hello Spring Boot!";}}

@ RestController comment: this annotation is a combined version of @ Controller and @ ResponseBody annotations

Step 3: start Spring Boot with IDEA

Let's go back to the SpringbootApplication class and right-click run:

Note: the reason why we did not manually configure the Tomcat server in the above project is that Spring Boot has built-in Tomcat

Wait a while and you will see the following prompt for a successful run:

You can see that our Tomcat is running on port 8080. Let's visit the "/ hello" address and try it:

You can see that the page successfully displays the information we returned.

Parsing Spring Boot Project

This part is referenced from: Spring Boot practical information series (1) elegant introduction-Doodle Independent blog

Parsing pom.xml files

Let's take a look at what's special about the default generated pom.xml file:

4.0.0 cn.wmyskxz springboot 0.0.1-SNAPSHOT jar springboot Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web Org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin

We can see a relatively unfamiliar tag that is configuring Spring Boot's parent dependencies:

Org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE

With this, the current project is the Spring Boot project, and spring-boot-starter-parent is a special starter that provides the relevant Maven default dependencies, and after using it, the commonly used package dependencies can omit the version tag.

For the dependencies of which jar packages are provided by Spring Boot, you can check the local Maven repository:\ repository\ org\ springframework\ boot\ spring-boot-dependencies\ 2.0.1.RELEASE\ spring-boot-dependencies-2.0.1.RELEASE.pom file. It's quite long.

Application entry class

Spring Boot projects usually have an entry class called Application and there is a main method in the entry class. * this main method is actually a standard entry method for Javay applications.

@ SpringBootApplication is the core annotation of SpringBoot, which is a combination of @ Configuration, @ EnableAutoConfiguration, and @ ComponentScan;. You can use these three annotations instead of @ SpringBootApplication annotations.

@ EnableAutoConfiguration allows Spring Boot to automatically configure the current project according to the jar package dependencies in the classpath. For example, if you add spring-boot-starter-web dependencies, you will automatically add Tomcat and Spring MVC dependencies, then Spring Boot will automatically configure Tomcat and Spring MVC.

SpringBoot also automatically scans the sibling package of the class in which @ SpringBootApplication belongs and the Bean in the subordinate package, so the entry class is recommended to be configured under the package name of the combination of grounpID and arctifactID (here is the cn.wmyskxz.springboot package)

Configuration file for Spring Boot

Spring Boot uses a global configuration file application.properties or application.yml, placed in the [src/main/resources] directory or under / config in the classpath.

Spring Boot supports not only regular properties configuration files, but also yaml language configuration files. Yaml is a data-centric language with object-oriented features when configuring data.

The global configuration file of Spring Boot is used to modify the configuration values of some default configurations.

Give me a simple example.

We also set the default port of Tomcat to 8080 and change the default access path from "/" to "/ hello". The difference between using properties files and yml files is shown in the figure above.

Note: yml needs to add a space after ":". Fortunately, IDEA well supports the format of yml files and has good code hints.

We can configure multiple properties ourselves.

We delete the file with the .properties suffix directly, use the .yml file for simple configuration, and then use @ Value to get the configuration properties:

Restart Spring Boot and enter the address: localhost:8080/hello to see the correct result:

Note: we do not specify the type of attribute in the yml file, but define it when we use it.

You can also use the current configuration in the configuration file:

You can still get the right results:

Problem: writing the configuration file this way is cumbersome and can lead to bloated classes, because there are so many @ Value annotations.

Encapsulate configuration information

We can encapsulate the configuration information into a class, first prefix our name and age with a student, and then create a new StudentProperties class to encapsulate the information with two annotations:

@ Component: indicates that the current class is a Java Bean

@ ConfigurationProperties (prefix = "student"): indicates to get the configuration information prefixed with sutdent

So we can use it in the controller and restart to get the correct information:

Hot deployment of Spring Boot

In the current Spring Boot project, when any changes have taken place, we need to restart to get the correct results, which will be a little troublesome. Spring Boot provides a way of hot deployment. When it is found that any class has changed, it will load the latest classes into the virtual machine through the JVM class load, so that you can see the effect of the changes without reboot.

The practice is also very simple, just modify the pom.xml!

Let's just add a dependency to pom.xml:

Org.springframework.boot spring-boot-devtools true

Restart Spring Boot, then modify any code, and you can observe the automatic restart of the console:

About how to configure hot deployment in IDEA: portals

Spring Boot usage

The above has completed the simple construction of the Spring Boot project, we just need to make some simple settings, write a HelloController to run directly, not too simple … Let's take a closer look at the use of Spring Boot.

Spring Boot supports JSP

The default view support for Spring Boot is the Thymeleaf template engine, but we are not familiar with this. What if we still want to use JSP?

Step 1: modify pom.xml to add support for JSP files

Javax.servlet javax.servlet-api provided javax.servlet jstl org.apache.tomcat.embed tomcat-embed-jasper provided

Step 2: configure the location where you are trying to redirect the JSP file

Modify the application.yml file to redirect our JSP file to the / WEB-INF/views/ directory:

Step 3: modify HelloController

Change the @ RestController annotation to @ Controller, and then change the hello method to:

Step 4: create a new hello.jsp file

Create a webapp, WEB-INF, views directory under the [src/main] directory, and create a hello.jsp file:

Step 5: refresh the web page

Because we have deployed the hot deployment feature, we only need to wait for the console restart message to complete and then refresh the web page to see the correct effect:

With regard to 404, running the project using spring-boot:run can solve the problem:

Integrated MyBatis

Step 1: modify pom.xml to increase support for MySql and MyBatis

Org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 mysql mysql-connector-java 5.1.21

Step 2: add database link parameters

Let's just use the student table we created earlier:

Step 3: create Student entity classes and StudentMapper mapping classes

Create a new [pojo] package under [cn.wmyskxz.springboot], and then create a Student class under it:

Public class Student {private Integer id; private Integer student_id; private String name; private Integer age; private String sex; private Date birthday; / * getter and setter * /}

Create a new [mapper] package under [cn.wmyskxz.springboot], and then create a StudentMapper mapping class under it:

Package cn.wmyskxz.springboot.mapper;import cn.wmyskxz.springboot.pojo.Student;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;import java.util.List;@Mapperpublic interface StudentMapper {@ Select ("SELECT * FROM student") List findAll ();

Step 4: write StudentController

Create a new [controller] package under [cn.wmyskxz.springboot], and then create a StudentController under it:

Package cn.wmyskxz.springboot.controller;import cn.wmyskxz.springboot.mapper.StudentMapper;import cn.wmyskxz.springboot.pojo.Student;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import java.util.List / * * Student controller * * @ author: @ I don't have three hearts * @ create: 2018-05-08-20:25 * / @ Controllerpublic class StudentController {@ Autowired StudentMapper studentMapper; @ RequestMapping ("/ listStudent") public String listStudent (Model model) {List students = studentMapper.findAll (); model.addAttribute ("students", students); return "listStudent";}}

Step 5: write the listStudent.jsp file

Let's simplify the JSP file to show only two fields of data:

Id name ${s.id} ${s.name}

Step 6: restart the server

Since a new dependent package has been added to pom.xml, automatically restarting the server does not work. We need to restart it manually, and then enter: localhost:8080/listStudent at the address to check the effect:

Above.

The difference between springMVC and springboot

The Spring framework is like a family, with many derivatives such as boot, security, jpa, and so on. But their foundation is Spring's ioc and aop ioc provide dependency injection container aop, solve the cross-section-oriented programming, and then implement the advanced functions of other extended products on the basis of both. Spring MVC is a MVC framework based on Servlet that mainly solves the problem of WEB development, because the configuration of Spring is very complex, and it is cumbersome to deal with all kinds of XML, JavaConfig and hin. So in order to simplify the use of developers, and thus creatively launched Spring boot, convention is better than configuration, simplifying the configuration process of spring.

To put it more simply: Spring initially used "DI" and "AOP" to decouple application components. Everyone thought it was very useful, so they built a MVC framework (some components decoupled by Spring) according to this model, and used to develop web applications (SpringMVC). Then it is found that each development writes a lot of boilerplate code, in order to simplify the workflow, so developed some "lazy integration package" (starter), this set is Spring Boot.

The function of Spring MVC

Spring MVC provides a mildly coupled way to develop web applications.

Spring MVC is a module of Spring, a web framework. It is easy to develop web applications through Dispatcher Servlet, ModelAndView and View Resolver. The problem areas to be solved are web application or service development-URL routing, Session, template engine, static Web resources, and so on.

The function of Spring Boot

Spring Boot implements automatic configuration, which reduces the complexity of project construction.

It is well known that the Spring framework requires a lot of configuration, and Spring Boot introduces the concept of automatic configuration to make project setup easy. Spring Boot itself does not provide the core features and extension functions of the Spring framework, but is used to quickly and agile develop a new generation of applications based on the Spring framework. In other words, it is not an alternative to Spring, but a tool that works closely with the Spring framework to enhance the experience of Spring developers. At the same time, it integrates a large number of commonly used third-party library configurations (such as Jackson, JDBC, Mongo, Redis, Mail, etc.), Spring Boot applications in these third-party libraries can be almost zero configuration out-of-the-box (out-of-the-box), most Spring Boot applications need only a very small amount of configuration code, developers can be more focused on business logic.

Spring Boot is just a carrier to help you simplify the project building process. If you are hosting a WEB project and use Spring MVC as the MVC framework, then the workflow is exactly the same as what you described above, because this part of the work is done by Spring MVC rather than Spring Boot.

For users, after switching to Spring Boot, the project initialization method has changed, and the configuration file has changed. In addition, there is no need to install container servers such as Tomcat separately. Maven runs directly into a jar package, but there is no change in your core business logic implementation and business process implementation.

So, in the simplest terms, it is:

Spring is an "engine"

Spring MVC is a MVC framework based on Spring

Spring Boot is a set of rapid development integration packages based on Spring4 conditional registration.

These are all the contents of the article "how to build SpringBoot quickly". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report