


How to solve the problems of SpringBoot official website construction and quick startup
SpringBoot Overview
SpringBoot
is a brand new framework provided by the Pivotal team and is designed to be used with To simplify the initial construction and development process of Spring applications.
Everyone has experienced the SpringBoot
program. Let’s look back and see what the main function of SpringBoot
is to simplify the construction and development process of Spring
.
OriginalSpring
There are the following problems in environment construction and development:
Cumbersome configuration dependencies
Settings Cumbersome
SpringBoot
The advantage of the program happens to be automatic configuration for the shortcomings of Spring
. This is used to solve the problem of cumbersome program configuration in
Spring
- ##starting dependencies. This is used to solve
Spring
the problem of cumbersome program dependency settings
- auxiliary functions (built-in server,...). When we started the
SpringBoot
program, we neither used the local
tomcatnor the
tomcatplug-in, but used the
SpringBootbuilt-in server.
SpringBoot
Spring Initializr The
pom.xml configuration file of the
Maven project created in the
method automatically generates many dependencies containing starter
, as shown below
These dependencies are startup dependencies. Next, let’s explore how they are implemented.
Exploring the parent project
From the above file, we can see that a parent project is specified. We enter the parent project and find that another parent project is specified in the parent project, as shown in the figure below
Then enter the parent project. In this project, we can see the configuration content structure as shown below
The properties
tag in the above picture defines the versions that each technical software depends on, which avoids us considering version compatibility issues when using different software technologies. In properties
we find the versions of servlet
and mysql
as shown below
dependencyManagement# The ## tag is for dependency version locking, but the corresponding dependency is not imported; if our project needs that dependency, we only need to introduce the
groupid and
artifactId of the dependency. There is no need to define the
version .
build tag also locks the plug-in version, as shown below
After configuring pom.xml, it is not difficult to understand why none of our project’s dependencies are configured
version.
pom.xml in the project we created
pom.xml and you will find that it introduces the following dependencies
spring-web and
spring-webmvc are dependent on each other. This is why our project can still use the annotations in
springMVC without relying on these two packages.
spring-boot-starter-tomcat. From the name, we can basically confirm that
tomcat is internally dependent, so our project can start normally.
starter
Common project names in SpringBoot
define all project coordinates used by the current project to reduce dependency configuration
parent
- The projects to be inherited by all
SpringBoot
projects define several coordinate version numbers (dependency management, not dependency) to reduce dependencies Conflicting Purpose
spring-boot-starter-parent
(2.5.0) andspring-boot-starter-parent
(2.4.6) have a total of 57 coordinates Different versions
Actual development
When using arbitrary coordinates, only write G and A in GAV, V is provided by SpringBoot
G:groupid
A:artifactId
V:version
If a coordinate error occurs, specify the version again (be careful of version conflicts)
Program startup
Every SpringBoot
program created contains a class similar to the following. We call this class the boot class
@SpringBootApplication public class Springboot01QuickstartApplication { public static void main(String[] args) { SpringApplication.run(Springboot01QuickstartApplication.class, args); } }
Note:
SpringBoot
When creating a project, use the jar packaging methodSpringBoot
The boot class is the entry point of the project. Running themain
method can start the project
because we configured ## in pom.xml
#spring-boot-starter-web depends on it, and this dependency knows through previous learning that it depends on
tomcat, so running the
main method can use
tomcat Start our project.
tomcat server, can we use
jetty# instead of tomcat
## Server, jetty
When we maven
are advanced, we will talk about the server used by maven
private servers. To switch the web
server, you need to exclude the default tomcat
server. How to exclude it? Using the exclusion
tag <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency></pre><div class="contentsignin">Copy after login</div></div>
Now can we run the bootstrap class? Try running it. The printed log information is as follows
The program stopped directly. Why? That's because if the
server is excluded, there will be no server in the program. So at this time not only the tomcat
server must be excluded, but also the jetty
server must be introduced. In pom.xml
, because the starting dependency of jetty
is <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency></pre><div class="contentsignin">Copy after login</div></div>
, run the boot class again. You can see in the log information that
Server
Summary:By switching the server, it is not difficult for us to find that we are using SpringBoot
Comparison between spring and springboot
When changing technology, you only need to import the starting dependencies of the technology.
After we finished the introductory case in the previous article, we can find that there is a big difference between the two:
- Spring
The coordinates in the program need to be written by yourself, and there are many coordinates
- SpringBoot
The coordinates in the program are automatically generated by checking when we create the project
- Spring
The program needs to write this configuration class by itself. Everyone has written this configuration class before, and it must feel very complicated
- SpringBoot
The program does not need to be written by ourselves
- Spring/SpringMVC
The configuration class of the program needs to be written by yourself. The
SpringBoot
program does not need to be written.
Spring Initializrbased on Idea requires an Internet connection to quickly build the
Official website construction projectSpringBoot
project.
The reason why the
SpringBoot project can be quickly built in the entry case is because Idea
uses the provided by the official website Quickly build the components of the SpringBoot
project. So how to build projects on the official website? Build through the following stepsEnter the SpringBoot official website
Enter the
SpringBoot official website and drag it to the bottom to see the following content
Then click
The hyperlink will jump to the following page
Does the content of this page look familiar? The interface is basically the same as the one we use
to quickly build the SpringBoot
project. Enter the corresponding information on the above pageSelect dependencies
Select
Spring Web You can click the ADD DEPENDENCIES... CTRL B
button in the upper right corner of the picture above , the following interface will appear<p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/887/227/168376831282036.png" class="lazy" alt="How to solve the problems of SpringBoot official website construction and quick startup" /></p><h4 id="生成工程">生成工程</h4><p>以上步骤完成后就可以生成 <code>SpringBoot
工程了。在页面的最下方点击 GENERATE CTRL + 回车
按钮生成工程并下载到本地,如下图所示
打开下载好的压缩包可以看到工程结构和使用 Idea
生成的一模一样,如下图
而打开 pom.xml
文件,里面也包含了父工程和 Spring Web
的依赖。
通过上面官网的操作,我们知道 Idea
中快速构建 SpringBoot
工程其实就是使用的官网的快速构建组件,那以后即使没有 Idea
也可以使用官网的方式构建 SpringBoot
工程。
SpringBoot工程快速启动
问题引入
以后我们和前端开发人员协同开发,而前端开发人员需要测试前端程序就需要后端开启服务器,这就受制于后端开发人员。为了摆脱这个受制,前端开发人员尝试着在自己电脑上安装 Tomcat
和 Idea
,在自己电脑上启动后端程序,这显然不现实。
我们后端可以将 SpringBoot
工程打成 jar
包,该 jar
包运行不依赖于 Tomcat
和 Idea
这些工具也可以正常运行,只是这个 jar
包在运行过程中连接和我们自己程序相同的 Mysql
数据库即可。这样就可以解决这个问题,如下图
那现在问题是如何打包呢?
打包
由于我们在构建 SpringBoot
工程时已经在 pom.xml
中配置了如下插件
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
所以我们只需要使用 Maven
的 package
指令打包就会在 target
目录下生成对应的 Jar
包。
注意:该插件必须配置,不然打好的
jar
包也是有问题的。
启动
进入 jar
包所在位置,在 命令提示符
中输入如下命令
java -jar 包名.jar
执行上述命令就可以看到 SpringBoot
运行的日志信息
The above is the detailed content of How to solve the problems of SpringBoot official website construction and quick startup. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Introduction to Jasypt Jasypt is a java library that allows a developer to add basic encryption functionality to his/her project with minimal effort and does not require a deep understanding of how encryption works. High security for one-way and two-way encryption. , standards-based encryption technology. Encrypt passwords, text, numbers, binaries... Suitable for integration into Spring-based applications, open API, for use with any JCE provider... Add the following dependency: com.github.ulisesbocchiojasypt-spring-boot-starter2. 1.1Jasypt benefits protect our system security. Even if the code is leaked, the data source can be guaranteed.

Usage scenario 1. The order was placed successfully but the payment was not made within 30 minutes. The payment timed out and the order was automatically canceled. 2. The order was signed and no evaluation was conducted for 7 days after signing. If the order times out and is not evaluated, the system defaults to a positive rating. 3. The order is placed successfully. If the merchant does not receive the order for 5 minutes, the order is cancelled. 4. The delivery times out, and push SMS reminder... For scenarios with long delays and low real-time performance, we can Use task scheduling to perform regular polling processing. For example: xxl-job Today we will pick

1. Redis implements distributed lock principle and why distributed locks are needed. Before talking about distributed locks, it is necessary to explain why distributed locks are needed. The opposite of distributed locks is stand-alone locks. When we write multi-threaded programs, we avoid data problems caused by operating a shared variable at the same time. We usually use a lock to mutually exclude the shared variables to ensure the correctness of the shared variables. Its scope of use is in the same process. If there are multiple processes that need to operate a shared resource at the same time, how can they be mutually exclusive? Today's business applications are usually microservice architecture, which also means that one application will deploy multiple processes. If multiple processes need to modify the same row of records in MySQL, in order to avoid dirty data caused by out-of-order operations, distribution needs to be introduced at this time. The style is locked. Want to achieve points

Springboot reads the file, but cannot access the latest development after packaging it into a jar package. There is a situation where springboot cannot read the file after packaging it into a jar package. The reason is that after packaging, the virtual path of the file is invalid and can only be accessed through the stream. Read. The file is under resources publicvoidtest(){Listnames=newArrayList();InputStreamReaderread=null;try{ClassPathResourceresource=newClassPathResource("name.txt");Input

When Springboot+Mybatis-plus does not use SQL statements to perform multi-table adding operations, the problems I encountered are decomposed by simulating thinking in the test environment: Create a BrandDTO object with parameters to simulate passing parameters to the background. We all know that it is extremely difficult to perform multi-table operations in Mybatis-plus. If you do not use tools such as Mybatis-plus-join, you can only configure the corresponding Mapper.xml file and configure The smelly and long ResultMap, and then write the corresponding sql statement. Although this method seems cumbersome, it is highly flexible and allows us to

SpringBoot and SpringMVC are both commonly used frameworks in Java development, but there are some obvious differences between them. This article will explore the features and uses of these two frameworks and compare their differences. First, let's learn about SpringBoot. SpringBoot was developed by the Pivotal team to simplify the creation and deployment of applications based on the Spring framework. It provides a fast, lightweight way to build stand-alone, executable

1. Customize RedisTemplate1.1, RedisAPI default serialization mechanism. The API-based Redis cache implementation uses the RedisTemplate template for data caching operations. Here, open the RedisTemplate class and view the source code information of the class. publicclassRedisTemplateextendsRedisAccessorimplementsRedisOperations, BeanClassLoaderAware{//Declare key, Various serialization methods of value, the initial value is empty @NullableprivateRedisSe

In projects, some configuration information is often needed. This information may have different configurations in the test environment and the production environment, and may need to be modified later based on actual business conditions. We cannot hard-code these configurations in the code. It is best to write them in the configuration file. For example, you can write this information in the application.yml file. So, how to get or use this address in the code? There are 2 methods. Method 1: We can get the value corresponding to the key in the configuration file (application.yml) through the ${key} annotated with @Value. This method is suitable for situations where there are relatively few microservices. Method 2: In actual projects, When business is complicated, logic
