# SpringBoot-Study
**Repository Path**: thread-git/spring-boot-study
## Basic Information
- **Project Name**: SpringBoot-Study
- **Description**: SpringBoot学习中的一些demo记录
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2022-06-26
- **Last Updated**: 2022-06-27
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# SpringBoot的学习之路
#### 自动配置:
- 自动配置Tomcat
- 自动配置SpringMVC
- 引入SpringMVC全套组件
- 自动配置SpringMVC常用组件
- 自动配置Web常见功能,如:字符编码问题
- SpringBoot配置了所有web开发的常见场景
- 默认包结构
- 主程序所在包以及下面的所有子包的组件都会被默认扫描
- 无需以前的包扫描配置
- 改变扫描路径,@SpringBootApplication(scanBasePackages=“完整包名”)
- 各种配置有默认值
- 默认配置最终都会映射到某个类
- 按需加载所有自动配置项
- 非常多的starter
- 所有引入的配置都会加载
- 所有的自动配置都在spring-boot-autoconfigure包里
##### pom.xml
- spring-boot-dependencies:核心依赖在这里
- 在引入依赖时不需要指定版本号,因为有这些依赖的版本仓库
##### 启动器
这是spring自动配置的核心依赖
```xml
org.springframework.boot
spring-boot-starter
```
- 启动器:就是SpringBoot的启动场景;
- 例如spring-boot-starter-web就会自动导入所有web环境的依赖
- springboot会把所有的功能场景变成一个个的启动器
##### 主程序
```java
//@SpringBootApplication 标注这个类是是springboot 的应用 启动类下所有资源导入
@SpringBootApplication
public class Springboot01HelloworldApplication {
// 启动springboot应用
public static void main(String[] args) {
SpringApplication.run(Springboot01HelloworldApplication.class, args);
}
}
```
注意:这个run方法返回的是ioc容器
主程序所在包及其下面的所有子包里的组件都会被默认扫描
##### 注解
```java
@SpringBootConfiguration :springboot配置
@Configuration :spring配置类
@Component :这也是spring的一个组件
@EnableAutoConfiguration :自动配置
@AutoConfigurationPackage :自动配置包
@Import(AutoConfigurationImportSelector.class) :导入选择器
List configurations = getCandidateConfigurations(annotationMetadata, attributes); :获取所有的配置
```
获取候选配置
```java
protected List getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
+ "are using a custom packaging, make sure that file is correct.");
return configurations;
}
```
## Shiro
由Subject(用户) SecurityManager(管理所有用户) Realm(连接数据)组成。
## 任务
##### 异步任务
加个注解开启
```java
@SpringBootApplication
@EnableAsync
public class Springboot09TaskApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot09TaskApplication.class, args);
}
}
```
在异步方法上加个注解就行了
```java
@Service
public class AsyncService {
//异步方法
@Async
public void hello(){
System.out.println("数据处理中");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据处理完毕");
}
}
```
##### 邮件任务
先加个依赖
```xml
org.springframework.boot
spring-boot-starter-mail
```
配置文件
```properties
spring.mail.username=邮箱名@qq.com
spring.mail.password=密钥
spring.mail.host=smtp.qq.com
# 开启加密
spring.mail.properties.mail.smtl.ssl.enable=true
```
简单的demo
```java
@Autowired
JavaMailSenderImpl mailSender;
//简单邮件
@Test
void contextLoads() {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setSubject("这是一个主题");
simpleMailMessage.setText("这是正文");
simpleMailMessage.setTo("邮箱名@qq.com");
simpleMailMessage.setFrom("邮箱名@qq.com");
mailSender.send(simpleMailMessage);
}
```
复杂邮件的demo
```java
//复杂邮件
@Test
void contextLoads1() throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setSubject("测试邮件标题");
helper.setText("测试正文 测试测试
", true);
//附件
helper.addAttachment("1.jpg", new File("C:\\Users\\Administrator\\Desktop\\1.jpg"));
helper.setTo("1@qq.com");
helper.setFrom("1@qq.com");
mailSender.send(helper.getMimeMessage());
}
```
##### 定时任务
```java
@EnableScheduling //定时任务注解
@SpringBootApplication
public class Springboot09TaskApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot09TaskApplication.class, args);
}
}
```
定时任务demo
```java
@Service
public class ScheduledService {
//cron表达式
//秒 分 时 日 月 周几
@Scheduled(cron = "20 57 17 * * 0-7")
public void hello() {
System.out.println("执行定时任务");
}
}
```