額…看標題大家應該覺得很奇怪,爲什麼叫僞分離,因爲嘛,正常前後端分離都是使用springboot+vue的,使用thymeleaf,那不是跟jsp一樣麼,對吧。那麼先說起因吧,我上篇文章講了。把第三方jar單獨分離出來,這樣可以大大減輕項目jar包的大小,但是感覺還是不夠,因爲資源文件css,js,html,圖片等文件隨着項目越做越大,佔用空間越來越大。我這個項目這些資源文件已經又100多mb了。。。每次打包都要全部打包一遍,然後上傳服務器,有時候簡單修改個樣式也是要全部打包一遍,感覺實在是浪費時間。所以就想到了把這些文件也單獨分離出來。使用WebMvcConfigurer配置,代碼如下
@SpringBootConfiguration
public class MyWebConfigurer implements WebMvcConfigurer {
@Value("${properties.uplaodFilePath}")
private String uplaodFilePath;
@Value("${properties.staticResourcesPath}")
private String staticResourcesPath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("file:"+staticResourcesPath);
registry.addResourceHandler("/file/**").addResourceLocations("file:" + uplaodFilePath);
}
}
這個staticResourcesPath的路徑就是靜態路徑的位置,因爲開發環境是生成環境不一樣,所以我把路徑配置在yml裏面
spring:
profiles:
active: dev
srping:
thymeleaf:
prefix: classpath:/templates/
mybatis:
typeAliasesPackage: com.hch.xconnect.entity
mapperLocations: classpath:/mapper/*.xml
---
#開發配置
spring:
profiles: dev
datasource:
url: jdbc:mysql://localhost:3306/fifa?useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
thymeleaf:
encoding: UTF-8
mode: HTML
servlet:
content-type: text/html
cache: false
servlet:
multipart:
max-file-size: 100MB
max-request-size: 5000MB
server:
port: 61193
properties:
uplaodFilePath: /usr/local/upload/
version: 1.0
staticResourcesPath: /Users/workspaces/fifa/src/main/resources/static/
---
#生產
spring:
profiles: pro
datasource:
url: jdbc:mysql://localhost:63306/fifa?useSSL=false
username: root
password: Zz.123.123
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true`在這裏插入代碼片`
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
encoding: UTF-8
mode: HTML
servlet:
content-type: text/html
cache: true
servlet:
multipart:
max-file-size: 100MB
max-request-size: 5000MB
server:
port: 61193
properties:
uplaodFilePath: /usr/local/upload/
version: 1.0
staticResourcesPath: /usr/local/fifa/static/
接下去就是在pom.xml裏面配置打包的時候不打包靜態資源文件也就是static目錄下面的文件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<excludes>
<exclude>/static/**</exclude><!--static目錄下面文件不打包-->
<exclude>/templates/**</exclude>
</excludes>
</configuration>
</plugin>
然後大家也可以看到配置裏面我把templates的文件也些上去,也就是templates目錄下面的文件也不打包。那麼一開始百度了很多,甚至論壇上被嘲諷說模版文件怎麼可能打包到外面,肯定要跟jar包一起的,說明jvm原理之類的,嘰裏呱啦,但是我覺得肯定有解決的方案,皇天不負有心人,我看到的下面的文章
https://www.cnblogs.com/jifeng/p/11647718.html
也就是thymeleaf 在application.yml配置的時候是
prefix: classpath:/templates/
(試過了直接配置prefix爲絕對路徑,是不行的)
那麼我們可以從這個classpath入手,在運行jar命令的時候,增加classpath搜索的路徑,這樣不就可以了,於是運行命令如下
java -Xbootclasspath/a:/Users/workspaces/fifa/src/main/resources/ -jar fifa-1.0.jar
注意我的templates完整目錄是/Users/workspaces/fifa/src/main/resources/templates,但是打命令的時候是/Users/workspaces/fifa/src/main/resources/,因爲在application.yml裏面還配置了prefix: classpath:/templates/,因此命令的目錄要在templates目錄上一級。
工程目錄如下
好,到了這裏,我們就分離出第三方jar包、static靜態文件、templates模板文件。這樣每次打包的是主jar包只有幾百kb左右,想想就爽,而每次如果有需要緊急修改靜態文件或者模板文件的時候,直接在服務器上修改就可以了,當然修改完要記得同步代碼。
當然一般中大公司不會允許你這樣直接在服務器上修改代碼,因爲這樣相當於直接把未經測試的代碼發佈到生產環境,很容易出事故的(我就經歷過這樣的慘案😭)。
好了,搞了一個通宵,終於可以好好睡一覺了😄