欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot項目加載配置文件的6種方式小結(jié)

 更新時間:2023年09月18日 11:17:41   作者:Roue治愈者ヅ聶  
這篇文章給大家總結(jié)了六種SpringBoot項目加載配置文件的方式,通過@value注入,通過@ConfigurationProperties注入,通過框架自帶對象Environment實現(xiàn)屬性動態(tài)注入,通過@PropertySource注解,yml外部文件,Java原生態(tài)方式注入這六種,需要的朋友可以參考下

1、通過@value注入

滿足條件:

1、該類首先要被SpringBoot框架管理,屬于SpringBoot框架的Bean。

2、application.properties(或者application.yml)配置文件中存在該配置名。(如果不存在可以加冒號,框架會賦值默認(rèn)值,也可以在冒號后面自定義默認(rèn)值。例如:test.domain:)。配置文件中不包含該配置,注解中也沒加冒號,項目啟動就會報錯。

3、被static和finely修飾的屬性配置該注解不會生效。

@Component
public class BaseConfig {
    @Value("${test.domain:}")
    private String domamin;
    @Value("${test.api:}")
    private String api;
}

2、通過@ConfigurationProperties注入

1、該類首先要被SpringBoot框架管理,屬于SpringBoot框架的Bean。

2、@ConfigurationProperties進(jìn)行指定配置文件中key的前綴。進(jìn)行自動裝配屬性。適用于批量綁定,批量注入屬性值。相比@value更省事。

 #application.yml配置文件
es:
  post:9200
  host:localhost
  name:es
@Data
@Component
@ConfigurationProperties(prefix="es")
public class ESProperties {
    private String host;
    private String name;
    private int port;
}

3、通過框架自帶對象Environment實現(xiàn)屬性動態(tài)注入

#application.yml配置文件
es:
  post:9200
  host:localhost
  name:es

方式一:容器自動注入SpringBoot框架自帶的類Environment進(jìn)行實現(xiàn)動態(tài)配置屬性值注入。

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class EsProperties {
    private String host;
    private String name;
    private int post;
    @Resource
    private Environment environment;
    public String getHost() {
        return environment.getProperty("es.host");
    }
    public String getName() {
        return environment.getProperty("es.name");
    }
    public int getPost() {
        String post = environment.getProperty("es.post");
        return Integer.parseInt(post);
    }
}

方式二:自己實現(xiàn)EnvironmentAware接口進(jìn)行重寫方法進(jìn)行注入Environment。好處可以和spring boot框架的解耦性更低一點。

import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class EsProperties implements EnvironmentAware {
    private String host;
    private String name;
    private int post;
    private Environment environment;
    public String getHost() {
        return environment.getProperty("es.host");
    }
    public String getName() {
        return environment.getProperty("es.name");
    }
    public int getPost() {
        String post = environment.getProperty("es.post");
        return Integer.parseInt(post);
    }
    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
}

4、通過@PropertySource注解實現(xiàn)外部配置文件注入屬性值

#es.properties配置文件
es.post=9200
es.host=localhost
es.name=es

1、通過@PropertySource注解實現(xiàn)導(dǎo)入外部配置文件。

2、配合@value注解實現(xiàn)屬性注入或者@ConfigurationProperties注解實現(xiàn)批量注入。

3、該方式只能獲取 .properties 的配置文件不能獲取 .yml 的配置文件。

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = "classpath:es.properties",encoding = "utf-8")
public class EsProperties {
    @Value("${es.host}")
    private String host;
    @Value("${es.name}")
    private String name;
    @Value("${es.post}")
    private int post;
}
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = "classpath:es.properties",encoding = "utf-8")
@ConfigurationProperties(prefix = "es")
public class EsProperties {
    private String host;
    private String name;
    private int post;
}

5、yml 外部配置文件動態(tài)注入

第4中方式只能實現(xiàn) .properties 文件的,該方式是實現(xiàn) .yml 文件的。

1、自定義配置類,實例化PropertySourcesPlaceholderConfigurer類,使用該類進(jìn)行屬性值的注入。

2、實例化完P(guān)ropertySourcesPlaceholderConfigurer類之后,就可以配合@value注解實現(xiàn)屬性注入或者@ConfigurationProperties注解實現(xiàn)批量注入。

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import java.util.Objects;
@Configuration
public class MyYmlConfig {
    @Bean
    public static PropertySourcesPlaceholderConfigurer yamlConfigurer(){
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("es.yml"));
        configurer.setProperties(Objects.requireNonNull(yaml.getObject()));
        return configurer;
    }
}
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class EsProperties {
    @Value("${es.host}")
    private String host;
    @Value("${es.name}")
    private String name;
    @Value("${es.post}")
    private int post;
}
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "es")
public class EsProperties {
    private String host;
    private String name;
    private int post;
}

6、Java原生態(tài)方式注入屬性值

#es.properties配置文件
es.post=9200
es.host=localhost
es.name=es
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
@Component
public class EsProperties {
    private String host;
    private String name;
    private int post;
    @Bean
    public void init(){
        Properties props = new Properties();
        InputStreamReader inputStreamReader = new InputStreamReader(
                this.getClass().getClassLoader().getResourceAsStream("es.properties"), StandardCharsets.UTF_8);
        try {
            props.load(inputStreamReader);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        this.host = props.getProperty("es.host");
        this.name = props.getProperty("es.name");
        this.post = Integer.parseInt(props.getProperty("es.post"));
    }
}

以上就是SpringBoot項目加載配置文件的6種方式小結(jié)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot加載配置文件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 通過netty把百度地圖API獲取的地理位置從Android端發(fā)送到Java服務(wù)器端的操作方法

    通過netty把百度地圖API獲取的地理位置從Android端發(fā)送到Java服務(wù)器端的操作方法

    這篇文章主要介紹了通過netty把百度地圖API獲取的地理位置從Android端發(fā)送到Java服務(wù)器端,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-10-10
  • Java讀寫鎖ReadWriteLock原理與應(yīng)用場景詳解

    Java讀寫鎖ReadWriteLock原理與應(yīng)用場景詳解

    這篇文章主要介紹了Java讀寫鎖ReadWriteLock原理與應(yīng)用場景詳解,讀寫狀態(tài)的設(shè)計,寫鎖的獲取與釋放,鎖降級需要的朋友可以參考下
    2023-02-02
  • java使用Feign實現(xiàn)聲明式Restful風(fēng)格調(diào)用

    java使用Feign實現(xiàn)聲明式Restful風(fēng)格調(diào)用

    這篇文章主要為大家詳細(xì)介紹了java使用Feign實現(xiàn)聲明式Restful風(fēng)格調(diào)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Java流式操作之Collectors工具類操作指南

    Java流式操作之Collectors工具類操作指南

    Collectors是Collector的工具類,類中提供了很多流收集、歸約、分組、分區(qū)等方法,方便我們直接使用,下面這篇文章主要給大家介紹了關(guān)于Java流式操作之Collectors工具類操作的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • 如何設(shè)置springboot禁止日志輸出到控制臺

    如何設(shè)置springboot禁止日志輸出到控制臺

    文章總結(jié):本文主要介紹了SpringBoot項目中使用SLF4J記錄日志時,日志默認(rèn)輸出到控制臺的原因及解決方法,日志框架如Logback默認(rèn)會將日志輸出到控制臺,可以通過`logback-spring.xml`配置文件或配置類來禁止日志輸出到控制臺,并設(shè)置日志輸出級別
    2025-01-01
  • 詳解用Eclipse如何創(chuàng)建Web項目

    詳解用Eclipse如何創(chuàng)建Web項目

    本篇文章主要介紹了詳解用Eclipse如何創(chuàng)建Web項目,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • 一篇文章帶你理解Java的SPI機(jī)制(圖文并茂)

    一篇文章帶你理解Java的SPI機(jī)制(圖文并茂)

    本文詳細(xì)介紹了Java的SPI機(jī)制,包括其定義、用途和實現(xiàn)方式,SPI(ServiceProviderInterface)是一種服務(wù)發(fā)現(xiàn)機(jī)制,用于實現(xiàn)框架或庫的擴(kuò)展點,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-10-10
  • 基于Java實現(xiàn)XML文件的解析與更新

    基于Java實現(xiàn)XML文件的解析與更新

    配置文件可以有很多種格式,包括?INI、JSON、YAML?和?XML。每一種編程語言解析這些格式的方式都不同。本文將通過Java語言實現(xiàn)XML文件的解析與更新,需要的可以參考一下
    2022-03-03
  • Java中StringBuilder常用構(gòu)造方法解析

    Java中StringBuilder常用構(gòu)造方法解析

    這篇文章主要介紹了Java中StringBuilder常用構(gòu)造方法解析,StringBuilder是一個可標(biāo)的字符串類,我們可以吧它看成是一個容器這里的可變指的是StringBuilder對象中的內(nèi)容是可變的,需要的朋友可以參考下
    2024-01-01
  • java生成XML的方法【附demo源碼下載】

    java生成XML的方法【附demo源碼下載】

    這篇文章主要介紹了java生成XML的方法,涉及java針對xml格式文件的簡單操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下
    2016-12-12

最新評論