目录
一、SpringBoot
一、概念简介
二、入门案例
三、快速启动
四、起步依赖
五、服务器启动程序(面试题)
六、配置格式(面试题之一)
七、核心注解(面试题之一)
二、基础配置
一、yaml配置
二、yaml数据读取方式(三种)面试题
三、多环境启动(面试题之一)
四、多环境启动命令格式
五、Maven与SpringBoot兼容
六、配置文件分类(面试题)
三、整合第三方技术
一、整合JUnit
二、整合SSM
四、基于SpringBoot的SSM整合案例
一、SpringBoot
一、概念简介
什么是SpringBoot?面试题之一
SpringBoot是由Pivotal团队提供的全新框架,其设计目的是简化Spring应用的初始搭建以及开发过程。
SpringBoot优点?面试题之一
①独立运行;②简化配置;③上手容易;④应用监控;⑤无代码生成和XML配置
二、入门案例
package com.chf.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping("{id}")
public String getById(@PathVariable Integer id){
System.out.println("id -->"+id);
return "Hello SpringBoot";
}
}
三、快速启动
①、对SpringBoot项目打包(执行Maven构建指令package)
②、在jar包的目录下打开终端执行启动指令:java -jar 完整jar包项目名.jar
<!--注意事项是要在SpringBoot中的pom.xml中要依赖此maven插件控制-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
四、起步依赖
一、starter:SpringBoot中常见项目名称,定义了当前项目使用过的所有项目坐标,以达到减少依赖配置的目的
二、parent:所有的SpringBoot项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的。
三、实际开发:使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供,若发生坐标错误,再指定version(要小心版本冲突)
<!--核心配置-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
开启SpringBoot特性的方式(面试题之一):
①继承spring-boot-starter-parent项目;②导入spring-boot-dependencies项目依赖
如何理解SpringBoot的Starters?以及常用的有哪些?(面试题之一)
Starters可以理解为启动器,它包含了一系列可以集成到应用里面的依赖包,可以一站式集成Spring其他技术,而不需要到处找示例代码和依赖包。
SpringBoot Starters的工作如下:
1、SpringBoot在启动时扫描项目所依赖的JAR包,寻找包含spring.factories文件的JAR;
2、根据spring.factories配置加载AutoConfigure类;
3、根据@Conditional注解的条件,进行自动配置并将Bean注入Spring Context。
常用的Starters有:
spring-boot-starter-web Web和RESful应用程序
spring-boot-starter-test 单元测试和集成测试
spring-boot-starter-jdbc 传统的JDBC
spring-boot-starter-hateoas 为服务添加HATEOAS功能
spring-boot-starter-security 添加SpringSecurity进行身份验证和授权
spring-boot-starter-data-jpa 带有Hibeernate的Spring Data JPA
spring-boot-starter-data-rest 带有Spring Data REST 公布简单的REST服务
五、服务器启动程序(面试题)
<!--使用maven依赖管理变更起步依赖项-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--web起步依赖环境中,排除Tomcat起步依赖-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加Jetty起步依赖,版本由SpringBoot的starter控制-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>
Jetty比Tomcat更轻量级,可扩展性更强,谷歌应用引擎以及全面切换为Jetty。
六、配置格式(面试题之一)
SpringBoot提供了多种属性配置方式:
①application.properties
server.port=80
②application.yml
server:
port: 81 注意数字前面有空格
③application.yaml
server:
port: 82 注意数字前面有空格
加载顺序:.properties > .yml > .yaml
注意事项:SpringBoot核心配置文件名为application;
SpringBoot内置属性过多,且所有属性集中在一起修改,在使用时,通过提示键+关键字修改属性。
七、核心注解(面试题之一)
启动类上面的注解是@SpringBootApplication,它也是Spring Boot的核心注解,主要组合包含了以下3个注解:
-
@SpringBootConfiguration:组合了@Configuration注解,实现配置文件的功能。
-
@EnableAutoConfiguration:打开自动配置的功能,也可以关闭某个自动配置的选项,如关闭数据源的自动配置:@SpringBootConfiguration(exclude = {DataSourceAutoConfiguration,class})。
-
@ComponengScan:Spring组件扫描。
二、基础配置
一、yaml配置
YAML(YAML Ain't Markup Language),一种数据序列化格式
优点:①容易阅读;②容易与脚本语言交互;③以数据为核心,重数据轻格式
YAML文件扩展名:.yml(主流)、.yaml
关于yaml语法规则:
①大小写敏感;
②属性层级关系使用多行描述,每行结尾使用冒号结束;
③使用缩进表示层关系,同层级左侧对其,只允许使用空格(不允许使用Tab键);
④属性值前添加空格(属性名与属性值之间使用冒号加空格作为空格);
⑤#表示注释。
核心规则:数据前面要加空格和冒号隔开。
#可以省略控制台输出的SpringBoot登录信息
logging:
level:
root: warn
关于yaml数组数据
数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔。
likes:
- Java
- python
- C++
二、yaml数据读取方式(三种)面试题
第一种:使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名...}
server:
port: 80
lesson: SpringBoot
user:
username : zhangsan
age : 18
tel : 123456
subject:
- Java
- Pyhton
- C++
@RestController
@RequestMapping("/books")
public class BookController {
@Value("${lesson}")
private String lesson;
@Value("${user.username}")
private String name;
@Value("${user.age}")
private Integer age;
}
第二种方式:全部数据封装到Environment对象当中
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private Environment environment;
@GetMapping("{id}")
public String getById(@PathVariable Integer id){
System.out.println(environment.getProperty("lesson"));
System.out.println(environment.getProperty("user.subject[1]"));
return "Hello SpringBoot02";
}
}
第三种方式:自定义对象封装指定数据
package com.chf.domain;
@Component
@ConfigurationProperties(prefix = "user")
public class User {
private String username;
private Integer age;
private Integer tel;
private String[] subject;
//以下省略所有的有参无参构造、getset方法、toString方法
}
package com.chf.controller
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private User user;
@GetMapping("{id}")
public String getById(@PathVariable Integer id){
System.out.println(user);
return "Hello SpringBoot02";
}
}
注:如果在自定义对象封装数据发生了警告,以下是解决方案:
<dependency>
<grounpId>org.springframework.boot</grounpId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
详解关于自定义对象的注解@ConfigurationProperties
-
@ConfigurationProperties注解用于自动配置绑定,可以将配置中的值注入到bean对象上。
-
需要使用@Component组件注解进行注册才能进行绑定(即必须将对象注入到IOC容器中才有配置绑定的功能)
三、多环境启动(面试题之一)
①关于.yml和.yaml后缀的多环节启动配置,只需在其中添加以下代码即可。
#设置启用的环境
spring:
profiles:
active: dev
---
#开发
spring:
config:
activate:
on-profile: dev
server:
port: 80
---
#生产
spring:
config:
activate:
on-profile: pre
server:
port: 81
---
#测试
spring:
config:
activate:
on-profile: test
server:
port: 82
②关于主启动配置文件application.properties(麻烦,了解即可)
spring-profiles.active=pro
环境分类配置文件application-pro.properties
server.port=80
环境分类配置文件application-dev.properties
server.port=81
环境分类配置文件application-test.properties
server.port=82
四、多环境启动命令格式
将项目打包后给前端或者测试人员,他们需要学以下命令在终端使用
注意:打包package之前先clean。
这是在终端的操作指令
java -jar 打包后的项目名.jar --spring.profiles.active=测试/开发/生产 看需要什么用什么
如果遇到端口号冲突,可以直接在这里修改端口号
这是在终端的操作指令
java -jar 打包后的项目名.jar --server.port=xx
综合使用如下
java -jar 打包后的项目名.jar --server.port=xx --spring.profiles.active=测试/开发/生产
五、Maven与SpringBoot兼容
步骤①:Maven中设置多环境属性
<profiles>
<profile>
<id>dev_env</id>
<properties>
<profile.active>dev</profile.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>pro_env</id>
<properties>
<profile.active>pro</profile.active>
</properties>
</profile>
<profile>
<id>test_env</id>
<properties>
<profile.active>test</profile.active>
</properties>
</profile>
</profiles>
②SpringBoot中引用Maven属性
spring:
profiles:
active: ${profile.active}
---
spring:
config:
activate:
on-profile: dev
server:
port: 80
---
spring:
config:
activate:
on-profile: pre
server:
port: 81
---
spring:
config:
activate:
on-profile: test
server:
port: 82
③对资源文件开启对默认占位符的解析
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<userDefaultDelimiters>true</userDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</build>
六、配置文件分类(面试题)
-
SpringBoot中4级配置文件
-
1级:file :config/application.yml 【最高】
-
2级:file :application.yml
-
file即是打开jar包的那个目录
-
3级:classpath:config/application.yml
-
4级:classpath:application.yml 【最低】IDEA中写的文件属于此级
-
-
作用:
-
1级与2级留做系统打包后设置通用属性
-
3级与4级用于系统开发阶段设置通用属性
-
三、整合第三方技术
一、整合JUnit
-
SpringBoot整合JUnit
-
@SpringBootTest class Springboot04TestApplicationTests { @Autowired private BookService bookService; @Test void contextLoads() { bookService.save(); } }
@SpringBootTest 测试类注解
-
作用:设置JUnit加载的SpringBoot启动类
二、整合SSM
-
SpringBoot整合Spring(不存在)
-
SpringBoot整合SpringMVC(不存在)
-
SpringBoot整合Mybatis(主要)
-
步骤①:创建新模块,选择Spring初始化,并配置模块相关基础信息
②:在application.yml中设置数据源参数
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
注意事项:若SpringBoot版本低于2.4.3并且Mysql驱动版本大于8.0,需要在url连接串中配置时区 、
url: jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
③:定义数据库接口与映射配置
package com.chf.dao
@Mapper
public interface BookDao {
@Select("select * from tbl_book where id=#{id}")
Book getById(Integer id);
}
④:测试类中注入dao接口,测试功能
@SpringBootTest
class Springboot05MybatisApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void testGetById() {
Book book = bookDao.getById(2);
System.out.println(book);
}
}
四、基于SpringBoot的SSM整合案例
1、pom.xml
-
配置起步依赖,必要的资源坐标(druid)
2、application.yml
-
设置数据源、端口等
3、配置类
-
全部删除
4、dao
-
设置@Mapper
5、测试类
6、页面
-
放置在resources目录下的static目录下
更多推荐
SpringBoot框架介绍(面试题较多)
发布评论