最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

1、通過(guò)@value注入

滿足條件:

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

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

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

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

2、通過(guò)@ConfigurationProperties注入

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

2、@ConfigurationProperties進(jìn)行指定配置文件中key的前綴。進(jìn)行自動(dòng)裝配屬性。適用于批量綁定,批量注入屬性值。相比@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、通過(guò)框架自帶對(duì)象Environment實(shí)現(xiàn)屬性動(dòng)態(tài)注入

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

方式一:容器自動(dòng)注入SpringBoot框架自帶的類Environment進(jìn)行實(shí)現(xiàn)動(dòng)態(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);
    }
}

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

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、通過(guò)@PropertySource注解實(shí)現(xiàn)外部配置文件注入屬性值

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

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

2、配合@value注解實(shí)現(xiàn)屬性注入或者@ConfigurationProperties注解實(shí)現(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 外部配置文件動(dòng)態(tài)注入

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

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

2、實(shí)例化完P(guān)ropertySourcesPlaceholderConfigurer類之后,就可以配合@value注解實(shí)現(xiàn)屬性注入或者@ConfigurationProperties注解實(shí)現(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項(xiàng)目加載配置文件的6種方式小結(jié)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot加載配置文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

宿松县| 新安县| 上犹县| 嘉荫县| 毕节市| 精河县| 沾化县| 通城县| 通山县| 璧山县| 松潘县| 昭觉县| 武宣县| 项城市| 安庆市| 龙南县| 抚顺市| 天气| 寻乌县| 赣榆县| 东海县| 徐水县| 鸡东县| 五寨县| 澎湖县| 长宁县| 嘉兴市| 宜兴市| 张北县| 绵阳市| 龙泉市| 富民县| 金溪县| 宜春市| 镇巴县| 宁化县| 墨竹工卡县| 新源县| 西华县| 祥云县| 北宁市|