springboot-基础
2020-12-21 本文已影响0人
麦大大吃不胖
by shihang.mai
1. 注解
其实就是一个注释.而每个注解起的作用是基于反射实现的
java中的元注解
| 注解 | 作用 | 可选值 |
|---|---|---|
| @Target | 描述该注解的使用范围 | ElementType.xxx |
| @Retention | 描述该注解的生命周期 | RetentionPolicy.xxx |
| @Documented | 描述该注解是否在javadoc文档 | |
| @Inherited | 描述该注解的能否被继承 |
1.1 自定义注解
public @interface MyAnnotation {
/**
* 看起来像方法,但是实际上是使用时填写的参数的key名,默认的key名是value
* 加上default是为了在使用时,不必填写所有的属性值
*/
String name() default "msh";
int age() default 18;
}
2. 获取yaml属性
| @ConfigurationProperties | @Value | |
|---|---|---|
| 功能 | 批量获取 | 一个个获取 |
| 松散绑定 | Y | N |
| SpEL(计算式) | N | Y |
| 数据校验 | Y | N |
| 复杂类型封装 | Y | N |
3. 配置文件优先级
- mshProject/config/application.yml
- mshProject/application.yml
- mshProject/src/main/resources/config/application.yml
- mshProject/src/main/resources/application.yml
4. 静态资源
静态资源存放路径
- classpath:/META-INF/resources/
- classpath:/resources/
- classpath:/static/
- classpath:/public/
路径由下面代码找到
//WebMvcAutoConfiguration->WebMvcAutoConfigurationAdapter.addResourceHandlers()
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
}
public class ResourceProperties {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
.......
}
如果同名资源的话,按上面目录的优先级读取