第3天 互联网公司中新版SpringBoot2.X开发规范解读

3-1 新版SpringBoot2.x目录文件结构讲解和静态资源访问

  • 目录讲解

    • src/main/java:存放代码
    • src/main/resources
    • static: 存放静态文件, 比如 css、js、image, (访问方式 http://localhost:8080/js/main.js)
    • templates:存放静态页面jsp,html,tpl
    • config:存放配置文件,application.properties
    • resources:
  • 同个文件的加载顺序,静态资源文件 Spring Boot 默认会挨个从

    • META/resources >

    • resources >

    • static >

    • public

      里面找是否存在相应的资源,如果有则直接返回,不在默认加载的目录,则找不不到

  • 默认配置
    spring.resources.static-locations = classpath:/META-
    INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

3-2 新版SpringBoot2.x启动类位置常见形式

应用启动的位置,Application启动类位置
三种形式

  • 当启动类和controller在同⼀类中时,在该类上添加注解@Controller即可;
  • 当启动类和controller分开时,启动类要放在根目录下,启动类上只需要注解
    @SpringBootApplication;
  • 当启动类和controller分开时,如果启动类在非根目录下,需要在启动类中增加注解
    @ComponentScan,并配置需要扫描的包名,如(basePackages = )
    @ComponentScan(basePackages ={“net.xdclass.controller”,”net.xdclass.service”})

3-3 SpringBoot2.X启动方式讲解和部署

SpringBoot2.X启动方式

  • IDEA开发中启动
    • 本地开发中常用
    • 外置Tomcat中启动
      接近淘汰
      tomcat版本兼容问题复杂
      微服务容器器化部署复杂
    • Jar方式打包启动
      官⽅方推荐,工作中最常⽤用
      步骤:pom文件新增maven插件
1
2
3
4
5
6
7
8
9
10
11
12
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

如果没有加,则执⾏行行jar包 ,报错如下
java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar
no main manifest attribute, in spring-boot-demo-0.0.1-SNAPSHOT.jar

必备打包、启动命令

1
2
3
4
5
6
7
构建:mvn install
构建跳过测试类 mvn install -Dmaven.test.skip=true

target目录下有对应的jar包就是打包后项目

进到对应的target目录启动 java -jar xxxxx.jar 即可
想后台运行,就用守护进程 nohup java -jar xxx.jar &

3-4 打包后的Jar里面的目录结构你知道多少

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
example.jar
|
+-META-INF
| +-MANIFEST.MF
+-org
| +-springframework
| +-boot
| +-loader
| +-<spring boot loader classes>
+-BOOT-INF
+-classes
| +-mycompany
| +-project
| +-YourClasses.class
+-lib
+-dependency1.jar
+-dependency2.jar
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

请我喝杯咖啡吧~

支付宝
微信