springboot获取properties配置文件的属性

jinyu59 jinyu59     2022-12-08     366

关键词:

自定义properties文件获取属性

 使用

  @ConfigurationProperties((prefix = "demo"))

  @PropertySource("classpath:myconfig.properties")

来批量注值到bean中

@Component
@ConfigurationProperties(prefix = "com.jy")
@PropertySource("classpath:myconfig.properties")
public class TestBean 
    private String bbb;

    public String getBbb() 
        return bbb;
    

    public void setBbb(String aaa) 
        this.bbb = aaa;
    

  不要忘了@Component

application.properties获取属性

application.propertie中定义一个属性

com.jy.aaa=111

一.使用Environment获取属性

  1).项目主程序中 : 使用ConfigurableApplicationContext的getEnvironment()方法

@SpringBootApplication
public class TestApplication 

    public static void main(String[] args) 
        ConfigurableApplicationContext context = SpringApplication.run(TestApplication.class, args);
        String aaa = context.getEnvironment().getProperty("aaa");
    

  2).其他类中 : 直接使用@AutoWired获取Environment对象

    @Autowired
    Environment env;

二.直接使用@Value("属性全名")注值

    @Value("aaa")
    String aaa;

三.使用@ConfigurationProperties((prefix = "demo"))批量注值

@Component
@ConfigurationProperties(prefix = "com.jy")
public class TestBean 
    private String aaa; //getter&setter方法省略

  使用的时候直接@Auto Wired获取TestBean对象即可

 

 

f

springboot系列配置文件详解

...定义数据配置1.通过prefix2.通过@value注解获取 引言:Springboot有一个全局配置文件,这个配置文件默认是properties文件,就是application.properties文件,其实还有一种文件 查看详情

springboot的配置文件——springboot

Spring配置文件的类型和作用  SpringBoot是基于约定的,所以很多配置都有默认值,但是如果想要使用自己的配置替换默认配置的话,就可以使用application.properties或者application.yml(yaml)进行配置。  SpringBoot默认会从Resources目录下... 查看详情

springboot用@configurationproperties获取配置文件值

SpringBoot的配置文件有yml和properties两种,看一些文章说yml以数据为中心,比较好。个人觉得properties更好用,所以这里以properties格式为例来说。我们都知道@Value 注解可以从配置文件读取一个配置,如果只是配置某个值,比如&n... 查看详情

springboot--配置(yaml/properties)语法(代码片段)

01:SpringBoot–配置(yaml/properties)语法02:SpringBoot–配置(yaml/properties)语法获取配置文件信息、SpringBootConfigurationAnnotation…03:SpringBoot–配置:多环境配置ProfileSp 查看详情

Spring Boot:Jackson 不会从“application.properties”中获取配置

】SpringBoot:Jackson不会从“application.properties”中获取配置【英文标题】:SpringBoot:Jacksonwon\'tpickupconfigurationfrom"application.properties"【发布时间】:2018-02-2505:03:42【问题描述】:我有一个基于Maven的多模块SpringBoot应用程序。在... 查看详情

springboot入门十,获取配置文件信息

SpringBoot获取配置文件的信息有很多,这里介绍比较常用的三种方式 默认获取的都是application.properties文件中的信息1.application.properties配置文件内容如下:server.port=80spring.mvc.view.prefix=/jsp/spring.mvc.view.suffix=.jsp#中文需要转换成为ASCII... 查看详情

springboot配置文件application-dev.properties,application-prod.properties,application-test.properties(代码片

springboot的配置文件有以下application.propertiesapplication-dev.properties开发环境application-prod.properties运行环境application-test.properties测试环境在application.properties中配置内容spring.profiles.active=dev  说明是项目默认使用配置文 查看详情

springboot加载properties和yml配置文件的顺序

...s中:server.port=8001,application.yml中:server.port=8888。问题:springboot是否都加载这两个配置文件?如果两个文件有相同的key,取哪一个文件的value?答:都加载,且按properties→yml的顺序加载。在看到spring.factories中,配置加载器顺序... 查看详情

java框架之springboot二:springboot配置获取

java框架之Springboot二:SpringBoot配置获取resources文件夹中的目录结构:static:保存所有的静态资源;js,css,imagestemplates:保存所有的模板页面;application.properties:配置文件,可以修改一些默认配置配置文件... 查看详情

springboot利用注解@value获取properties属性为null

今天在项目中想使用@Value来获取Springboot中properties中属性值。场景:定义了一个工具类,想要获取一些配置参数,使用了@value来获取,但是死活也获取不到。如何解决:在使用这个工具类的时候是new的,要想使用@value来获取,必... 查看详情

springboot之配置

 回顾:SpringBoot之基础配置文件① 两种全局配置文件(文件名是固定的)  配置文件放在src/main/resources目录或者类路径/config下  application.properties(优先级高)  application.yml/application.yaml  配置文 查看详情

springboot属性类自动加载配置文件中的值

springboot属性类自动加载配置文件中的值,如Person类加载在yml中配置的name,age等属性值,可以通过如下步骤获取:类上添加@ConfigurationProperties注解,prefix为yml中配置的属性名称,要想属性类生效得加上@Component注解如果想要在yml中... 查看详情

springboot读取application.properties文件方法

首先在配置文件application.properties定义如下内容:aaa=123然后在控制器中编写:(只能在控制器中才能自动注入) 定义@Autowired即可自动注入 Enviroment,然后用env.getProperty("参数名")即可获取任意的配置参数。  查看详情

springboot获取配置文件,就这么简单。

在讲SpringBoot获取配置文件之前我们需要对SpringBoot的项目有一个整体的了解,如何创建SpringBoot项目,项目结构等等知识点,我在这里就不一一讲述了,没有学过的小伙伴可以自己在网上找一些资料进行学习,很简单的。下面让我... 查看详情

springboot--配置:配置文件加载顺序(代码片段)

01:SpringBoot–配置(yaml/properties)语法02:SpringBoot–配置(yaml/properties)语法获取配置文件信息、SpringBootConfigurationAnnotation…03:SpringBoot–配置:多环境配置ProfileSp 查看详情

springboot2系列教程理解springboot配置文件application.properties

在SpringBoot中,配置文件有两种不同的格式,一个是properties,另一个是yaml。虽然properties文件比较常见,但是相对于properties而言,yaml更加简洁明了,而且使用的场景也更多,很多开源项目都是使用yaml进行配置(例如Hexo)。除了... 查看详情

springboot之配置文件application.properties

SpringBoot默认的配置文件为application.properties,放在src/main/resources目录下或者类路径的/config目录下,作为SpringBoot的全局配置文件,对一些默认的配置进行修改。接下来使用例子展示SpringBoot配置文件的用法:首先在src/main/resources下... 查看详情

在我的Springboot项目中,如何使用@Value注解来获取Properties文件中的属性值

】在我的Springboot项目中,如何使用@Value注解来获取Properties文件中的属性值【英文标题】:InmySpringbootproject,howtouse@ValueannotationtogetthepropertyvalueinthePropertiesfile【发布时间】:2021-11-2908:57:05【问题描述】:有两个类和一个配置文件... 查看详情