Spring Boot 属性配置&自定义属性配置
在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配置足以满足正常的功能开发。
如果需要修改自定义修改默认配置,spring boot 提供了很简便的方法,只需要在application.properties 中添加修改相应的配置。(spring boot启动的时候会读取application.properties这份默认配置)
一、修改默认配置
例1、spring boot 开发web应用的时候,默认tomcat的启动端口为8080,如果需要修改默认的端口,则需要在application.properties 添加以下记录:
server.port=8888
重启项目,启动日志可以看到:Tomcat started on port(s): 8888 (http) 启动端口为8888,浏览器中访问 http://localhost:8888 能正常访问。
例2、spring boot 开发中的数据库连接信息配置(这里使用com.alibaba 的 druid), 在application.properties 添加以下记录:
druid.url=jdbc:mysql://192.168.0.20:3306/test
druid.driver-class=com.mysql.jdbc.Driver
druid.username=root
druid.password=123456
druid.initial-size=1
druid.min-idle=1
druid.max-active=20
druid.test-on-borrow=true
以上两个例子,说明了如需修改starter模块中的默认配置,只需要在在application.properties 添加需要修改的配置即可。
附: application.properties 全部配置项,点击查看Spring Boot 所有配置说明
二、自定义属性配置
在application.properties中除了可以修改默认配置,我们还可以在这配置自定义的属性,并在实体bean中加载出来。
1、在application.properties中添加自定义属性配置
com.sam.name=sam
com.sam.age=11
com.sam.desc=magical sam
2、编写Bean类,加载属性
Sam类需要添加@Component注解,让spring在启动的时候扫描到该类,并添加到spring容器中。
第一种:使用spring支持的@Value()加载
package com.sam.demo.conf;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author sam
* @since 2017/7/15
*/
@Component
public class Sam {
//获取application.properties的属性
@Value("${com.sam.name}")
private String name;
@Value("${com.sam.age}")
private int age;
@Value("${com.sam.desc}")
private String desc;
//getter & setter
}
第二种:使用@ConfigurationProperties(prefix="") 设置前缀,属性上不需要添加注解。
package com.sam.demo.conf;
import org.springframework.stereotype.Component;
/**
* @author sam
* @since 2017/7/15
*/
@Component
@ConfigurationProperties(prefix = "com.sam")
public class Sam {
private String name;
private int age;
private String desc;
//getter & setter
}
3、在controller中注入并使用Sam这个Bean。
package com.sam.demo.controller;
import com.sam.demo.conf.Sam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author sam
* @since 2017/7/14
*/
@RestController
public class IndexController {
@Autowired
private Sam sam;
@RequestMapping("/index")
public String index() {
System.out.println(sam.getName() + " " + sam.getAge() + " " + sam.getDesc());
return "index";
}
}
浏览器访问:http://localhost:8080/index ,控制台正常打印出sam的内容。
三、application.properties 属性配置详解
1、参数引用与random随机数方法的使用
在application.properties内可以直接通过${}引用其他属性的值,如下:
com.sam.name=sam
com.sam.age=11
com.sam.desc=${name} is ${age} years old.
在application.properties中如果需要获取随机数,可以通过${random},如下:
#获取随机字符串
com.sam.randomValue=${random.value}
#获取随机字符串:${random.value}
#获取随机int:${random.int}
#获取10以内的随机数:${random.int(10)}
#获取10-20的随机数:${random.int[10,20]}
#获取随机long:${random.long}
#获取随机uuid:${random.uuid}
2、多环境配置
实际开发中可能会有不同的环境,有开发环境、测试环境、生成环境。对于每个环境相关配置都可能有所不同,如:数据库信息、端口配置、本地路径配置等。
如果每次切换不同环境都需要修改application.properties,那么操作是十分繁琐的。在spring boot中提供了多环境配置,使得我们切换环境变得简便。
在application.properties同目录下新建一下三个文件:
application-dev.properties //开发环境的配置文件
application-test.properties //测试环境的配置文件
application-prod.properties //生产环境的配置文件
上面三个文件分别对应了 开发、测试、生产 的配置内容,接下来就是应该怎么选择性引用这些配置了。
在application.properties添加:
spring.profiles.active=dev
#引用测试的配置文件
#spring.profiles.active=test
#引用生产的配置文件
#spring.profiles.active=prod
添加spring.profiles.active=dev后启动应用,会发现引用了dev的这份配置信息。
可以看出上面三个配置文件符合 application-{profile}.properties 格式,而在application.properties添加的 spring.profiles.active=dev 中的dev正是上面配置文件中的 profile。根据具体环境进行切换即刻。
用命令运行jar包启动应用的时候,可以指定相应的配置.
java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
附:配置方式和优先级
这些方式优先级如下:
a. 命令行参数
b. 来自java:comp/env的JNDI属性
c. Java系统属性(System.getProperties())
d. 操作系统环境变量
e. RandomValuePropertySource配置的random.*属性值
f. jar外部的application-{profile}.properties或application.yml(带spring.profile)配置文件
g. jar内部的application-{profile}.properties或application.yml(带spring.profile)配置文件
h. jar外部的application.properties或application.yml(不带spring.profile)配置文件
i. jar内部的application.properties或application.yml(不带spring.profile)配置文件
j. @Configuration注解类上的@PropertySource
k. 通过SpringApplication.setDefaultProperties指定的默认属性
注:命令行参数这种jar包指定参数启动应用的方式,可能是不安全的,我们可以设置禁止这种方式启动应用,如下:
springApplication.setAddCommandLineProperties(false);
package com.sam.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
// SpringApplication.run(DemoApplication.class, args);
SpringApplication springApplication = new SpringApplication(DemoApplication.class);
//禁止命令行设置参数
springApplication.setAddCommandLineProperties(false);
springApplication.run(args);
}
}
补充:
在spring boot 中配置除了支持 application.properties,还支持application.yml的配置方式,如下:
新建application.yml代替application.properties
server:
port: 9999
com:
sam:
name: sam
age: 11
desc: magical sam
注意:port: 9999 中间是有空格的,yml语法请参考:yml配置文件用法
版权声明:本文为博主原创文章,转载请注明出处。
Spring Boot 属性配置&自定义属性配置的更多相关文章
- Spring Boot 系列(三)属性配置&自定义属性配置
在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配 ...
- 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】
原本这个章节是要介绍<log4j多环境不同日志级别的控制的>但是没有这篇文章做基础的话,学习起来还是有点难度的,所以我们先一起了解下spring boot属性文件之多环境配置,当然文章中也 ...
- Spring Boot 属性配置和使用
Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...
- Spring Boot 属性配置和使用(转)
Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...
- Spring Boot实践——用外部配置填充Bean属性的几种方法
引用:https://blog.csdn.net/qq_17586821/article/details/79802320 spring boot允许我们把配置信息外部化.由此,我们就可以在不同的环境 ...
- Spring boot 官网学习笔记 - Spring Boot 属性配置和使用(转)-application.properties
Spring Boot uses a very particular PropertySource order that is designed to allow sensible overridin ...
- Spring Boot之实现自动配置
GITHUB地址:https://github.com/zhangboqing/springboot-learning 一.Spring Boot自动配置原理 自动配置功能是由@SpringBootA ...
- 学记:为spring boot写一个自动配置
spring boot遵循"约定优于配置"的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来.spring boot的神奇 ...
- 【spring boot】spring boot中使用定时任务配置
spring boot中使用定时任务配置 =============================================================================== ...
随机推荐
- Git学习笔记五--分支管理
为什么要引入分支? 分支在实际中有什么用呢?假设你准备开发一个新功能,但是需要两周才能完成,第一周你写了50%的代码,如果立刻提交,由于代码还没写完,不完整的代码库会导致别人不能干活了.如果等代码全部 ...
- Java运行时异常和非运行时异常
1.Java异常机制 Java把异常当做对象来处理,并定义一个基类java.lang.Throwable作为所有异常的超类.Java中的异常分为两大类:错误Error和异常Exception,Java ...
- jquery 获取css position的值
jquery 获取css position的值 CreateTime--2018年5月28日14:03:12 Author:Marydon 1.情景展示 <div id="aa&q ...
- 〖Linux〗proc的一些文件读取操作
/* * ===================================================================================== * * Filen ...
- [Done]ibatis/mybatis: java.lang.NoSuchMethodException
异常描述: Caused by: org.apache.ibatis.reflection.ReflectionException: Error instantiating interface cn. ...
- MYSQL 什么时候用单列索引?什么使用用联合索引?(收集)
我一个表 students 表,有3个字段 ,id,name,age 我要查询 通过 name 和age,在这两个字段 是创建 联合索引?还是分别在name和age上创建 单列索引呢? 多个字段查询什 ...
- mysql数据结构优化,范式和反范式
1.范式,正常的建表,反范式,为了提高效率,适当的已空间换时间 2.垂直拆分,就是把经常用的.或者text大存储的字段单独拉出来存表 3.水平拆分,解决数据量大的问题,进行取莫的方式将数据放到相同的n ...
- appium 1.6.3 + ios 10.2 + xcode 8.2.1 真机运行safari
启动appium 命令: appium --address "127.0.0.1" --session-override --pre-launch --debug-log-spac ...
- unity, asset operations
//----create asset //ref: http://wiki.unity3d.com/index.php?title=CreateScriptableObjectAsset CmyScr ...
- 大数据量下MySQL插入方法的性能比较
不管是日常业务数据处理中,还是数据库的导入导出,都可能遇到需要处理大量数据的插入.插入的方式和数据库引擎都会对插入速度造成影响,这篇文章旨在从理论和实践上对各种方法进行分析和比较,方便以后应用中插入方 ...