额…看标题大家应该觉得很奇怪,为什么叫伪分离,因为嘛,正常前后端分离都是使用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> 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左右,想想就爽,而每次如果有需要紧急修改静态文件或者模板文件的时候,直接在服务器上修改就可以了,当然修改完要记得同步代码。
当然一般中大公司不会允许你这样直接在服务器上修改代码,因为这样相当于直接把未经测试的代码发布到生产环境,很容易出事故的(我就经历过这样的惨案😭)。
好了,搞了一个通宵,终于可以好好睡一觉了😄