SpringBoot released eight principles and solutions to four questions, solve problems 99%

Solve 99% of problems SpringBoot multi-module release? Solutions SpringBoot released eight principles and four issues

If you are using a multi-module project SpringBoot, you may encounter a variety of problems at the time of publication. This article summarizes the solution in the following eight principles that often appear when and publish four issues, mastered these principles and solutions can solve almost overwhelming number SpringBoot post questions.

SpringBoot multi-module released eight major principles

1 in the publishing module packaging, rather than on the parent module package

For example, the following project directory:

If you want to publish it on its api module package directly, rather than packaged on the parent module.

Public calling module 2, to the packaging type jar format

A common module, such as the model and the need to set common packaging format to jar arranged in pom.xml:

<packaging>jar</packaging>

3 is set to release the module packaging type format war

Set in a release module in pom.xml:

<packaging>war</packaging>

4 exclude built-tomcat

Set in a release module in pom.xml:

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-tomcat</artifactId>
 <scope>provided</scope>
</dependency>

When setting the scope = provided, this jar package does not appear in the article in a publication, which ruled out the built-in tomcat.

5 Set the startup class

This step corresponds to the entrance tell tomcat where to start. Need to add the following code startup class:

@SpringBootApplication
public class ApiApplication extends SpringBootServletInitializer {
 @Override
 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
 return application.sources(ApiApplication.class);
 }
 public static void main(String[] args) {
 SpringApplication.run(ApiApplication.class, args);
 }
}

6 If you are using interceptors must exclude static files

For example, I use a swagger in the project, then I need to exclude static files swagger, the code is as follows:

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
 @Override
 protected void addResourceHandlers(ResourceHandlerRegistry registry) {
 // 排除静态文件
 registry.addResourceHandler("swagger-ui.html")
 .addResourceLocations("classpath:/META-INF/resources/");
 registry.addResourceHandler("/webjars/**")
 .addResourceLocations("classpath:/META-INF/resources/webjars/");
 }
 // do something
}

7 is first loaded with a common module, and then publish the project

If the release of the module references other public modules of the project, the public need to first module of this project is loaded into the local repository.

Operation, double-click the parent module to install, after install successfully, click Publish package generation module package of war, to complete the package of the project, as shown below:

8 deployment project

With After war package, the package need only a single war, into tomcat webapps directory, to restart tomcat, as shown below:

Item normal operation will generate the same name in the directory webapps folder, as shown below:

完成以上配置,就可以 happy 的访问自己发布的项目了。

可能出现的问题和解决方案

问题一:SpringBoot 配置了端口号影不影响程序发布?

答:不影响,配置的 server.port 会被覆盖,以 tomcat 本身的端口号为准,tomcat 端口号在 tomcat/config/server.xml 文件中配置。

问题二:发布报错,不能找到其他模块或项目中的公共模块,怎么办?

答:因为没有执行父节点 maven 的 install 操作,install 就是把公共模块放入本地仓库,提供给其它项目使用。

问题三:不能找到 SpringBoot 运行的 main 类,怎么办?

答:因为没有设置启动类导致的,设置方式:

  • pom.xml 配置启动类,配置 configuration><mainClass>com.bi.api.ApiApplication</mainClass></configuration> 。
  • 启动类继承 SpringBootServletInitializer 实现 SpringApplicationBuilder 方法,具体代码参考文中第五部分。
  • 问题四:把 SpringBoot 项目部署到 Tomcat 7 一直提示找不到 xxx.jar 包?
  • 答:这是因为 SpringBoot 版本太高,tomcat 版本太低的原因。如果你使用的是最新版的 SpringBoot,可以考虑把 tomcat 也升级为 tomcat 8.x+ 最新的版本,就可以解决这个问题。

Guess you like

Origin blog.csdn.net/qwe123147369/article/details/93193660