转: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. SQL进阶-隐式类型转换

    一.隐式类型转换 1.隐式类型转换 隐式类型转换: '; 程序可读性差,且依赖数据库的隐式转换规则,如果数据库升级,则程序可能无法正确执行: 有可能会导致索引失效: 有可能会导致意想不到的结果: 显式 ...

  2. Redis内存数据库

    remote dictionary server 远程字典服务器 Redis默认支持16个数据库,不同的应用应该使用不同的Redis实例存储数据.   支持数据类型:字符串,哈希散列,列表,集合,有序 ...

  3. RK3288板子刷linux-ubuntu16固件

    Firefly-RK3288 是一个高性能平台,拥有强大的多线程运算能力.图形处理能力以及硬件解码 能力,而且支 持 Android和Ubuntu双系统; 下面我们使用RK3288的板子刷linux ...

  4. RequireJS - 快速指南

    原文: https://www.tutorialspoint.com/requirejs/requirejs_quick_guide.htm RequireJS - 概述 RequireJS是一个Ja ...

  5. 如何单独编译Linux内核源码中的驱动为可加载模块?

    答: 分为两步: 1. 配置某个驱动为模块(如: CONFIG_RTC_XXX=m) 2. 指定路径并编译, 如编译drivers/rtc中的驱动 make SUBDIRS=drivers/rtc m ...

  6. Springboot Mybatis 集成 Redis

    版本信息 Sprintboot 采用 2.1.7 RELEASE 版本 Mybatis 采用 2.1.0 Redis 采用 2.1.6.RELEASE Redis 的使用 添加 Redis 依赖 &l ...

  7. Mysql创建数据库以及用户分配权限

    一.创建mysql数据库 1.创建数据库语法 --创建名称为“testdb”数据库,并设定编码集为utf8 CREATE DATABASE IF NOT EXISTS testdb DEFAULT C ...

  8. Java学习-054-Mybatis IN查询,通过 foreach 获取列表数据

    通过如下语句查询商品订单信息: ,,,) 在 Mapper.java 中定义如下接口: List<GoodsOrder> findGoodsOrderByIds(String ids); ...

  9. android分渠道打包,监测日活量统计(基于友盟SDK)

    客服说要看App日活,让加个统计功能. (我们技术部已经混到客服部都能直接提需求的地步) 首先接入友盟统计的SDK,在项目外层的build.gradle中添加依赖'https://dl.bintray ...

  10. Indellij IDEA的菜单 File Edit View等主菜单栏不小心删除,恢复的解决办法

    修改以后保存,重启Idea