-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path学习笔记.txt
81 lines (64 loc) · 3.58 KB
/
学习笔记.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
一、后台校验
1.1 spring-boot-starter-web中已经集成了hibernate-validator后台校验框架,直接使用即可
1.2 @RequestMapping(value = "/save2", method = RequestMethod.GET)
public User save2(@Validated User user) {
return userJPA.save(user);
}
传入参数上放@Validated注解会在参数传入时校验,否则使用时再校验
1.3 @Validated和@Valid区别
参考: https://blog.csdn.net/qq_27680317/article/details/79970590
Spring Validation验证框架对参数的验证机制提供了@Validated(Spring's JSR-303规范,是标准JSR-303的一个变种),javax提供了@Valid(标准JSR-303规范),配合BindingResult可以直接提供参数验证结果。
在检验Controller的入参是否符合规范时,使用@Validated或者@Valid在基本验证功能上没有太多区别。但是在分组、注解地方、嵌套验证等功能上两个有所不同:
分组功能:@Validated有,@Valid没有
注解位置:@Validated不能用于字段
嵌套验证:
@Validated:用在方法入参上无法单独提供嵌套验证功能。不能用在成员属性(字段)上,也无法提示框架进行嵌套验证。能配合嵌套验证注解@Valid进行嵌套验证。
@Valid:用在方法入参上无法单独提供嵌套验证功能。能够用在成员属性(字段)上,提示验证框架进行嵌套验证。能配合嵌套验证注解@Valid进行嵌套验证。
二、springboot启动
2.1@SpringBootApplication等于以下三个注解
@EnableAutoConfiguration:
@ComponentScan
@Configuration
排除某种自动配置:@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
实际上,只要有@EnableAutoConfiguration注解,就能正常启动了
但是由于没有扫描项目中相关组件,所以会找不到对应URL,访问失败
2.2自定义启动球配置
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MySpringConfiguration.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
或者
new SpringApplicationBuilder()
.sources(Parent.class)
.child(Application.class)
.bannerMode(Banner.Mode.OFF)
.run(args);
2.3 启动之前执行相关
注册实现ApplicationRunner 或CommandLineRunner的bean
@Component
public class MyBean implements CommandLineRunner {
public void run(String... args) {
// Do something...
}
}
三、spring boot新的事件
ApplicationStartingEvent is sent at the start of a run but before any processing, except for the registration of listeners and initializers.
ApplicationEnvironmentPreparedEvent is sent when the Environment to be used in the context is known but before the context is created.
ApplicationPreparedEvent is sent just before the refresh is started but after bean definitions have been loaded.
ApplicationStartedEvent is sent after the context has been refreshed but before any application and command-line runners have been called.
ApplicationReadyEvent is sent after any application and command-line runners have been called. It indicates that the application is ready to service requests.
ApplicationFailedEvent is sent if there is an exception on startup.
添加方式:
SpringApplication.addListeners(…)
SpringApplicationBuilder.listeners(…)
四、读取配置
@Value("${property}")
或者
@ConfigurationProperties("acme") 读取配置到类,支持嵌套
@EnableConfigurationProperties(AcmeProperties.class) 注册配置类
使用时直接注入
@Autowired
private final AcmeProperties properties;
五、lombok使用注意
lombok在idea中使用,需要下载安装相应的插件,否则get/set还是会报错的