- 一、SpringBoot配置
- 1、配置文件
- 2、给属性赋值的几种方式
- 3、多环境配置及配置文件位置
- 4、整合其他框架
- 1)整合Junit
- 2)整合redis
- 3)整合MyBatis
- 二、SpringBoot自动配置
- 1、condition
- 2、切换内置Web服务器
- 3、Enable*注解
- 4、自动装配原理
- 三、SpringBoot监听机制
- 1、事件监听
- 2、监控使用
- 3、Spring Boot Admin
- 四、SpringBoot Web开发
- 1、静态资源导入
- 2、Thymeleaf模板引擎
- 五、SpringBoot项目部署
- 2、Thymeleaf模板引擎
- 五、SpringBoot项目部署
SpringBoot使用一个全局的配置文件,配置文件的名称是固定的
- application.properties
- 语法结构:key=value
- application.yml
- 语法结构:key:(空格)value
配置文件的作用:修改SpringBoot自动配置的默认值,因为SpringBoot在底层都给我们自动配置好了
# 对空格的要求非常高
name: yanheng
# 对象
student1:
name: cyh
age: 14
# 行内写法
student2: {name: cyh, age: 14}
# 数组
pets:
- cat
- dog
- pig
pets2: [cat,dog,pig]
server.port=8081 student.name=cyh student.age=142、给属性赋值的几种方式
@ConfigurationProperties
将配置文件中配置的每一个属性的值,映射到这个组件中;
告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
参数prefix=”person“:将配置文件中的person下面的所有属性一一对应
只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能
org.springframework.boot spring-boot-configuration-processor true
@Repository
@ConfigurationProperties(prefix = "person")
public class Person {
private Integer id;
private String name;
private String hello;
private Dog dog;
}
person:
id: ${random.uuid}
name: cyh
hello: hha
dog:
name: ${person.hello:hello}旺财
age: 3
PropertySource
@PropertySource(value = "classpath:application.properties")
public class Dog {
@Value("${name}")
private String name;
private Integer age;
}
name=cccc
引入environment
@Resource
private Environment env;
@RequestMapping("/hello")
public String hello(){
String property = env.getProperty("server.port");
System.out.println(property);
return "index";
}
对比
- @ConfigurationPropertie只需要写一次即可,value则需要每一个字段都添加
- yaml支持松散绑定
- yaml里last-name和java里的lastName可以绑定
- yaml支持JSR303数据校验
- 可以在字段增加一层过滤器验证,保证数据的合法性
- yaml支持复杂类型封装,可以封装对象
@Repository
@ConfigurationProperties(prefix = "person")
@Validated//JSR303数据校验
public class Person {
private String id;
@Email(message = "邮箱错了!!")//数据不合法则控制台提示邮箱错了
private String name;
private String hello;
private Dog dog;
3、多环境配置及配置文件位置
优先级
properties>yaml
- 项目路径下的config文件夹配置文件
- 项目路径下的配置文件
- 资源路径下的config文件夹配置文件
- 资源路径下的配置文件
打成jar包之后不会包含项目根路径的config文件夹(不属于springboot的基本项目结构),故使用jar包运行时项目根路径下的config中的配置会失效
多环境配置
默认执行application.properties
- profile用来完成不同环境下,配置动态切换功能的
- profile配置方式
- 多profile文件方式:提供多个配置文件,每一个代表一种环境
- application-dev.properties/yaml开发环境
- application-pro.properties/yaml生产环境
- application-test.properties/yaml测试环境
- yaml多文档方式
- 在yaml中使用—分隔不同配置
- 通过spring.config.activate.on-profile:pro/dev/test区分各个分区
- 多profile文件方式:提供多个配置文件,每一个代表一种环境
- profile的激活方式
- 配置文件:在配置文件(application.properties或yaml中的独立分区)中配置:spring.profiles.active=dev
- 虚拟机参数:在VM options指定:-Dspring.profiles.active=dev
- 命令行参数:java-jar xxx.jar --spring.profiles.active=dev
- 使用外部配置文件
- 在命令行参数:java-jar xxx.jar --spring.config.location=e://application.properties
- 配置文件放在和jar包同级的目录,运行jar包时配置文件会被自动读取
- 配置文件放在和jar包同级的config文件夹下,运行jar包时配置文件会被自动读取,优先级大于上者
实现步骤:
- 搭建SpringBoot工程
- 引入starter-test起步依赖
- 编写测试类
- 添加测试相关注解
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes=启动类.class)
- 编写测试方法
org.springframework.boot spring-boot-starter-test test junit junit test
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootApplication.class)
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testAdd(){
userService.add();
}
}
@SpringBootTest中的classes参数为springboot启动类的字节码文件
如果该Test文件所在的包和springboot启动类所在包一致或为其子包(都为cn.ken),则可以省略该参数
2)整合redis实现步骤:
- 搭建SpringBoot工程
- 引入redis起步依赖
- 配置redis相关属性(默认本机6379端口)
- 注入RedisTemplate模板
- 编写测试方法,测试
@SpringBootTest
class SpringbootRedisApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void testSet() {
redisTemplate.boundValueOps("name").set("cyh");
}
@Test
void testGet() {
Object name = redisTemplate.boundValueOps("name").get();
System.out.println(name);
}
}
需先运行redis-server.exe
3)整合MyBatis实现步骤:
- 搭建SpringBoot工程
- 引入MyBatis起步依赖,添加mysql驱动
- 编写DataSource和MyBatis相关配置
- 定义表和实体类
- 编写dao和mapper文件/纯注解开发
- 测试
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?serverTimeZone=UTC
username: root
password: 129496
mybatis:
#config-location: #指定mybatis的核心配置文件
mapper-locations: classpath:mapper
@Component
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("CommandLineRunner");
}
}
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner");
}
}
# resources下的META-INF目录下的spring.factories org.springframework.boot.SpringApplicationRunListener=cn.ken.listener.MySpringApplicationRunListener org.springframework.context.ApplicationContextInitializer=cn.ken.listener.MyApplicationContextInitializer2、监控使用
使用步骤:
-
导入依赖坐标
-
org.springframework.boot spring-boot-starter-actuator
-
-
访问http://localhost:8080/actuator
-
配置
# info下的信息 info.name=zhangsan info.age=13 # 开启健康检查的完整信息 # 默认关闭,因为上线时展示所有信息有风险 management.endpoint.health.show-details=always # 将所有监控的endpoint暴露出来 management.endpoints.web.exposure.include=*3、Spring Boot Admin
- Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序
- Spring Boot Admin有两个角色,客户端(Client)和服务端(Server)
- 应用程序作为Spring Boot Admin Client向Spring Boot Admin Server注册
- Spring Boot Admin Server的UI界面将Spring Boot Admin Client的Actuator Endpoint上的一些监控信息进行展示
使用步骤:
- admin-server:
- 创建admin-server模块
- 导入依赖坐标admin-starter-server
- 在引导类上启用监控功能@EnableAdminServer
- admin-client:
- 创建admin-client模块
- 导入依赖坐标admin-starter-client
- 配置相关信息:server地址等
- 启动server和client程序,访问server
server.port=8081 # 开启健康检查的完整信息 # 默认关闭,因为上线时展示所有信息有风险 management.endpoint.health.show-details=always # 将所有监控的endpoint暴露出来 management.endpoints.web.exposure.include=* # 注册到server上(url即为server的地址) spring.boot.admin.client.url=http://localhost:8080
// server的设置
@EnableAdminServer
@SpringBootApplication
public class SpringbootAdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootAdminServerApplication.class, args);
}
}
四、SpringBoot Web开发
1、静态资源导入
在SpringBoot,我们可以使用以下方式处理静态资源
- webjars:localhost:8080/webjars/
- public、static、resources:localhost:8080/
localhost:8080/webjars/jquery/3.4.1/jquery.js
localhost:8080/1.js
优先级:resources>static>public
2、Thymeleaf模板引擎org.thymeleaf thymeleaf-spring5 org.thymeleaf.extras thymeleaf-extras-java8time
随后将html页面放在templates文件夹下,将静态资源放在static文件夹下
templates下的资源不能直接访问,只能通过控制器
默认情况下:
public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html";五、SpringBoot项目部署
SpringBoot项目开发完毕后,支持两种方式部署到服务器:
- jar包(官方推荐)
- war包
[外链图片转存中…(img-JUs0BRjA-1652886495933)]
localhost:8080/1.js
优先级:resources>static>public
2、Thymeleaf模板引擎org.thymeleaf thymeleaf-spring5 org.thymeleaf.extras thymeleaf-extras-java8time
随后将html页面放在templates文件夹下,将静态资源放在static文件夹下
templates下的资源不能直接访问,只能通过控制器
默认情况下:
public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html";五、SpringBoot项目部署
SpringBoot项目开发完毕后,支持两种方式部署到服务器:
- jar包(官方推荐)
- war包



