SpringBoot

image-20230728000819890






第一个SpringBoo程序

1、构建

1、在线

Spring Boot

image-20230728005359815

  • 然后解压导入项目即可

2、idea构建

  • Spring Iniiializr –》 选择web依赖


2、编写

  • 主程序:java包中的 ** Application 类为 项目的主入口,其余层(pojo、service、controller、dao)要和该类的目录同级才能被扫描到
///@SpringBootApplication:标注这个类是一个springboot的应用
@SpringBootApplication
public class Springboot01HellowordApplication {
public static void main(String[] args) {
//将springboot应用启动
SpringApplication.run(Springboot01HellowordApplication.class, args);
}
}
  • Controller层代码编写和SpringMvc框架一样





原理

自动配置

全面接管SpringMVC的配置!

pom.xml

  • spring-boot-dependencies : 核心依赖在父工程中!
  • 引入一些SPringboot依赖的时候,不需要指定版本,就因为有这些版本仓库

  • 启动器:
<!--启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
  • 启动器:说白了就是Springboot的启动场景;
  • 比如 spring-boot-starter-web,就会帮我们自动导入web环境所有的依赖!
  • springboots会将所有的功能场景,都变成一个个的启动器
  • 我们要使用什么功能,就只需要找到对应的启动器就可以了starter

主程序

///@SpringBootApplication:标注这个类是一个springboot的应用
@SpringBootApplication
public class Springboot01HellowordApplication {
public static void main(String[] args) {
//将springboot应用启动
SpringApplication.run(Springboot01HellowordApplication.class, args);
}
}
  • 注解【@SpringBootApplication层级】

    @SpringBootConfiguration //springboot的配置
    @Configuration //spring配置类
    @Component //说明这也是一个spring组件

    @EnableAutoConfiguration //自动配置
    @AutoConfigurationPackage //自动配置包
    @Import(AutoConfigurationPackages.Registrar.class) //自动配置 包注册
    @Import({AutoConfigurationImportSelector.class}) //导入选择器




主启动类

这个类主要做了以下四件事情:

1.推断应用的类型是普通的项目还是Web项目

2.查找并加载所有可用初始化器,设置到initializers.属性中

3.找出所有的应用程序监听器,设置到listeners属性中

4.推断并设置main方法的定义类,找到运行的主类

//@SpringBootApplication:标注这个类是一个springboot的应用:启动类下的所有资源被导入
@SpringBootApplication
public class Springboot01HellowordApplication {
public static void main(String[] args) {
//将springboot应用启动
//SpringApplicaion类
//run方法
SpringApplication.run(Springboot01HellowordApplication.class, args);
}
}





配置文件格式

properties

  • application.properties

    • 语法结构 : key = value
    • 只能保存键值对

实体类数据注入

  • 在properties配置文件中录入待注入的对象数据
name=chen
  • 在实体类中使用注解实现指定配置文件注入
@PropertySource(value= "classpath:指定配置文件")
@PropertySource(value= "classpath:application.properties")
public class Person {
//独自使用SPEL表达式取出配置文件的值
@Value("${name}")
private String name;
private Integer age;
private Boolean happy;
private Date birth;
private Map<String, Object> maps;
private List<Object> lists;
private Dog dog;
}}


yaml【*】

  • application.yaml (yaml == yml)
    • 语法结构 : key :空格 value
    • 键值对、数组、对象
    • 可以注入到配置类中

语法规则

# 普通 key: value
name: chen

#对象
student:
name: chen
age: 3
#行内写法
student: {name: chen,age: 3}

#数组
pets:
- cat
- dog
- pig
#行内写法
pets: [cat,dog,pig]
  • 可以使用表达式
person:
name: ${radom.uuid}
age: ${radom.int}
namec: ${person.hello:cc}_默认值 #设置默认值:如果person.hello存在则赋值为 person.hello ,否则赋值默认值( cc_默认值 )
  • 松散绑定:比如yaml中命名为 last-name ,这是和映射为 lastName是一样的, - 后面跟着的字母默认为大写(驼峰命名)

  • JSR303数据校验:这个就是我们可以在字段是增加一层过滤器验证,可以保证数据的合法性

    • image-20230730001119240

    • 导入依赖:spring-boot-starter-validation

    • @Validated //数据校验
      public class Person {
          @Email	//指定类型
          @Email(message = "自定义格式错误提醒")	//指定类型
          private String name;
      }
      









      <br/>

      ### **实体类数据注入**

      - 在yaml配置文件中录入待注入的对象数据

      ```yaml
      person:
      name: chen
      age: 2
      happy: true
      birth: 2023/07/29
      maps: {k1: v1,k2: v2}
      lists:
      - code
      - music
      - girl
      dog:
      name: 大黄
      sex: 男孩子

  • 在实体类中使用注解注入

image-20230729162507350

@ConfigurationProperties(prefix = "yaml中的对象名称")
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private Integer age;
private Boolean happy;
private Date birth;
private Map<String, Object> maps;
private List<Object> lists;
private Dog dog;
}

在类中直接 注入Person 即可把 配置文件的对象注入到 当前注入的对象,new则为空

@Resource
private Person person;


单数据注入

#token密钥
jwt:
secret: 9c7a0c16f41e4062a94c5e7a3413d7a8
@Value("${jwt.secret}")
private String secertKey;





application配置

Spring Boot使用一个全局的配置文件,配置文件名称是固定的,可存在多个配置文件 ,处理优先级问题

格式优先级:properties > yml > yaml


多环境配置

1、properties

springboot的多坏境配置:可以选择激活那一个配置文件

例如多套环境:

application.properties

application-dev.properties

application-test.properties

#主默认配置文件编写
spring.profiles.active=dev


2、yaml

  • - 为多套配置分隔符
#选择激活版本
spring:
profiles:
active: test

server:
port: 8081

---
server:
port: 8082
spring:
profiles: dev

---
server:
port: 8083
spring:
profiles: test





SpringBoot Web开发

自动装配

  • xxxAutoConfiguraion.向容器中自动配置组件

  • xxxProperties:自动配置类,装配配置文件中自定义的一些内容!


1、静态资源

在springboot,我们可以使用以下方式处理静态资源


以maven坐标引入包

//访问
(pathPattern:"/webiars/**"))


  • resources下的目录
    • 访问映射路径:localhos:8080/
private static final String[]CLASSPATH_RESOURCE_LOCATIONS 
//以下目录都可被扫描访问
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
};

优先级:

resources > static > public



  • 配置文件自定义指定包
spring:
mvc:
static-path-pattern: /目录/**





2、首页定制

1、自动映射命名为 index.html 为首页 :localhost:8080

  • 目录:public、staic、resources

2、访问 templates目录下的页面

  • 需要导入模板引擎的支持:thymeleaf
  • 只能通过Controller来跳转

3、首页配置:注意点,所有页面的静态资源都需要使用thymeleaf接管






3.1、templates模板引擎

依賴

<!--templates-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>

将html放在templates目录下即可



配置

#关闭thymeleaf缓存
spring.thymeleaf.cache=false


语法

  • html 导入约束
<html lang="en" xmlns:th="http://www.thymeleaf.org">

  • 所有的html元素都可以被thymeleaf替換接管
  • th:元素名 (和Vue类似)
  • 渲染数据:[[ ${Value }]] (Vue使用