转: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. 【luoguP2986】[USACO10MAR]伟大的奶牛聚集Great Cow Gathering

    题目链接 先把\(1\)作为根求每个子树的\(size\),算出把\(1\)作为集会点的代价,不难发现把集会点移动到\(u\)的儿子\(v\)上后的代价为原代价-\(v\)的\(size\)*边权+( ...

  2. nginx.conf 配置解析之 server配置

    server{} 包含在http{}内部,每一个server{}都是一个虚拟主机(站点) 以下为nginx.conf配置文件中server{  }部分的内容. server { listen ; // ...

  3. ftp使用

    1.pom文件中添加依赖 <!-- ftp使用 --><dependency> <groupId>commons-net</groupId> <a ...

  4. rabbitmq在linux下单节点部署和基本使用

    RabbitMQ是基于erlang开发的消息服务,官网为:https://www.rabbitmq.com,RabbitMQ要依赖erlang运行,所以要先安装erlang环境,rabbitmq可以用 ...

  5. jQuery 取值操作

    模板使用: https://startbootstrap.com/themes/sb-admin-2/ 使用的 bootstrap 模块 ,上面的这个网站可以下载 select 取值 <sele ...

  6. SpringBoot入门-JPA(三)

    什么是JPA JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. pom.xml <par ...

  7. osg geometry清空vertex

    _vertices->clear(); _vertices->dirty(); _drawArrays->set(sog::PrimitiveSet::POINTS,0,0); _g ...

  8. 各种转码(bytes、string、base64、numpy array、io、BufferedReader )

    bytes 与 string 之间互转 Python3 最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分.文本总是 Unicode,由str类型表示,二进制数据则由 bytes 类型表示. ...

  9. flow

    Flow vs Stream https://wikidiff.com/flow/stream As nouns the difference between flow and stream is t ...

  10. Srping的IOC

    XML方式: IOC:控制反转的底层原理就是:工厂模式+反射+配置文件DI:依赖注入就是通过配置文件设置属性值 BeanFactory 是老版本的工厂类:调用getBean的时候,才会生成类的实例Ap ...