SpringBoot-配置文件属性注入-3种方式
配置文件:
datasource.username = admin
datasource.url = /hello/world
方式一: @Value
前提:
<!-- JavaBean处理工具包 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
使用:
@Component
@Data
public class PropertyBean {
@Value("${datasource.url}")
private String url;
@Value("${datasource.username}")
private String userName;
}
方式二:
前提:
<!-- 支持 @ConfigurationProperties 注解 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring-boot.version}</version>
</dependency>
使用: @ConfigurationProperties
@Component
@Configuration
@EnableAutoConfiguration
public class PropertyBeanUtil { @Bean
@ConfigurationProperties(prefix = "datasource")
public PropertyBean propertyBean() {
return new PropertyBean();
} }
方式三:获得Environment 的对象
@SpringBootApplication
public class SpringBootDemo3Application { public static void main(String[] args) {
final ApplicationContext ctx = SpringApplication.run(SpringBootDemo3Application.class, args);
Environment environment = ctx.getEnvironment();
System.out.println(environment.getProperty("datasource.username"));
}
}
扩展:
使用@PropertySource注解加载自定义的配置文件,但该注解无法加载yml配置文件。然后可以使用@Value注解获得文件中的参数值
/**
* 加载properties配置文件,在方法中可以获取
* abc.properties文件不存在,验证ignoreResourceNotFound属性
* 加上encoding = "utf-8"属性防止中文乱码,不能为大写的"UTF-8"
*/
@Configuration
@PropertySource(value = {"classpath:/config/propConfigs.properties","classpath:/config/db.properties"},
ignoreResourceNotFound = true,encoding = "utf-8")
public class PropConfig { // PropertySourcesPlaceholderConfigurer这个bean,
// 这个bean主要用于解决@value中使用的${…}占位符。
// 假如你不使用${…}占位符的话,可以不使用这个bean。
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
SpringBoot-配置文件属性注入-3种方式的更多相关文章
- SpringBoot配置定时任务的两种方式
一.导入相关的jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- SpringBoot配置Bean的两种方式--注解以及配置文件
一.注解方式 编写实体类: package com.example.bean; import org.springframework.boot.context.properties.Configura ...
- springBoot配置文件属性注入
以一个微信公众号开发为例,注入微信appid 1.在application.properites文件中定义属性 #===================微信相关=============#公众号wxp ...
- SpringBoot整合Servlet的两种方式
SpringBoot整合Servlet有两种方式: 1.通过注解扫描完成Servlet组件的注册: 2.通过方法完成Servlet组件的注册: 现在简单记录一下两种方式的实现 1.通过注解扫描完成Se ...
- SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)
前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...
- 【SpringBoot】03.SpringBoot整合Servlet的两种方式
SpringBoot整合Servlet的两种方式: 1. 通过注解扫描完成Servlet组件注册 新建Servlet类继承HttpServlet 重写超类doGet方法 在该类使用注解@WebServ ...
- 【转】Apache 配置虚拟主机三种方式
Apache 配置虚拟主机三种方式 原文博客http://www.cnblogs.com/hi-bazinga/archive/2012/04/23/2466605.html 一.基于IP 1. 假 ...
- spring配置属性的两种方式
spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ...
- web.config文件中配置数据库连接的两种方式
web.config文件中配置数据库连接的两种方式 标签: 数据库webconfig 2015-04-28 18:18 31590人阅读 评论(1)收藏举报 分类: 数据库(74) 在网站开发 ...
- 【转】python之配置日志的几种方式
[转]python之配置日志的几种方式 作为开发者,我们可以通过以下3种方式来配置logging: 1)使用Python代码显式的创建loggers, handlers和formatters并分别调用 ...
随机推荐
- 【廖雪峰老师python教程】——进程与线程
多进程 操作系统轮流让各个任务交替执行,任务1执行0.01秒,切换到任务2,任务2执行0.01秒,再切换到任务3,执行0.01秒……这样反复执行下去.表面上看,每个任务都是交替执行的,但是,由于CPU ...
- 「日常温习」Hungary算法解决二分图相关问题
前言 二分图的重点在于建模.以下的题目大家可以清晰的看出来这一点.代码相似度很高,但是思路基本上是各不相同. 题目 HDU 1179 Ollivanders: Makers of Fine Wands ...
- XSS--PHPwind5.3复现
xss再分析 短payload <svg/onload=alert(1)> <body/onfocus=alert``> <body/onfocus=confirm``& ...
- 即刻开始使用Kotlin开发Android的12个原因(KAD 30)
作者:Antonio Leiva 时间:Jul, 11, 2017 原文链接:https://antonioleiva.com/reasons-kotlin-android/ 这组文章已到最后了,它们 ...
- Python 多线程、进程、协程上手体验
浅谈 Python 多线程.进程.协程上手体验 前言:浅谈 Python 很多人都认为 Python 的多线程是垃圾(GIL 说这锅甩不掉啊~):本章节主要给你体验下 Python 的两个库 Thre ...
- Linux命令应用大词典-第37章 Linux系统故障排错
37.1 mkbootdisk:创建用于运行系统的独立启动软盘 37.2 chroot:切换根目录环境 37.3 badblocks:搜索设备的坏块 37.4 mkinitrd:创建要载入ramdis ...
- TPO-12 C1 Revise a Hemingway paper
TPO-12 C1 Revise a Hemingway paper 第 1 段 1.Listen to a conversation between a student and a professo ...
- 第4章 TCP/IP通信案例:访问Internet上的Web服务器
第4章 TCP/IP通信案例:访问Internet上的Web服务器 4.2 部署代理服务器 书中为了演示访问Internet上的Web服务器的全过程,使用了squid代理服务器程序模拟了一个代理服务器 ...
- 技本功丨知否知否,Redux源码竟如此意味深长(下集)
上集回顾 Redux是如何使用的?首先再来回顾一下这个使用demo(谁让这段代码完整地展示了redux的使用) 如果有小伙伴对这段代码不是很理解的话,建议先去学习Redux的使用再来看这篇源码,这样更 ...
- LeetCode 141——环形链表
1. 题目 2. 解答 2.1 方法 1 定义快慢两个指针,慢指针每次前进一步,快指针每次前进两步,若链表有环,则快慢指针一定会相遇. /** * Definition for singly-link ...