1.SpringBoot
入门
https://www.bilibili.com/video/BV14z4y1N7pg?p=3&vd_source=d064f8442bd675d75083056b0c1ec7aa
+ 创建Maven
工程
+ 导入spring-boot-starter-web
起步依赖
+ 编写Controller
+ 提供启动类
2.配置文件格式
application.properties
server.port=9090
server.servlet.context-path=/start
application.yml
(更常用)
server:
port: 9191
servlet:
context-path: /start2
3.yml
配置信息书写与获取
- 书写:
email:
user: 1542@qq.com
code: jsiajida
host: smtp.qq.com
auth: true
- 获取
@Value("${键名}")
@Component
public class EmailProperties {
@Value("${email.user}")
public String user;
@Value("${email.code}")
public String code;
@Value("${email.host}")
public String host;
@Value("${email.auth}")
private boolean auth;
}
+ `@ConfigurationProperties(prefix = "前缀")`
@Component
@ConfigurationProperties(prefix = "email")
public class EmailProperties {
public String user;
public String code;
public String host;
private boolean auth;
}
4.springboot
整合mybatis
https://www.bilibili.com/video/BV14z4y1N7pg/?p=7&vd_source=d064f8442bd675d75083056b0c1ec7aa
5.Bean
注册
如果要注册的bean
对象来自于第三方(不是自定义),是无法使用@Component
及衍生注解声明bean
的
https://www.bilibili.com/video/BV14z4y1N7pg/?p=9&vd_source=d064f8442bd675d75083056b0c1ec7aa
方法:@Import
- 导入配置类
- 导入
ImportSelector
接口实现类 @EnableXxxx
注解,封装@Import
注解
6.注册条件
SpringBoot
提供了设置注册生效条件的注解@Conditional
@ConditionalOnProperty
:配置文件中存在对应的属性,才声明该bean
@ConditionalOnMissingBean
:当不存在当前类型的bean
时,才声明该bean
@ConditionalOnClass
:当前环境存在指定的这个类时,才声明该bean