springboot学习(三) springboot文件配置
1、简介
springboot没有了原来自己整合Spring应用时繁多的XML配置内容,替代它的是在pom.xml中引入模块化的Starter POMs,其中各个模块都有自己的默认配置,所以如果不是特殊应用场景,就只需要在application.properties中完成一些属性配置就能开启各模块的应用。
2、自定义属性和加载
我们使用的时候在application.properties中定义一些自己使用的属性:
com.cnblogs.name=origalom
com.cnblogs.title=学习Spring Boot
然后在代码中利用@Value("${属性名}")注解来获取对应的配置的值:
@Component
public class Properties { @Value("${com.cnblogs.name}")
private String name; @Value("${com.cnblogs.title}")
private String title; // ...... }
最后,编写测试代码进行测试,查看是否可以正常获取到配置中的数据:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class TestProperties { @Autowired
private Properties properties; @Test
public void test() {
System.out.println(properties.getName());
System.out.println(properties.getTitle());
}
}
3、参数间的引用
在配置文件中参数之间是可以直接引用的,方法为${属性名}:
com.cnblogs.name=origalom
com.cnblogs.title=学习Spring Boot
com.cnblogs.desc=${com.cnblogs.name.name}正在${com.cnblogs.title}
4、使用随机数
有时,我们需要的参数不是一个固定的数,而是随机的一个数。这个时候我们就可以通过${random}来产生随机数:
# 随机字符串
com.cnblogs.value=${random.value}
# 随机整数
com.cnblogs.int=${random.int}
# 随机long
com.cnblogs.long=${random.long}
# 100以内的随机整数
com.cnblogs.int1=${random.int(100)}
# 100-200之间的随机整数
com.cnblogs.int2=${random.int[100,200]}
# 随机uuid
com.cnblogs.uuid=${random.uuid}
5、通过命令行设置属性值
默认情况下,SpringApplication将任何可选的命令行参数(以'--'开头,比如,--server.port=9000)转化为property,并将其添加到Spring Environment中。如上所述,命令行属性总是优先于其他属性源。
如果不想使用命令行的形式,可以通过设置取消这个功能:
SpringApplication application = new SpringApplication(Application.class);
// 取消通过命令行设置属性值
application.setAddCommandLineProperties(false);
application.run(Application.class, args);
6、多环境配置
一般情况下,应用程序会被安装到不同的环境中,比如开发,测试,生产等,而在这些环境中数据库,服务器端口号等配置可能不同,所以很容易发生错误。
在spring boot中,多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识:
- application-dev.properties: 开发环境
- application-test.properties: 测试环境
- application-prod.properties: 生产环境
然后,可以在application.properties中设置spring.profiles.active来确定使用哪个环境,其值对应{profile}值。
springboot学习(三) springboot文件配置的更多相关文章
- SpringBoot学习(三)-->Spring的Java配置方式之读取外部的资源配置文件并配置数据库连接池
三.读取外部的资源配置文件并配置数据库连接池 1.读取外部的资源配置文件 通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值,具体用法: @Configuration ...
- SpringBoot学习<二>——SpringBoot的默认配置文件application和多环境配置
一.SpringBoot的默认文件appliction 上一篇文章已经说明,springboot启动会内嵌tomcat,端口也是默认的8080,如果我们想要改变端口如果做呢? 在springboot项 ...
- SpringBoot学习(三):日志
1.日志框架 小张:开发一个大型系统: 1.System.out.println(""):将关键数据打印在控制台:去掉?写在一个文件? 2.框架来记录系统的一些运行时信息: ...
- Springboot学习:SpringMVC自动配置
Spring MVC auto-configuration Spring Boot 自动配置好了SpringMVC 以下是SpringBoot对SpringMVC的默认配置:==(WebMvcAuto ...
- springboot之assembly的文件配置
一.在使用springboot框架的时候,存在一个问题.就是我们配置yaml文件,需要单独提出来做参数修改.当然这个是可以通过spring.profiles.active的方式来配置dev,prod等 ...
- SpringBoot学习(七)-->SpringBoot在web开发中的配置
SpringBoot在web开发中的配置 Web开发的自动配置类:在Maven Dependencies-->spring-boot-1.5.2.RELEASE.jar-->org.spr ...
- SpringBoot学习(六)-->SpringBoot的自动配置的原理
Spring Boot的自动配置的原理 Spring Boot在进行SpringApplication对象实例化时会加载META-INF/spring.factories文件,将该配置文件中的配置载入 ...
- 尚硅谷springboot学习9-配置文件值注入
首先让我想到的是spring的依赖注入,这里我们可以将yaml或者properties配置文件中的值注入到java bean中 配置文件 person: lastName: hello age: 18 ...
- SpringBoot入门-多环境文件配置(二)
pom.xml <name>springboot-application</name> <description>A project for Spring Boot ...
- springboot学习三:整合jsp
在pom.xml加入jstl <!--springboot tomcat jsp 支持开启--> <dependency> <groupId>org.apache. ...
随机推荐
- Hadoop-eclipse插件配置
1.准备jar包与安装eclipse. 2.将jar包拷贝到eclipse/plugin.
- C++ 对象的内存布局【转】
单一继承: 虚函数表在最前面的位置. 成员变量根据其继承和声明顺序依次放在后面. 在单一的继承中,被overwrite的虚函数在虚函数表中得到了更新. 多重继承 每个父类都有自己的虚表. 子类的成员函 ...
- 【kmp算法】hdu4763 Theme Section
kmp中next数组的含义是:next[i]表示对于s[0]~s[i-1]这个前缀而言,最大相等的前后缀的长度是多少.规定next[0]=-1. 迭代for(int i=next[i];i!=-1;i ...
- 【数位dp】hdu2089 不要62
http://www.cnblogs.com/xiaohongmao/p/3473599.html #include<cstdio> using namespace std; int n, ...
- 【离散化】【扫描线】CH Round #59 - OrzCC杯NOIP模拟赛day1 队爷的新书
//上图绿色扫描线右侧少画了一条扫描线. 很多区间把数轴分成了很多段,看哪个点的(区间覆盖数*该点权值)最大. 显然在某个区间的右端点的答案是最优的. 排序后 用扫描线从左到右扫描,维护每个点的覆盖数 ...
- CodeForces - 44A Indian Summer
Indian summer is such a beautiful time of the year! A girl named Alyona is walking in the forest and ...
- iOS开发-设置在使用NavigateController时View的顶部位置
最近我在开发中遇到了一个问题,在使用NavigationController时内部的ViewController的View总是与屏幕顶部对齐,而我们有时候不需要这种效果: 在开发过程中,我们可能会 ...
- iOS开发——使用Autolayout生成动态高度的TableViewCell单元格
步骤一.TableViewCell中使用Autolayout 要点:Cell的高度必须在Constraints中指明,但不能定死,需要让内部由内容决定高度的View决定动态高度. 如UILabel设置 ...
- 移除tabBar 重影
-(void)viewWillLayoutSubviews{ [super viewWillLayoutSubviews]; for (UIView *child in self.tabBar.sub ...
- gitk图形界面中文乱码情况
当打开gitk图形界面时,文件中的中文部分乱码了,这大部分是因为编码格式的问题,为了跟上时代的脚步,本人建议都是用utf-8编码. 为了方便,我将全局配置为utf-8编码: git config -- ...