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 对静态资源访问提供了很好的支持, ...
随机推荐
- <逆向学习第二天>如何手动脱UPX、Aspack壳
UPS.AsPack压缩壳介绍: UPX .AsPack是一款先进的可执行程序文件压缩器.压缩过的可执行文件体积缩小50%-70% ,这样减少了磁盘占用空间.网络上传下载的时间和其它分布以及存储费用. ...
- oracle优化-leading提示和ordered提示以及materialize提示
以下内容适用于oracle 10.2.0.5及其以上版本 一个查询很慢,原始SQL如下: select a.* from (select ssi.ID, 'small_station_info' TB ...
- Maven里面多环境下的属性过滤(配置)
情景:通常一个项目都为分为开发环境(dev)和测试环境(test)还有正式环境(prod),如果每次一打包都要手动地去更改配置文件,例如数据库连接配置.将会很容易出差错. 解决方案:maven pro ...
- 虚拟内存设置(解决linux内存不够情况)
一. 虚拟内存介绍 背景介绍 Memory指机器物理内存,读写速度低于CPU一个量级,但是高于磁盘不止一个量级.所以,程序和数据如果在内存的话,会有非常快的读写速度.但是,内存的造价是要高于 ...
- Vue报错 [Vue warn]: Cannot find element
在前端开发全面进入前端的时代 作为一个合格的前端开发工作者 框架是不可或缺的Vue React Anguar 作为前端小白,追随大佬的脚步来到来到博客园,更新现在正在学习的Vue 注 : 相信学习Vu ...
- Asp.NET Core 在IIS部署 An assembly specified in the application dependencies manifest was not found
今天在发布应用的时候,出来了一个报错:An assembly specified in the application dependencies manifest was not found 情况如下 ...
- PHP令人困惑的strtotime
经常会有人被strtotime结合-1 month, +1 month, next month的时候搞得很困惑, 然后就会觉得这个函数有点不那么靠谱, 动不动就出问题. 用的时候就会很慌… 比如:今天 ...
- 004---Linux系统设置
Linux版本相关命令 查看系统版本:cat /etc/redhat-release 查看系统内核版本以及位数:uname -r [root@hostname1 ~]# cat /etc/redhat ...
- idea添加源代码目录,编译代码出现时钟样式
项目结构需要有一个target目录,需要一个src目录,
- springmvc 配置多视图(jsp,freemarker,HTML等)
SpringMVC 的 Controller 可以返回各种各样的视图.比如 JSP, JSON, Velocity, FreeMarker, XML, PDF, Excel, 还有Html字符流 等等 ...