springboot配置参数
来源:互联网 发布:拦沙坝计算软件 编辑:程序博客网 时间:2024/05/17 01:24
一、application.properties的位置
1.当前目录的 “/config”的子目录下
2.当前目录下
3.classpath根目录的“/config”包下
4.classpath的根目录下
spring会从classpath下的/config目录或者classpath的根目录查找application.properties或application.yml。
/config优先于classpath根目录
说明:
- 1,2两项适合生产环境,可以直接跟jar包放在同级目录下
- 如果同时在四个地方都有配置文件,配置文件的优先级是从1到4。
- 使用配置文件之后,spring boo启动时,会自动把配置信息读取到spring容器中,并覆盖spring boot的默认配置
SpringBoot的配置方式有很多,它们的优先级如下所示(优先级递减顺序):
- 命令行参数
- 来自java:comp/env的JNDI属性
- Java系统属性(System.getProperties())
- 操作系统环境变量
- RandomValuePropertySource配置的random.*属性值
- jar包外部的
application-{profile}.properties
或application.yml
(带spring.profile)配置文件 - jar包内部的
application-{profile}.properties
或application.yml
(带spring.profile)配置文件 - jar包外部的
application.properties
或application.yml
(不带spring.profile)配置文件 - jar包内部的
application.properties
或application.yml
(不带spring.profile)配置文件 @Configuration
注解类上的@PropertySource
- 通过
SpringApplication.setDefaultProperties
指定的默认属性
因为jar包外部的优先级高,所以可以运行时指定application.properties的位置。
以上配置方式虽然挺多,实际用到的只有一两种。
二、 通过命令行来配置少量项
SpringBoot可以不依赖Tomcat容器,作为单应用启动。这时,可以通过命令行来控制运行参数。
通过命令行来重写和配置环境变量,优先级最高,例如可以通过下面的命令来重写spring boot 内嵌tomcat的服务端口,注意“=”俩边不要有空格
java -jar demo.jar --server.port=9000
如果想要设置多个变量怎么办,可以用json的格式字符串来设置
java -jar demo.jar --spring.application.json='{"foo":"bar"}'
三、 使用@Value
注解
@RestController@RequestMapping("/task")public class TaskController {@Value("${connection.remoteAddress}") private String address;@RequestMapping(value = {"/",""})public String hellTask(@Value("${connection.username}")String name){ return "hello task !!";}}
@Value注解有好几种姿势:
1. #{expression?:default value}
@Value("#{systemProjecties['mongodb.port']?:27017}")private String mongodbPort;@Value("#{config['mongodb.url']?:'127.0.0.1'}")private String mongodbUrl;@Value("#{aBean.age ?:21}")private int age;
2. ${property:default value}
//@propertySource("classpath:/config.properties")//configuration@Value("${mongodb.url:127.0.0.1}")private String mongodbUrl;@Value("#{'${mongodb.url:172.0.0.1}'}")private String mongoUrl;@Value("#config['mongodb.url']?:'127.0.0.1'")private String mogodbUrl;
配置文件config.property如下:
mogodb.url = 1.2.3.4mogodb.db = hello
3. 注意
Must register a static PropertySourcesPlaceholderConfiger bean in either XML or annotation ,so that Spring @Value
konw how to interpret ${}
//@PropertySource("classpath:/config.properties}")//@Configuration @Bean public static PropertySourcesPlaceholderConfigurer propertyConfigIn() { return new PropertySourcesPlaceholderConfigurer(); }
四、 属性的引用
myapp.name=springmyapp.desc=${myapp.name} niceSpringBoot提供以下特殊引用:my.secret=${random.value}my.number=${random.int}my.bignumber=${random.long}my.number.less.than.ten=${random.int(10)}my.number.in.range=${random.int[1024,65536]}
五、 自定义配置信息
@Componentpublic class SystemConfig { private static Properties props ; public SystemConfig(){ try { Resource resource = new ClassPathResource("/application.properties");// props = PropertiesLoaderUtils.loadProperties(resource); } catch (IOException e) { e.printStackTrace(); } } /** * 获取属性 * @param key * @return */ public static String getProperty(String key){ return props == null ? null : props.getProperty(key); } /** * 获取属性 * @param key 属性key * @param defaultValue 属性value * @return */ public static String getProperty(String key,String defaultValue){ return props == null ? null : props.getProperty(key, defaultValue); } /** * 获取properyies属性 * @return */ public static Properties getProperties(){ return props; }}//用的话,就直接这样子String value = SystemConfig.getProperty("key");
六、SpringBoot自带的配置属性
常用属性如日志,端口配置
# LOGGINGlogging.path=/var/logslogging.file=myapp.loglogging.config= # location of config file (default classpath:logback.xml for logback)logging.level.*= # levels for loggers, e.g. "logging.level.org.springframework=DEBUG" (TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF)# EMBEDDED SERVER CONFIGURATION (ServerProperties)server.port=8080server.address= # bind to a specific NICserver.session-timeout= # session timeout in secondsserver.context-parameters.*= # Servlet context init parameters, e.g. server.context-parameters.a=alphaserver.context-path= # the context path, defaults to '/'server.servlet-path= # the servlet path, defaults to '/'
更多属性参见官网文档
@ContextConfiguration
@ContextConfiguration(locations={"classpath*:mongodb.xml"})
七、属性名匹配规则
例如有如下配置对象:
@Component@ConfigurationProperties(prefix="person")public class ConnectionSettings { private String firstName;}
firstName可以使用的属性名如下:
person.firstName
,标准的驼峰式命名person.first-name
,虚线(-)分割方式,推荐在.properties和.yml配置文件中使用
PERSON_FIRST_NAME
,大写下划线形式,建议在系统环境变量中使用
参考:
https://www.cnblogs.com/weidiao/p/6820923.html
- springboot配置参数
- Springboot Web应用中服务器配置参数ServerProperties的加载
- springboot配置
- springboot配置
- SpringBoot配置
- 【SpringBoot】SpringBoot核心-基本配置
- 【SpringBoot】SpringBoot核心-外部配置
- SpringBoot-01SpringBoot配置FastJson
- SpringBoot自定义参数注解
- Springboot获取请求参数
- springboot中使用定时任务,异步调用,自定义配置参数(八)
- SpringBoot中自定义properties文件配置参数并带有输入提示
- springboot log4j配置
- springboot 配置oracle
- SpringBoot配置属性系列
- 【springboot】自定义模板配置
- springboot log4j2配置
- SpringBoot配置积累
- 动态行转列sql
- 集合框架之List集合详解
- java中定义枚举类
- java 泛型变量的限定
- MTK_FAQ_Telephony
- springboot配置参数
- win10家庭版怎样administrator登陆
- 软考常考知识点整理-项目风险管理计划
- 关于一个小学算数引发的思考
- linux 安装mysql
- 用Excel理解神经网络
- Java中的Infinity和NaN
- Spring4+Atomikos3.9+mysql6.06使用注解实现跨库事务
- 使用patch接口连接两个ovs网桥