SpringBoot

开始

延迟初始化

bean不会一开始就初始化,启动快,但是相应的bean配置错误不会在一开始显现出来

1
2
3
spring:
main:
lazy-initialization: true

元数据支持

引入configuration-processor依赖,在yaml中配置也会有相应提示

1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

image-20200904111759672

会生成metadata.json

配置属性

Javabean属性绑定和Constructor绑定

image-20200904113617977

yaml

1
2
3
4
5
6
7
acme:
enabled: false
remote-address: 192.168.45.113
security:
username: root
password: root
roles: [11,22,33]

松散绑定

了解一下

image-20200904114951397

JSR-303属性校验

image-20200904115124824

如果存在内部类,在内部类加上@Valid注解才会生效

@ConfigurationProperties vs. @Value

特征 @ConfigurationProperties @Value
松散绑定 Yes Limited (see note below)有限支持
元数据支持 Yes No
SpEL 表达式 No Yes
复杂类型绑定 Yes No
JSR-303属性校验 Yes No

Async

1
2
3
4
5
6
7
8
9
10
11
12
13
@Service
public class AsyncService {

@Async // 异步任务
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据处理完成!");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
@Controller
public class AsyncController {

@Autowired
private AsyncService asyncService;

@ResponseBody
@GetMapping("hello")
public String hello() {
asyncService.hello();
return "OK";
}
}
1
2
3
4
5
6
7
8
9
@EnableAsync // 开启异步功能
@SpringBootApplication
public class SpringbootAsyncMailScheduledApplication {

public static void main(String[] args) {
SpringApplication.run(SpringbootAsyncMailScheduledApplication.class, args);
}

}

Schedule

1
2
3
4
5
6
7
8
9
@Service
public class ScheduledService {

// 定时任务 cron表达式(5秒执行一次)
@Scheduled(cron = "0/5 * * * * *")
public void printDate(){
System.out.println(new Date());
}
}
1
2
3
4
5
6
7
8
9
@EnableScheduling // 开启定时任务
@SpringBootApplication
public class SpringbootAsyncMailScheduledApplication {

public static void main(String[] args) {
SpringApplication.run(SpringbootAsyncMailScheduledApplication.class, args);
}

}

image-20201015141922807

Mail

评论