Skip to content

Commit 5e4d600

Browse files
committed
[add] springboot项目从jar包转为war包
1 parent e160b75 commit 5e4d600

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

springboot.md

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# SpringBoot
2+
3+
## 项目从jar变成war
4+
5+
> IDEA使用Sping Initializer新建SpringBoot项目,选择打包为jar后,如果后期想改成war部署在Tomcat中,该怎么做呢?
6+
7+
假设我们的启动类为`Applicaion.java`
8+
9+
```java
10+
import org.springframework.boot.SpringApplication;
11+
import org.springframework.boot.autoconfigure.SpringBootApplication;
12+
13+
@SpringBootApplication
14+
public class Applicaion {
15+
public static void main(String[] args) {
16+
SpringApplication.run(Applicaion.class, args);
17+
}
18+
}
19+
```
20+
21+
### Step 1
22+
23+
修改Application类,使其继承自`SpringBootServletInitializer`, 并重写`configure`方法
24+
25+
```java
26+
import org.springframework.boot.SpringApplication;
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
29+
@SpringBootApplication
30+
public class Applicaion extends SpringBootServletInitializer{
31+
@Override
32+
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
33+
return application.sources(CourseWarApplication.class);
34+
}
35+
public static void main(String[] args) {
36+
SpringApplication.run(Applicaion.class, args);
37+
}
38+
}
39+
40+
```
41+
42+
### Step 2
43+
44+
修改`pom.xml`文件
45+
46+
```xml
47+
<packaging>jar</packaging>
48+
```
49+
50+
51+
52+
```xml
53+
<packaging>war</packaging>
54+
```
55+
56+
并加入如下的依赖
57+
58+
```xml
59+
<!-- 这个是比生成jar文件要多加的包 -->
60+
<dependency>
61+
<groupId>org.springframework.boot</groupId>
62+
<artifactId>spring-boot-starter-tomcat</artifactId>
63+
<scope>provided</scope>
64+
</dependency>
65+
```
66+
67+
> 实际是改变springBoot内置Tomcat依赖的作用域,使其被部署时不会出现冲突
68+
69+
## jar变成war后要注意!
70+
71+
很有可能出现数据库连接不上的坑!
72+
73+
```xml
74+
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db_name</property>
75+
```
76+
77+
上面这个看起来没问题,但实际操作时,应注意`localhost`可能给你制造麻烦,某些服务器本地地址不一定是`127.0.0.1`,而是其他诸如`192.169.1.100`, **建议不用`localhost`,而是用实际本地地址**
78+

0 commit comments

Comments
 (0)