Spring Boot . 4 -- 定制 Spring Boot 配置 【2】
除了第一篇中使用 覆写的方式进行 自动配置的更改外,还可以通过 Spring Boot 中提供的 application.properties 文件改变应用的运行时配置。这种配置的方式粒度是非常精细的。
application.properties
在 Spring Boot 应用中,application.properties 文件在 resource/ 目录下,初始状态下,application.properties 是空白的。通过这个文件可以比较细粒度的控制Spring Boot 中的一些配置,比如 数据库链接、特定参数配置等。默认情况下,应用启动后监听的端口为 8080,如果想要将坚挺的端口设置为7000,那么只要在application.properties 文件中加上 server.port=7000,然后启动应用,在浏览器中 使用 localhost:7000 就可以访问页面了。
application.properties 文件有一些默认的可配置参数,比如spring.datasource.* 可以使用这些配置,对应用中使用的DataSource进行配置。这些已有的配置可以通过文档查到。使用自主的配置与使用原有配置,方法基本是一致的。举个例子,如果想在一个类中使用application.properties 中配置的数据。需要使用注解 @ConfigurationProperties(prefix="test")
@Controller
@RequestMapping("/")
@ConfigurationProperties(prefix = "test")
public class ReadingListController { private String property; System.out.println("value" + property); public void setProperty(String property) {
this.property=property;
}
}
在 application.properties 中只要简单指定 属性的值:
test.property=just a test
当然,如果想要在代码中使用 application.properties 中的参数初始化一个类对象,在其他的地方直接以实例的方式使用也是非常好的。使用方式与上面基本是一致的。首先在application.properties 文件中定义需要的数据:
dealer.item=ball
dealer.fashion=true
dealer.money=100
dealer.home=shop
定义一个类Dealer,使用当前定义的配置 :
@ConfigurationProperties(prefix = "dealer")
@Configuration
public class Dealer { private String item;
private boolean fashion;
private int money;
private String home; public String getItem() {
return item;
} public void setItem(String item) {
this.item = item;
} public boolean isFashion() {
return fashion;
} public void setFashion(boolean fashion) {
this.fashion = fashion;
} public int getMoney() {
return money;
} public void setMoney(int money) {
this.money = money;
} public String getHome() {
return home;
} public void setHome(String home) {
this.home = home;
}
}
Dealer 在Controller 中进行使用:
@Controller
public class HelloController { @Autowired
private ReaderRepository readerRepository; private Dealer dealer; @Autowired
public HelloController(Dealer dealer) {
this.dealer = dealer;
} @RequestMapping(value = "/prop")
public String testProp()
{
System.out.println("HERE " + dealer.getHome());
return "index";
}
}
使用的套路基本上就是这样的。
Spring Boot . 4 -- 定制 Spring Boot 配置 【2】的更多相关文章
- Spring Boot . 4 -- 定制 Spring Boot 配置
覆写 Auto-Configuration 的类 利用外部属性进行动态配置 [本文] 定制 Error 页面 [第二篇] Spring Boot的自动配置可以节省很多无趣的配置工作,但是并不是所有的自 ...
- Spring Boot实践——基础和常用配置
借鉴:https://blog.csdn.net/j903829182/article/details/74906948 一.Spring Boot 启动注解说明 @SpringBootApplica ...
- 自定义的Spring Boot starter如何设置自动配置注解
本文首发于个人网站: 在Spring Boot实战之定制自己的starter一文最后提到,触发Spring Boot的配置过程有两种方法: spring.factories:由Spring Boot触 ...
- Spring Boot源码探索——自动配置的内部实现
前面写了两篇文章 <Spring Boot自动配置的魔法是怎么实现的>和 <Spring Boot起步依赖:定制starter>,分别分析了Spring Boot的自动配置和起 ...
- Spring Boot使用Druid和监控配置
Spring Boot默认的数据源是:org.apache.tomcat.jdbc.pool.DataSource 整体步骤: (1) -- Druid简单介绍,具体看官网: (2) ...
- Spring Boot应用的后台运行配置
酱油一篇,整理一下关于Spring Boot后台运行的一些配置方式.在介绍后台运行配置之前,我们先回顾一下Spring Boot应用的几种运行方式: 运行Spring Boot的应用主类 使用Mave ...
- Spring boot 的 properties 属性值配置 application.properties 与 自定义properties
配置属性值application.properties 文件直接配置: com.ieen.super.name="MDD" 自定义properties文件配置:src/main/r ...
- Spring Boot 启动(二) 配置详解
Spring Boot 启动(二) 配置详解 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Boot 配置 ...
- Spring Boot 中使用 @Transactional 注解配置事务管理
事务管理是应用系统开发中必不可少的一部分.Spring 为事务管理提供了丰富的功能支持.Spring 事务管理分为编程式和声明式的两种方式.编程式事务指的是通过编码方式实现事务:声明式事务基于 AOP ...
随机推荐
- bzoj 4753 最佳团体 —— 01分数规划+树形背包
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4753 注意赋初值为 -inf: eps 设为 1e-3 会 WA ... 代码如下: #in ...
- bzoj1426 (洛谷P4550) 收集邮票——期望
题目:https://www.luogu.org/problemnew/show/P4550 推式子……:https://blog.csdn.net/pygbingshen/article/detai ...
- 【转】jenkins上配置robotframeworkride自动化脚本任务
jenkins上配置robotframeworkride自动化脚本任务 编写好的自动化脚本,集成在jenkins上进行自动运行于监控,这里采用分布式构建,在一台slave上进行任务构建与自动化脚本的运 ...
- robotframework收藏
收藏文章 http://www.360doc.com/userhome/38592059
- Windows JDK 1.8降级为1.7方法
转自:https://www.aliyun.com/jiaocheng/890626.html 正常情况下,JDK的升降级只需更改环境变量即可 可是到了1.8版本,Oracle使了一点小手段, 在系统 ...
- 520. Detect Capital(检测数组的大小写)
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...
- E20170518-hm
inherit inherit v. 继承; inheritance n. 继承; 遗传; 遗产; eigen [词典] 特征的,本征; single n. 单程票单人房间; [复数] (高尔夫球 ...
- POJ3682;King Arthur's Birthday Celebration(期望)
传送门 题意 进行翻硬币实验,若k次向上则结束,进行第n次实验需花费2*n-1的费用,询问期望结束次数及期望结束费用 分析 我们令f[i]为结束概率 \[f[i]=C_{i-1}^{k-1}*p^k* ...
- 《windows核心编程系列》五谈谈线程基础
线程基础 与前面介绍的进程一样,线程也有两部分组成.一个是线程内核对象.它是一个数据结构,操作系统用它来管理线程以及用它来存储线程的一些统计信息.另一个是线程栈,用于维护线程执行时所需的所有函数参数和 ...
- Python上下文管理器(Context managers)
上下文管理器(Context managers) 上下文管理器允许你在有需要的时候,精确地分配和释放资源. 使用上下文管理器最广泛的案例就是with语句了.想象下你有两个需要结对执行的相关操作,然后还 ...