创建springboot工程的第一种方式
1、创建工程在pom文件中,继承父工程【springboot工程】
- 导入spring-boot-starter依赖
- 导入spring-boot-starter-web依赖【具有MVC的能力,相当于继承了springmvc】
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--父工程 springboot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.chenshuang</groupId>
<artifactId>springboot_01</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!--执行maven命令插件-->
<!--可以打成jar包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、需要在资源中springboot配置文件【application.properties】
3、需要主启动类 | 在Java/com.chenshuang/目录下创建类【SpringbootStarter.class】 在主启动类前添加 @SpringBootApplication注解
在类中写main方法,方法中借助SpringApplication.run(SpringbootStarter.class,args)方法
//主启动类
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class,args);
}
}
测试:在主启动类所在的包下创建controller包(自动扫描注解,主启动类所在的包就是扫描范围),添加控制类【controller注解】
@Controller
public class PersonController {
@RequestMapping("/add")
@ResponseBody
public String addPerson(){
System.out.println("执行了addperson方法!");
return "success";
}
}
文件配置值的注入
application.properties中的配置
修改类的默认值,需要在类上面加注解ConfigurationProperties
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
#配置访问端口号
server.port=8081
#配置上下文路径
server.servlet.context-path=/demo
#配置springboot项目的名称
spring.application.name=springboot_b
#默认配置Person类中的 id属性
person.id=100
#默认配置Person类中的 name属性
person.name=zhangsan
#默认配置Person类中的 success属性【boolean类型】
person.success=true
#默认配置Person类中的 birthday属性【Date类型】
person.birthday=1992/01/20
#默认配置Person类中的 dog属性中的dogName属性【特殊类型--Dog对象中的属性】
person.dog.dogName=xiaogou
#默认配置Person类中的 dog属性中的dogAge属性【特殊类型--Dog对象中的属性】
person.dog.dogAge=3
#默认配置Person类中的 nameList属性 【ArrayList集合】
person.nameList=zhangsan,lisi,wangwu
#默认配置Person类中的 hobbies属性 【String数组】
person.hobbies=running,jump,lookbook
#默认配置Person类中的 maps属性 【Map集合--(key=bookName value=java)】
person.maps.bookName=java
#默认配置Person类中的 maps属性 【Map集合--(key=bookAuthor value=lisi)】
person.maps.bookAuthor=lisi
#默认配置Person类中的 maps属性 【Map集合--(key=bookPrice value=99)】
person.maps.bookPrice=99
application.yml中的配置
- 注意缩进对齐
- 冒号后面必须有空格
#配置访问端口号
server:
port: 8088
#配置上下文路径
servlet:
context-path: /springboot
#配置springboot项目的名称
spring:
application:
name: springbootYML
person:
id: 100
name: 哈哈哈
success: false
birthday: 1988/05/24
dog:
dogName: 狗狗狗
dogAge: 88
nameList: 张三,李四,王五
hobbies: [唱歌,跳舞,游泳]
maps:
bookName: 水浒传
bookAuthor: 施耐庵
bookPrice: 108
yml中多环境配置
Profile是Spring对不同环境提供不同配置功能的支持,可以通过激活、指定参数等方式快速切换环境;
1、多文件多环境形式:
格式:application-{profile}.properties/yml
例如:可以在项目中创建如下主配置文件:application-dev.properties、application-test.properties、application-prod.properties、application.properties,默认使用application.properties,可以通过配置spring.profiles.active=profile指定使用某个环境的配置文件。
2、yaml支持单文件多环境形式:【不同环境中间用---隔开】
spring:
profiles:
active: test
---
spring:
profiles: dev
server:
port: 8081
---
spring:
profiles: test
server:
port: 8082
通过---来区分不同的profile环境。
3、激活方式:
在配置文件中指定 spring.profiles.active=dev
命令行 java -jar springboot-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
jvm参数 -Dspring.profiles.active=dev
配置文件加载位置
springboot默认会从以下位置加载配置文件:application.properties/application.yml
项目所在磁盘路径file:./config/
项目所在磁盘路径file:./
项目类路径classpath:/config/
项目类路径classpath:/
优先级由高到底,高优先级的配置会覆盖低优先级的配置;SpringBoot会从这四个位置全部加载主配置文件,互补配置;
如果要加载其他位置的配置文件,可以通过–spring.config.location(只能加到命令行)来指定要加载的配置文件的位置。
静态资源的处理
主要掌握springboot当中静态资源的处理,springboot当中提供的静态资源存放的位置。 在实际当中开发,我们应该满足springboot的约定和规范。如果提供的静态资源目录不能满足实际的需求,还可以自定义静态资源的位置
静态资源的优先级
从该方法中可以发现:
1、默认情况下:
(1)匹配/webjars/** 的请求,都去 classpath:/META-INF/resources/webjars/ 找资源;
(2)匹配 “/**” 的请求,都去(静态资源的文件夹)找映射,静态资源文件夹路径如下:
“classpath:/META‐INF/resources/”
“classpath:/resources/”
“classpath:/static/”
“classpath:/public/”
“/”:当前项目的根路径
2、自定义配置静态资源路径:
spring.web.resources.static-locations=自定义路径
在ResourceProperties类中
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
webjars形式引用静态资源
引用 JQuery 、bootstrap等公共的静态资源,通过pom文件引用依赖的形式 把该类资源打成jar包,随后引用。
静态资源坐标参考:https://www.webjars.org
在pom文件中引入
<!-- 引入jquery webjars -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.6.0</version>
</dependency>
<!-- 引入 bootstrap webjars-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>5.1.3</version>
</dependency>
随后在html页面中引用
<!DOCTYPE html>
<html lang="en" >
<head>
<script src="webjars/jquery/3.6.0/jquery.js"></script>
<script src="webjars/popper.js/2.9.3/esm/enums.js"></script>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>
访问页面可以查看,证明引入成功
自定义位置存放静态资源
当自定义静态存放路径之后,默认的resources、static、public下的静态资源自动失效
在yml中设置静态资源配置
spring:
web:
resources:
static-locations: classpath:/page/
在properties文件添加如下配置
spring.web.resources.static-locations=classpath:/page/
位置存放静态资源
当自定义静态存放路径之后,默认的resources、static、public下的静态资源自动失效