# springbootDemo **Repository Path**: smartwan/springboot-demo ## Basic Information - **Project Name**: springbootDemo - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-14 - **Last Updated**: 2026-02-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # springboot 常用整理 ## application.properties 1. 正确书写 list、map等类型 ```properties person.name=zhangsan person.age=12 person.happy=true person.birth=2022/10/01 # Map类型配置(正确写法) person.maps.k1=v1 person.maps.k2=v2 # List类型配置(推荐写法,更清晰) person.lists[0]=code person.lists[1]=girl person.lists[2]=music person.dog.name=acai person.dog.age=3 ``` 2. 在类中引用: - 方式之一:@Value ```java public class Dog { // @Value 将外部配置注入到类的字段中 // @Value("阿黄") 直接注入常量 @Value("${app.dog.name}") // 注入配置文件 private String name; @Value("${app.dog.age:1}") // 防止配置缺少:可以给一个默认值 private Integer age; } ``` - 方式之二:@ConfigurationProperties 一次性导入,避免一次次写入: @Value("${name}")、@Value("${age}")、@Value("${happy}") 3. 多环境切换: - 权重 config 下的application.yaml 1 最低,根目录下的application.yaml 2, resource/config 下的 application.yaml 3, resource 下的 application.yaml 4 最高 - 通过 application-test.properties 代表测试环境配置 、 application-dev.properties 代表开发环境配置 - 在 application.properties 中通过 spring.profiles.active=dev 指定环境 - spring.config.location 来改变默认的配置文件位置: java -jar spring-boot-config.jar --spring.config.location=F:/application.properties ## 数据校验原理 1. JSR 303 - 通过 @Validated 定义字段验证 - 常用有 @NotNull 、@Size 、@Past 、@Pattern 、@Length、@Future、@AssertFalse、@AssertTrue、@Null 、@NotEmpty ## 静态资源 1. 通过ip 能直接访问resources下的静态资源 - 全局搜索:CLASSPATH_RESOURCE_LOCATIONS,可知包含四个路径 - 优先级:resources/resoureces > resoureces/static > resoureces/public 2. 自定义静态资源路径 - 在 application.properties文件中 设置: - spring.resources.static-locations=classpath:/coding/,classpath:/ss/ ## springmvc 自动配置 1. com.example.demo下 新建config目录。 ```java package com.example.demo.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class MyMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); registry.addViewController("/index.html").setViewName("index"); } } ``` - MyMvcConfig 是 Spring MVC 的自定义配置类,实现 WebMvcConfigurer 接口可以自定义 Spring MVC 的规则(比如静态资源、拦截器、视图解析器、跨域等) 它的生效完全依赖 Spring 的自动装配机制,不需要写任何 “调用代码”。 - 他替代了:IndexController ,如下: ```java @Controller public class IndexController{ @RequestMapping({"/","/index.html"}) public String index() { return "index"; } } ``` 2. 直接启动项目,即可访问index.html页面: - http://localhost:8081/index.html