spring-boot笔记-属性配置(二)
2017-06-01 16:28
471 查看
本篇主要介绍SpringBoot的属性配置方式和内容,及自定义属性的使用。
参数用–xxx=xxx的形式传递。可以使用的参数可以是我们自己定义的,也可以是Spring Boot中默认的参数。
注意:命令行参数在xxx.jar的后面!
可以通过
事实上,我们可以进行配置的参数相当的多,具体可以参照 参数配置
方式一:直接获取
通过
方式二:配置属性类
并且在初始化的类中加上配置:
使用方式如下:
效果图
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/05/e79df7016cb727eccabc6e17e77bf59c)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/05/396c5ec083c34b980a98419eb1ab49d3)
对于多环境的配置,各种项目构建工具或是框架的基本思路是一致的,通过配置多份不同环境的配置文件,再通过打包命令指定需要打包的内容之后进行区分打包,Spring Boot也不例外,或者说更加简单。
在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:
application-dev.properties:开发环境
application-test.properties:测试环境
application-prod.properties:生产环境
至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。
如:
这里暂时只介绍properties的方式,至于YAML方式暂不讨论。
一、属性配置
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方式暂不讨论。
相关文章推荐
- Spring Boot学习笔记-项目属性的简单配置
- Spring boot 学习笔记(1)——hello world创建,属性application.properties、 yml配置
- spring boot 学习笔记(二):项目属性配置
- Spring boot将配置属性注入到bean类中
- Spring Boot application.propertis配置文件的相关通用属性
- Spring Boot 配置文件 - 属性大全
- SpringBoot配置属性之NOSQL
- [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)
- Spring 学习笔记 4. 尚硅谷_佟刚_Spring_属性配置细节
- 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】(
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
- Spring boot将配置属性注入到bean 专题
- SpringBoot配置属性之DataSource
- Spring Boot 属性配置和使用
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
- Spring Boot 属性配置和使用
- SpringBoot配置属性系列
- 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】
- Spring Boot 属性配置和使用(转)
- Spring Boot属性配置文件详解