In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to build a project that separates the parent dependence of common in Springboot, and the content of the article is of high quality, so the editor shares it for you to do a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
With the promotion of the separation of the front and rear of the project, more and more enterprises begin to pay attention to the separation of the front and rear of the project when designing the project. The so-called front-end separation, the front-end and back-end separation, the front-end and back-end can be separated from each other to run independently, while the back-end is mostly Springboot, which is the focus of this blog post.
First, create a new project
First of all, you need to create a new Spring project. You can use development tools such as eclipse, IntelliJ IDEA, STS, etc., to create the Spring project (omitted here), or you can go directly to the Springboot official website and click on the following figure.
Then select the parameters to create the project, and finally click the button circled below to download an initialized project.
Then extract the project and import the project into the development tool, which may be slow because the maven project automatically downloads the required jar package according to the pom file. If the import is too slow, you can try to configure the warehouse for the domestic warehouse in maven first (for example, Ali warehouse), then use cmd to enter the local directory of the project (you can see the directory of pom.xml), run with mvn install, the speed will be much faster, and then import the project after the emergence of green SUCCESS.
Then re-import it into the development tool, open the pom.xml file, and see if web dependencies have been added. If not, add web dependencies, otherwise startup will report no error in the active configuration, thus unable to start tomcat.
Org.springframework.boot spring-boot-starter-web
Then find the startup class and run
If the console appears the following, it is a successful startup, then congratulations, you have completed the first step (now only the first step, hold on! )
Second, separate the common parent dependency.
First of all, make a copy of the initialization project locally. We call it demo-common-parent and delete all files except pom.xml.
Then first open the file in text (the sublime text I use here), then modify the configuration inside and change the artifactId to demo-common-parent, that is, the name of the folder.
Just fine. However, I am used to customizing the parent dependency, without the parent provided by springboot, that is, spring-boot-starter-parent. If you do not want to use this, you can delete the file (yes, you read it correctly, delete all the parent), change it to the following configuration (details here), and set the packing method to pom format (very important).
Org.springframework.boot spring-boot-dependencies 2.1.3.RELEASE pom import
At this point, the pom file becomes the following
Next, save it by ctrl+s, then dos it to demo-common-parent again, and then use the mvn install command to make the parent rely on automatic download and packaging. Congratulations, you are one step closer to success.
(PS: I like to watch this green BUILD SUCCESS. It's comfortable for thieves.)
Third, the project's reference to the parent dependency
First of all, I would like to congratulate you who are reading this blog, because you have reached the penultimate step, and it is a step that increases your confidence. Are you very happy? let's move on to the next step.
First of all, import the parent dependency into the development tool (note: be sure to import the project. If you are using the idea development tool, be sure to check the maven you use to avoid trampling because the maven packaging warehouse used by the child project is different from that of the child project. If the child project uses its own locally configured maven, the parent dependency uses the default maven of the development tool. If it is not changed to be consistent, some will suffer. Then open the pom.xml file in the subproject, that is, the demo project, and direct the tag to the demo-common-parent you just wrote, that is, change the contents of the tag to the following
Com.example demo-common-parent 0.0.1-SNAPSHOT
Delete the configuration that duplicates the parent dependency (because the parent dependency already exists and will be introduced directly), and the pom.xml will look like the following figure
Next, find the entry file DemoApplication.java
Dare to run it, and then, when the console appears the following
Perfect, successful, so that the dependency of the Springboot project successfully points to its own parent dependency
IV. Project dependencies and configuration files
After completing the above three steps, in fact, a rough back-end architecture is set up, and the last step is the dependent configuration of the project and the configuration file of the project
The configuration of dependency is actually very simple. If you need any dependency, search it on the Internet, then find the corresponding version, and then copy it to the pom file. As for whether to put it into the parent dependent pom or the project pom, it depends on what the specific dependency is used for. For example, if the dependency introduced is a tool class, you can consider introducing this into the parent dependency, so that as long as other modules reference the parent dependency, it also has the tool dependency; and if the dependency you want to introduce is only used by this project, consider devolving it to the pom of the current project. Here we will focus on the configuration file of Springboot.
There are two kinds of Springboot configuration files, application.properties and application.yml. I personally recommend using yml because of the level of visual effects and clarity. The initialized project looks like this
If we want to use yml, first delete application.properties, and then create a new file application.yml in the same location, and then write the configuration directly in it, because resources itself is under classpath, and the configuration file will be read when starting. We simply configure the port and path of the container.
Restart the project, and the console displays as shown in the following figure
Congratulations again, the configuration has taken effect, the port and path of the container have been changed to the contents in the configuration file, and then write a controller casually.
After successfully restarting the project, open a browser to access http://localhost:12345/demo/hello/success. At this time, the browser displays the following page
What is springboot? springboot is a new programming specification, which is designed to simplify the initial construction and development process of new Spring applications. SpringBoot is also a framework that serves the framework, and the scope of services is to simplify configuration files.
On the Springboot separated from the common parent project building is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.