转: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. PHP入门之调试

    环境 开发工具VSCode 2019 代码库 自建git 仓库 win7集成环境 PHPStudy2018  具体设置信息:   PHP 5.4.45 ,MySql 5.7 , Apache 2.4 ...

  2. gitlab 从古老的 bitnami 版本 迁移到官方最新版本

    这是我之前发布在 yuque 的文章.是我刚来新公司的时候帮公司搬迁 git 记录下来的,现在看来去掉敏感部分直接发布也没啥问题啦,就搬家过来,我自己也方便查 XD . 8.1.6 -> 10. ...

  3. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

  4. [技术博客]react native事件监听、与原生通信——实现对通知消息的响应

    在react native中会涉及到很多页面之间的参数传递问题.静态的参数传递通常利用组件的Props属性,在初始化组件时即可从父组件中将参数传递到子组件中.对于非父子关系的组件来说,无法直接传递参数 ...

  5. Spring Boot-JPA常用注解

  6. sharding jdbc(sphere) 3.1.0 spring boot配置

    sharding jdbc 2.x系列详解参见https://www.cnblogs.com/zhjh256/p/9221634.html. 最近将sharding jdbc的配置从xml切换到了sp ...

  7. sumdoc t411 dir.txt

    C:\Users\zhoufeiyue\Documents\sumdoc t411\(9+条消息)redis Jedis存取list对象和map - shenjianxz的博客 - CSDN博客.mh ...

  8. nginx https 转发

    add_header Content-Security-Policy upgrade-insecure-requests;

  9. Ubuntu 上多版本软件管理方法

    https://linuxize.com/post/how-to-install-gcc-compiler-on-ubuntu-18-04/ sudo apt install software-pro ...

  10. word 条件多项式公式对齐

    条件多项式公式对齐 觉得有用的话,欢迎一起讨论相互学习~Follow Me 对于使用word编写具有多个多项式的公式时,经常会有所偏移 最不优雅的方式就是使用逗号进行分隔和排版使其公式上下对齐 第二种 ...