springboot 学习笔记(二)--- properties 配置
springboot可以提供了多种方式配置properties。
一、Java System.setProperty(k, v)
System.setProperty("myname", "Java_System_name");
二、在classpath目录下创建配置文件 application.properties
文件内容格式是 KV格式
myname=classpath_name
三、支持嵌套注解
application.properties
db=db
jdbc.username=root
jdbc.password=root
注解主类
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties
public class MysqlConfig { private String db; private Jdbc jdbc; public String getDb() {
return db;
} public void setDb(String db) {
this.db = db;
} public Jdbc getJdbc() {
return jdbc;
} public void setJdbc(Jdbc jdbc) {
this.jdbc = jdbc;
} }
附类
public class Jdbc {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
springboot 会自动解析jdbc开头的属性,和注解类jdbc映射
四、创建yml文件配置
首先, pom需要依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
my:
server:
- hehe
- haha
配置类注解 : 使用
@ConfigurationProperties prefix : 获取yml文件的my配置项
@Component("serverConfig")
@ConfigurationProperties(prefix = "my")
public class ServerConfig {
private List<String> server = new ArrayList<String>();
public List<String> getServer() {
return server;
}
public void setServer(List<String> server) {
this.server = server;
}
}
测试 :
@RestController : spring路由请求,直接将结果返回给请求者
@EnableAutoConfiguration : springboot启动入口
@ComponentScan : 扫描注解
@RestController
@EnableAutoConfiguration
@ComponentScan
public class Application { @Autowired
private ServerConfig serverConfig; @RequestMapping("/")
public String index() { getServerConfig(); return "hello, spring boot" ;
} public void getServerConfig() {
Gson gson = new Gson(); System.out.println(gson.toJson(serverConfig.getServer()));
} public static void main(String[] args) { SpringApplication.run(Application.class, args); }
}
结果 :
["hehe","haha"]
参考文献
springboot 学习笔记(二)--- properties 配置的更多相关文章
- SpringBoot学习笔记<二>注解
此篇为项目作结之笔记,关于注解. 项目启动入口@SpringBootApplication[必选] @ServletComponentScan[可选] 注解后: Servlet.Filter.Lis ...
- Nginx学习笔记二基本配置
1.Nginx的配置文件默认在Nginx程序安装目录的conf二级目录下,主配置文件为nginx.conf.假设您的Nginx安装 在/usr/local/webserver/nginx/目录下,那么 ...
- SpringBoot学习笔记二之Spring整合Mybatis
原文链接: https://www.toutiao.com/i6803235766274097678/ 在learn-admin-component子工程中加入搭建环境所需要的具体依赖(因为比较长配置 ...
- 《深入理解Spring Cloud与微服务构建》学习笔记(二十)~配置中心Spring Cloud Config
本例重新创建项目,构建一个空的mavan工程. 一.Config Server 从本地读取配置文件 新建一个moudle config_server ,pom添加依赖 <dependency ...
- php-resque学习笔记二(配置)
1:前提 系统:CentOS PHP版本:PHP7 安装目录:/usr/local/php php-resque 安装目录:/home/software/php-resque ...
- PyCharm学习笔记(二) 调试配置
选择PyCharm编译器 注意工程默认使用的解释器可能是Pycharm自带的,而不是单独安装的.
- Springboot学习笔记(六)-配置化注入
前言 前面写过一个Springboot学习笔记(一)-线程池的简化及使用,发现有个缺陷,打个比方,我这个线程池写在一个公用服务中,各项参数都定死了,现在有两个服务要调用它,一个服务的线程数通常很多,而 ...
- SpringBoot学习笔记(1):配置Mybatis
SpringBoot学习笔记(1):配置Mybatis 反思:如果自己写的笔记自己都看不懂,那就不要拿出来丢人现眼! IDEA插件 Free MyBatis Plugin插件可以让我们的MyBatis ...
- springboot 学习笔记(二)
springboot 学习笔记(二) 快速创建一个springboot工程,并引入所需要的依赖 1.利用Spring initializr 来创建一个springboot项目,登陆http://sta ...
- SpringBoot学习笔记(3):静态资源处理
SpringBoot学习笔记(3):静态资源处理 在web开发中,静态资源的访问是必不可少的,如:Html.图片.js.css 等资源的访问. Spring Boot 对静态资源访问提供了很好的支持, ...
随机推荐
- 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--D-psd面试
链接:https://www.nowcoder.com/acm/contest/90/D 来源:牛客网 1.题目描述 掌握未来命运的女神 psd 师兄在拿了朝田诗乃的 buff 后决定去实习. 埃森哲 ...
- ABAP术语-Method
Method 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/05/1091077.html Component of classes in ...
- 【shell脚本学习-3】
part-1 #!/bin/bash:<<FTP#test [ 1 -eq 2] #条件测试x="abc" #不允许有空格y="abc" [ &qu ...
- typecho博客组插件:openSug.js百度搜索框下拉提示免费代码
Typecho候选搜索增强插件:安装openSug插件即可获得带有“搜索框提示”功能的搜索框,让Typecho搜索更便捷! 支持百度.谷歌.雅虎.Yandex.360好搜.UC神马.酷狗.优酷.淘 ...
- STM32(4)——系统时钟和SysTick
1.STM32的时钟系统 在STM32中,一共有5个时钟源,分别是HSI.HSE.LSI.LSE.PLL HSI是高速内部时钟,RC振荡器,频率为8MHz: HSE是高速外部时钟,可接石英/陶瓷谐振器 ...
- DedeCMS V5.7sp2最新版本parse_str函数SQL注入漏洞
织梦dedecms,在整个互联网中许多企业网站,个人网站,优化网站都在使用dede作为整个网站的开发架构,dedecms采用php+mysql数据库的架构来承载整个网站的运行与用户的访问,首页以及栏目 ...
- 为什么我要放弃javaScript数据结构与算法(第三章)—— 栈
有两种结构类似于数组,但在添加和删除元素时更加可控,它们就是栈和队列. 第三章 栈 栈数据结构 栈是一种遵循后进先出(LIFO)原则的有序集合.新添加的或待删除的元素都保存在栈的同一端,称为栈顶,另一 ...
- win10 无法修改默认程序 默认打开方式的解决方法
此时是2018年11月24日 win10 pro 64位 版本是1803 具体版本号是17134 情景: 我的状况是.json文件的默认打开方式被新安装的应用霸占了,然后无论是通过“右键-属性-更改 ...
- 关于springboot 连接mysql 数据库报错问题
springboot连接MySQL运行报错: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more ...
- Python的logging模块、os模块、commands模块与sys模块
一.logging模块 import logging logging.debug('This is debug message') logging.info('This is info message ...