配置文件:

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种方式的更多相关文章

  1. SpringBoot配置定时任务的两种方式

    一.导入相关的jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  2. SpringBoot配置Bean的两种方式--注解以及配置文件

    一.注解方式 编写实体类: package com.example.bean; import org.springframework.boot.context.properties.Configura ...

  3. springBoot配置文件属性注入

    以一个微信公众号开发为例,注入微信appid 1.在application.properites文件中定义属性 #===================微信相关=============#公众号wxp ...

  4. SpringBoot整合Servlet的两种方式

    SpringBoot整合Servlet有两种方式: 1.通过注解扫描完成Servlet组件的注册: 2.通过方法完成Servlet组件的注册: 现在简单记录一下两种方式的实现 1.通过注解扫描完成Se ...

  5. SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)

    前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...

  6. 【SpringBoot】03.SpringBoot整合Servlet的两种方式

    SpringBoot整合Servlet的两种方式: 1. 通过注解扫描完成Servlet组件注册 新建Servlet类继承HttpServlet 重写超类doGet方法 在该类使用注解@WebServ ...

  7. 【转】Apache 配置虚拟主机三种方式

    Apache 配置虚拟主机三种方式  原文博客http://www.cnblogs.com/hi-bazinga/archive/2012/04/23/2466605.html 一.基于IP 1. 假 ...

  8. spring配置属性的两种方式

    spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ...

  9. web.config文件中配置数据库连接的两种方式

    web.config文件中配置数据库连接的两种方式 标签: 数据库webconfig 2015-04-28 18:18 31590人阅读 评论(1)收藏举报    分类: 数据库(74)  在网站开发 ...

  10. 【转】python之配置日志的几种方式

    [转]python之配置日志的几种方式 作为开发者,我们可以通过以下3种方式来配置logging: 1)使用Python代码显式的创建loggers, handlers和formatters并分别调用 ...

随机推荐

  1. 代码重复率检查工具jsinspect

    检查重复代码,去掉冗余代码. 安装: npm install -g jsinspect 用法:jsinspect [options] <paths ...> 检测复制粘贴和结构类似的Jav ...

  2. Win10系统XWware虚拟机安装Linux系统(Ubuntu)最新版教程

    XWware虚拟机安装Linux系统(Ubuntu)教程 一.下载并安装VMware虚拟机 借助VMware Workstation Pro, 我们可以在同一台Windows或Linux PC上同时运 ...

  3. MVC数据的注册及验证简单总结

    一.注解 注解是一种通用机制,可以用来向框架注入元数据,同时,框架不只驱动元数据的验证,还可以在生成显示和编辑模型的HTML标记时使用元数据. 二.验证注册的使用 1.Require:属性为Null或 ...

  4. Siki_Unity_2-1_API常用方法和类详细讲解(上)

    Unity 2-1 API常用方法和类详细讲解(上) 任务1&2:课程前言.学习方法 && 开发环境.查API文档 API: Application Programming I ...

  5. linux系统简单命令

    # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # hostn ...

  6. hexo部署失败如何解决

  7. spark写入ES(动态模板)

    使用es-hadoop插件,主要使用elasticsearch-spark-20_2.11-6.2.x.jar 官网:https://www.elastic.co/guide/en/elasticse ...

  8. spark-shell解析

    spark-shell 作用: 调用spark-submit脚本,如下参数 --classorg.apache.spark.repl.Main --name "Spark shell&quo ...

  9. LeetCode 104——二叉树中的最大深度

    1. 题目 2. 解答 如果根节点为空,直接返回 0.如果根节点非空,递归得到其左右子树的深度,树的深度就为左右子树深度的最大值加 1. /** * Definition for a binary t ...

  10. Cassandra 类型转换限制

    原文地址:http://stackoverflow.com/questions/31880381/cassandra-alter-column-type-which-types-are-compati ...