转:https://blog.csdn.net/qq_27298687/article/details/79033102

SpringBoot获得application.properties中数据的几种方式

第一种方式

  1. @SpringBootApplication
  2. public class SpringBoot01Application {
  3. public static void main(String[] args) {
  4. ConfigurableApplicationContext  context=SpringApplication.run(SpringBoot01Application.class, args);
  5. <span style="color:#FF0000;">String str1=context.getEnvironment().getProperty("aaa");</span>
  6. System.out.println(str1);
  7. }
  8. }

第二种方式(自动装配到Bean中)

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.core.env.Environment;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. public class Student {
  7. @Autowired
  8. private Environment env;
  9. public void speak() {
  10. System.out.println("=========>" + env.getProperty("aaa"));
  11. }
  12. }

第三种方式(使用@value注解)

    1. package com.example.demo.entity;
    2. import org.springframework.beans.factory.annotation.Value;
    3. import org.springframework.context.annotation.PropertySource;
    4. import org.springframework.stereotype.Component;
    5. @Component
    6. @PropertySource("classpath:jdbc.properties")//如果是application.properties,就不用写@PropertySource("application.properties"),其他名字用些
    7. public class Jdbc {
    8. @Value("${jdbc.user}")
    9. private String user;
    10. @Value("${jdbc.password}")
    11. private String password;
    12. public void speack(){
    13. System.out.println("username:"+user+"------"+"password:"+password);
    14. }
    15. }

SpringBoot获得application.properties中数据的几种方式的更多相关文章

  1. springboot读取application.properties中自定义配置

    假设在application-xxx.properties中配置 user.name=yuhk 一.在Controller中读取 @Value("{$user.name}") pr ...

  2. eureka ... is an unknown property 在 application.properties 中

    问题如图 在application.properties中无法识别eureka   解决方式 (想了想这个好像是在某个依赖里面来的)发现 eureka 是在 某个依赖里面 所以添加了以下之后就解决了 ...

  3. SpringBoot在logback.xml中读取application.properties中配置的日志路径

    1.在springboot项目中使用logback记录日志,在logback.xml中配置日志存储位置时读取application.properties中配置的路径,在 logback.xml中配置引 ...

  4. SpringBoot配置文件 application.properties详解

    SpringBoot配置文件 application.properties详解   本文转载:https://www.cnblogs.com/louby/p/8565027.html 阅读过程中若发现 ...

  5. SpringBoot配置文件 application.properties,yaml配置

    SpringBoot配置文件 application.properties,yaml配置 1.Spring Boot 的配置文件 application.properties 1.1 位置问题 1.2 ...

  6. SpringBoot读取application.properties文件

    http://blog.csdn.net/cloume/article/details/52538626 Spring Boot中使用自定义的properties Spring Boot的applic ...

  7. 【spring boot logback】日志使用自定义的logback-spring.xml文件后,application.properties中关于日志的相关配置还会起作用么

    本篇 将针对[日志使用自定义的logback-spring.xml文件后,application.properties中关于日志的相关配置还会起作用么]这一个主题进行探索. 这个测试项目是根据[spr ...

  8. spring boot项目下application.properties中使用logging.path和logging.file时的细节

    logging.path仅仅用于指定日志输出的目录,且不能指定输出的文件名,且默认名为spring.log  若指定的是相对目录,则会生成在当前总项目的目录下 idea中新建sprnig boot项目 ...

  9. 使用 application.properties 中配置的属性,举例:@Value("${server.port}")

    使用 application.properties 中配置的属性:@Value 注解. @RestController public class HelloWorldController { @Val ...

随机推荐

  1. A1102 | 反转二叉树

    #include <stdio.h> #include <memory.h> #include <math.h> #include <string> # ...

  2. 重装了服务器,用的是centos/php微信小程序版,centos 命令大全

    centos 命令大全 1.关机 (系统的关机.重启以及登出 ) 的命令 shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdo ...

  3. nginx 访问控制之 document_uri

    这就用到了变量$document_uri,根据前面所学内容,该变量等价于$uri,其实也等价于location匹配. 示例1: if ($document_uri ~ "/admin/&qu ...

  4. nginx 反向代理配置示例

    Nginx反向代理在生产环境中使用很多的. 场景1: 域名没有备案,可以把域名解析到香港一台云主机上,在香港云主机做个代理,而网站数据是在大陆的服务器上. server { listen ; serv ...

  5. python 整数转16进制数

    def toHex(num): """ :type num: int :rtype: str """ chaDic = {: : : : : ...

  6. 浅谈UDF并行

    首先我们来看说明UDF并行流程的这个图 网格和求解数据分布和储存在计算节点(compute-node)处理器上,而对于GUI界面和主机(host)节点上不存储任何数据,主机节点将命令从GUI传递到0节 ...

  7. Windows 文件过滤驱动经验总结

    Windows 文件过滤驱动经验总结作者:sinister 本文转载自驱动开发网 看了 ChuKuangRen 的第二版<文件过滤驱动开发教程>后,颇有感触.我想,交流都是建立在平等的基础 ...

  8. Mysql之Incorrect string value: '\xF0\x9F\x98\x89 \xE6... 保存emoji表情

    错误信息如下: Incorrect string value: '\xF0\x9F\x98\x89 \xE6...' 问题产生的原因是字符串不兼容4字节的unicode导致的,一般我们常见的表情编码等 ...

  9. 类中嵌套定义指向自身的引用(C、C++、C#)或指针(C、C++)

    在定义类的时候,类中可以嵌套定义指向自身的引用(C.C++.C#)或指针(C.C++).详见代码: Node类: using System; using System.Collections.Gene ...

  10. C#中得到每周,每月,每季,每年的年初末日期

    DateTime表示时间上的一刻,通常以日期和当天的时间表示.借用这个结构,我们可以实现较丰富的功能,本文给出得到每周每天的方法,及得到本月第一天,本月最后一天,本季第一天,本季最后一天,本年第一天及 ...