springboot配置笔记
/** * @SpringBootApplication :是以下三个注解的和 * @SpringBootConfiguration :备注配置类 * @EnableAutoConfiguration :备注开启自动配置 * @ComponentScan :备注包扫描 */ /** * @RestController :以下两个注解的和 * @Controller :备注控制器 * @ResponseBody :备注返回body */ /** * resources :目录结构 * static:存放静态资源(js,css,img) * templates:存放模板页面(springboot默认不支持jsp)(可以使用模板引擎:freemarker,thymeleaf) * application.[properties,yml]:(springboot配置文件) * 比如:server.port:8081(改变端口号) */ <!--web项目相关依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--配置yml时给出提示信息--> <!--方式一: 配置类使用@ConfigurationProperties(prefix = "xxx")标注--> <!--方式二: 配置类使用@Component标注 ,用@Value导入值--> <!--方式三: 配置类使用@PropertySource来指定使用的配置文件(默认使用全局配置文件) 与方式一和二结合使用--> <!--方式四:使用自定义配置文件@ImportResource--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency> ##YML person: #使用随机值 name: 张三${random.uuid} #使用随机值 age: ${random.int(0,80)} #使用给定值 name-age: ${person.name}____${person.age} #指定默认值 id: ${person.next:1} #启动什么环境配置文件 application-prod.yml application-dev.yml spring: profiles: active: dev #表示当前配置是什么环境下生效 spring: profiles: dev ####命令行中指定 #args --spring.profiles.active=prod #vmOptions --DSpring.profiles.active=prod #### 配置文件加载位置 -file:./config/ -file:./ -classpath:/config/ -classpath:/ spring.config.location:默认情况下按照优先级搜搜加载,也可以使用这个参数指定加载位置 server.context-path:在每一个模块的application.properties文件中设置时,访问模块时得加此值 application.properties 可以放在jar包所在目录 @Conditional 指定的条件成立才会添加组件或配置类配置才会生效