springboot war包运行

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhoucheng05_13/article/details/77915294

为什么要把SpringBoot打成war包

正常情况下SpringBoot项目是以jar包的形式,通过命令行:

java -jar demo.jar

    
    
  • 1

来运行的,并且SpringBoot是内嵌Tomcat服务器,所以每次重新启动都是用的新的Tomcat服务器。正因如此,也出现了一个问题:
上传到项目的文件,如果是保存在项目中的,那么重启过后文件就会丢失。比如我们上传了一个头像,重启项目后,这个头像就没了。而如果将文件保存在本地磁盘中的话,html中标签没办法获取(当然,企业项目中一般是有专门的图片服务器的)。因此,我们才需要将SpringBoot项目打成war包,放到Tomcat中去运行。

修改方法

在pom.xml文件中添加下面的依赖:

 <!--因配置外部TOMCAT 而配置-->
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

同时,将pom.xml文件首部的jar改成war

	<groupId>com.star</groupId>
	<artifactId>yiyong</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<!--<packaging>jar</packaging>-->

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

最后,将启动类从

@SpringBootApplication
public class YiyongApplication {
	public static void main(String[] args) {
		SpringApplication.run(YiyongApplication.class, args);
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

修改为

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

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

注意:该类继承了SpringBootServletInitializer并且重写了configure方法,这是关键所在。

打包部署

在IDEA右侧Maven栏双击package等待Build Success即可。
这里写图片描述
然后把target目录下的war包放到tomcat的webapps目录下,启动tomcat,即可自动解压部署。
最后在浏览器中输入

http://localhost:[端口号]/[打包项目名]/

 
 
  • 1

发布成功

        </div>
					<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-a47e74522c.css" rel="stylesheet">
            </div>
								
				<script>
					(function(){
						function setArticleH(btnReadmore,posi){
							var winH = $(window).height();
							var articleBox = $("div.article_content");
							var artH = articleBox.height();
							if(artH > winH*posi){
								articleBox.css({
									'height':winH*posi+'px',
									'overflow':'hidden'
								})
								btnReadmore.click(function(){
									if(typeof window.localStorage === "object" && typeof window.csdn.anonymousUserLimit === "object"){
										if(!window.csdn.anonymousUserLimit.judgment()){
											window.csdn.anonymousUserLimit.Jumplogin();
											return false;
										}else if(!currentUserName){
											window.csdn.anonymousUserLimit.updata();
										}
									}
									
									articleBox.removeAttr("style");
									$(this).parent().remove();
								})
							}else{
								btnReadmore.parent().remove();
							}
						}
						var btnReadmore = $("#btn-readmore");
						if(btnReadmore.length>0){
							if(currentUserName){
								setArticleH(btnReadmore,3);
							}else{
								setArticleH(btnReadmore,1.2);
							}
						}
					})()
				</script>
				</article>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值