SpringBoot全局配置文件默认为src/main/resources下的application.properties,另外它还可以重命名为.yml格式(即SpringBoot对着两种格式均支持)。
修改默认配置
如修改SpringBoot内嵌Tomcat的启动端口为9080(.yml格式)
server:
port: 9080
启动项目即可在控制台启动日志中看到
2018-06-24 17:42:25.784 INFO 2658 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9080 (http) with context path ''
这时在浏览器输入localhost:9080即可正常访问
附Spring Boot 全部配置项 SpringBoot Common application properties
自定义属性配置
我们也可以在SpringBoot配置文件中自定义属性配置,如
com:
example:
girl:
name: baby
age: 18
cupSize: B
然后通过@Value("${属性名}")注解来加载对应的配置属性
com/example/springbootconfiguration/controller/GirlController.java
package com.example.springbootconfiguration.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GirlController {
@Value("${com.example.girl.name}")
private String girlName;
@Value("${com.example.girl.age}")
private Integer girlAge;
@Value("${com.example.girl.cupSize}")
private String girlCupSize;
@GetMapping("/girl")
public String getGirlInfo() {
return "girlName: " + girlName + " girlAge: " + girlAge + " girlCupSize: " + girlCupSize;
}
}
启动工程,访问:localhost:9080/girl,浏览器显示:
girlName: baby girlAge: 18 girlCupSize: B
属性注入成功
属性配置间的引用
在SpringBoot全局配置文件中的各个属性之间可以通过直接引用来使用
com:
example:
girl:
name: baby
age: 18
cupSize: B
desc: name:${com.example.girl.name} age:${com.example.girl.age} cupSize:${com.example.girl.cupSize}
同样可以使用@Value注解将girl.desc属性配置注入到某一属性中,如
package com.example.springbootconfiguration.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GirlController {
@Value("${com.example.girl.name}")
private String girlName;
@Value("${com.example.girl.age}")
private Integer girlAge;
@Value("${com.example.girl.cupSize}")
private String girlCupSize;
@Value("${com.example.girl.desc}")
private String girlDesc;
@GetMapping("/girl")
public String getGirlInfo() {
// return "girlName: " + girlName + " girlAge: " + girlAge + " girlCupSize: " + girlCupSize;
return girlDesc;
}
}
再次启动工程,访问:localhost:9080/girl,浏览器显示:
name:baby age:18 cupSize:B
将属性配置赋给实体类
当我们属性配置很多的时候,使用@Value注解一个一个的注入将会变得很繁琐,这时SpringBoot提供了将属性配置与实体类结合的方式,具体先来看一下SpringBoot中官方的使用如org.springframework.boot.autoconfigure.data.redis.RedisProperties
package org.springframework.boot.autoconfigure.data.redis;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(
prefix = "spring.redis"
)
public class RedisProperties {
private int database = 0;
private String url;
private String host = "localhost";
private String password;
private int port = 6379;
private boolean ssl;
private int timeout;
private RedisProperties.Pool pool;
private RedisProperties.Sentinel sentinel;
private RedisProperties.Cluster cluster;
...
}
对于上面我们自己关于girl的一些配置,同理我们可以创建一个GirlProperties类,如
package com.example.springbootconfiguration.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Component
@ConfigurationProperties(prefix = "com.example.girl")
public class GirlProperties {
private String name;
private Integer age;
private String cupSize;
// ... getter and setters
}
当你在Idea中敲完这些代码Idea可能会提示Spring Boot Configuration Annotation Processor not found in classpath
Spring Boot Configuration Annotation Processor not found提示点击Open Documentation会打开Generating Your Own Metadata by Using the Annotation Processor页面
嗯,主要是少了个依赖spring-boot-configuration-processor
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
如何使用?在需要使用的类上加@EnableConfigurationProperties
注解,同时使用@Autowired
注解注入即可,如
package com.example.springbootconfiguration.controller;
import com.example.springbootconfiguration.properties.GirlProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableConfigurationProperties(value = {GirlProperties.class})
public class GirlController {
@Autowired
private GirlProperties girlProperties;
@GetMapping("/girl2")
public GirlProperties getGirlInfo2() {
return girlProperties;
}
}
再次启动工程,访问:localhost:9080/girl2,浏览器显示:
返回结果
使用随机值
Spring Boot的属性配置文件中可以通过${random}
来产生随机int、long、uuid或者string字符串,来支持属性的随机值。
比如我们给girl随机来个年龄
age: ${random.int}
自定义配置文件
虽然SprinBoot提供了application.properties或application.yml全局配置文件,但有时我们还是需要自定义配置文件,如将上文关于girl的属性配置提取到girl.properties文件中
girl.name: baby
girl.age: 18
girl.cupSize: B
GirlProperties2.java
package com.example.springbootconfiguration.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:girl.properties")
@ConfigurationProperties(prefix = "girl")
public class GirlProperties2 {
private String name;
private Integer age;
private String cupSize;
}
GirlController.java
package com.example.springbootconfiguration.controller;
import com.example.springbootconfiguration.properties.GirlProperties;
import com.example.springbootconfiguration.properties.GirlProperties2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableConfigurationProperties(value = {GirlProperties.class, GirlProperties2.class})
public class GirlController {
@Autowired
private GirlProperties2 girlProperties2;
@GetMapping("/girl3")
public GirlProperties2 getGirlInfo3() {
return girlProperties2;
}
}
再次启动工程,访问:localhost:9080/girl3,浏览器显示:
返回结果
多环境配置
实际开发中可能会有不同的环境,有开发环境、测试环境、生成环境。对于每个环境相关配置都可能有所不同,如:数据库信息、端口配置、本地路径配置等。
如果每次切换不同环境都需要修改application.properties,那么操作是十分繁琐的。在spring boot中提供了多环境配置,使得我们切换环境变得简便。
在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:
- application-test.yml:测试环境
server:
servlet:
context-path: /test
- application-dev.yml:开发环境
server:
servlet:
context-path: /dev
- application-prod.yml:生产环境
server:
servlet:
context-path: /prod
激活profile,在application.yml文件中通过spring.profiles.active属性来设置,其值对应{profile}值,如
spring:
profiles:
active: dev
这时启动应用访问localhost:9080/girl3就访问不到了,需访问localhost:9080/dev/girl3才行
用命令运行jar包启动应用的时候,可以指定相应的配置.
mvn package
java -jar target/spring-boot-configuration-0.0.1-SNAPSHOT.jar --spring.profiles..active=dev
注:命令行参数这种jar包指定参数启动应用的方式,可能是不安全的,我们可以在启动类中设置禁止这种方式启动应用,如下:
package com.example.springbootconfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
public class SpringBootConfigurationApplication {
public static void main(String[] args) {
// SpringApplication.run(SpringBootConfigurationApplication.class, args);
SpringApplication application = new SpringApplication(SpringBootConfigurationApplication.class);
application.setAddCommandLineProperties(false);
application.run(args);
}
}
网友评论