Spring Boot打war包的实例教程
Spring Boot除了可以打可执行jar包外,也支持传统的war包。本文介绍如何使用Spring Boot构建传统war包。
Spring Boot打war包步骤如下:
1、在pom.xml里定义打包类型
<packaging>war</packaging>
2、添加Spring Boot启动器(也可通过parent)
<dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.6.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
3、添加spring-boot-starter-web依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
4、添加打包插件
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
5、主类继承SpringBootServletInitializer
/**
* WAR application
*/
@SpringBootApplication
public class WarApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(WarApplication.class, args);
}
}
6、执行mvn clean package打包
$mvn clean package
7、将打好的war包拷贝到容器(如tomcat)运行即可。
这里需要简单说明下:
主应用可以重写SpringBootServletInitializer里面有configure方法,自定义配置Spring Boot。
/**
* Configure the application. Normally all you would need to do is to add sources
* (e.g. config classes) because other settings have sensible defaults. You might
* choose (for instance) to add default command line arguments, or set an active
* Spring profile.
* @param builder a builder for the application context
* @return the application builder
* @see SpringApplicationBuilder
*/
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Java使用Soap方式调用WebService接口代码示例
Java调用WebService接口是指通过Java语言来访问并与WebService进行交互,WebService是一种基于Web的服务架构,它通过标准的XML和HTTP协议来提供服务,这篇文章主要给大家介绍了关于Java使用Soap方式调用WebService接口的相关资料,需要的朋友可以参考下2024-03-03
Spring Cloud 整合 nacos实现动态配置中心的详细步骤
这篇文章主要介绍了Spring Cloud 整合 nacos 实现动态配置中心,整合步骤是通过添加依赖新建nacos配置,本文分步骤通过实例代码给大家详细讲解,需要的朋友可以参考下2022-10-10


最新评论