背景 SpringBoot配置文件可以放置在多种路径下,不同路径下的配置优先级有所不同 可放置目录(优先级从高到低) 1.file:./config/ (当前项目路径config目录下); 2.file:./ (当前项目路径下); 3.classpath:/config/ (类路径config目录下); 4.classpath:/ (类路径config下). 优先级由高到底,高优先级的配置会覆盖低优先级的配置 配置方法 在tomcat根目录下新建一个名为config的文件夹,放入配置文件,如application-prod.yml 将刚刚新建的config文件夹添加到tomcat的classpath中 打开tomcat/conf/catalina.properties文件,在common.loader处添加"${catalina.home}/config" 如此即可 war 包在 tomcat 中加载外部配置文件 Tomcat
Controller 添加注解 @ConditionalOnProperty(name = "TestController.disable", havingValue = "false") 其中 name 用来从 application.yml 配置文件中读取某个属性值,如果该值为空,则返回***false***;如果值不为空,则将该值与***havingValue***指定的值进行比较,如果一样则返回***true***;否则返回***false***。 如果返回值为***false***,则该 Controller 不生效;为***true***则生效。 配置文件 在 dev 环境下配置该属性,则可实现只有 dev 环境下该 Controller 才生效 TestController: disable: false SpringBoot 通过配置控制 Controller 是否生效 SpringBoot
添加依赖 <!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> 配置不同环境是否启用 在相应配置文件中添加(以yml为例) swagger: enable: true 创建配置类 @Configuration @EnableSwagger2 public class SwaggerConfig.... SpringBoot 集成 Swagger SpringBoot
添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 该依赖里默认包含了spring-data-redis和Jedis依赖,见这里 Spring Boot 1.5.x 整合Redis SpringBoot