本篇主要介绍SpringBoot的属性配置方式和内容,及自定义属性的使用。
一、属性配置
1.1 外部命令配置
通过java -jar xx.jar --name="Spring" --server.port=9090
方式来传递参数。【serverport前面是两个横岗】
参数用–xxx=xxx的形式传递。可以使用的参数可以是我们自己定义的,也可以是Spring Boot中默认的参数。
注意:命令行参数在xxx.jar的后面!
可以通过SpringApplication.setAddCommandLineProperties(false)
禁用命令行配置。
1.2 配置文件配置
像我们比较关心的web项目中的端口号、上下文等配置,都可以在应用配置文件(.properties或.yml)中配置:
# 配置数据库
spring.jpa.database = postgresql
# 查询时是否显示日志
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
server.port=8080
#server.address= # bind to a specific NIC
#server.session-timeout= # session timeout in seconds
#server.context-parameters.*= # Servlet context init parameters, e.g. server.context-parameters.a=alpha
# the context path, defaults to '/'
server.context-path= /demo
#server.servlet-path= # the servlet path, defaults to '/'
事实上,我们可以进行配置的参数相当的多,具体可以参照 参数配置
二、自定义属性使用
-
- application.properties 中的配置
##自定义属性
#32位随机字符串
gh.rd-secret=${random.value}
#int类型的随机数字
gh.rd-intValue=${random.int[1024,65536]}
#自定义名字
gh.name=www.loveaocai.com
#属性占位符属性
gh.desc=${gh.name} is a domain name
-
- 方式一:直接获取
@Value(value = "${gh.rd-secret}")
private String randomValue;
// int类型的随机数字
@Value(value = "${gh.rd-intValue}")
private int randomInt;
// 自定义名字
@Value(value = "${gh.name}")
private String name;
// 属性占位符属性 ${jerome.name} is a domain name
@Value(value = "${gh.desc}")
private String desc;
@RequestMapping("testProp")
public Map<String, Object> testProp(){
Map<String, Object> map = new HashMap<>();
map.put("title", "hello world");
map.put("randomValue", randomValue);
map.put("randomInt", randomInt);
map.put("name", name);
map.put("desc", desc);
return map;
}
通过@Value(value = "${gh.name}")
注解可直接获取properties文件里面的值,直接在方法中使用。不过这种方式好像已经不推荐使用了,但也能用的。
-
- 方式二:配置属性类
@ConfigurationProperties(prefix="gh")
public class GhProperties {
private String name;
private String desc;
private String rdSecret;
private Integer rdIntValue;
//getter setter
}
并且在初始化的类中加上配置:
//下面这句话
@EnableConfigurationProperties({GhProperties.class})
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
使用方式如下:
@Autowired
private GhProperties ghProperties;
@RequestMapping("testProp2")
public Map<String, Object> testProp2(){
Map<String, Object> map = new HashMap<>();
map.put("title", "hello world22");
map.put("randomValue", ghProperties.getRdSecret());
map.put("randomInt", ghProperties.getRdIntValue());
map.put("name", ghProperties.getName());
map.put("desc", ghProperties.getDesc());
return map;
}
-
- 效果图
三、多环境配置
我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发、测试、生产等。其中每个环境的数据库地址、服务器端口等等配置都会不同,如果在为不同环境打包时都要频繁修改配置文件的话,那必将是个非常繁琐且容易发生错误的事。
对于多环境的配置,各种项目构建工具或是框架的基本思路是一致的,通过配置多份不同环境的配置文件,再通过打包命令指定需要打包的内容之后进行区分打包,Spring Boot也不例外,或者说更加简单。
在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:
- 效果图
application-dev.properties:开发环境
- application-test.properties:测试环境
application-prod.properties:生产环境
至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。
如:spring.profiles.active=test
就会加载application-test.properties配置文件内容
这里暂时只介绍properties的方式,至于YAML方式暂不讨论。