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

springboot中獲取配置文件中屬性值的幾種方式小結(jié)

 更新時(shí)間:2024年05月26日 10:58:29   作者:Holy_Java  
本文主要介紹了springboot中獲取配置文件中屬性值的幾種方式小結(jié),主要介紹了六種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

第一章、使用@Value注解

①@Value注解用于獲取配置文件中的屬性定義并綁定到Java Bean或?qū)傩灾小T诤诵呐渲梦募pplicatin.properties中,添加兩個(gè)自定義配置項(xiàng)school.name和school.website。

在這里插入圖片描述

②在SpringBootController中定義屬性,并使用@Value注解或者自定義配置值,并對(duì)其方法進(jìn)行測(cè)試

package com.example.springboot.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SpringBootController {
    @Value("${school.name}")
    private String schoolName;

    @Value("${school.websit}")
    private String schoolWebsit;

    @RequestMapping(value = "/springBoot/first")
    @ResponseBody
    public String say() {
        return schoolName + "------" + schoolWebsit;
    }
}

③訪問瀏覽器成功,說明成功通過@value讀取配置的屬性值

在這里插入圖片描述

第二章、使用@PropertySource注解

2.1)指定文件路徑,在setter方法上添加@Value注解

@PropertySource(“classpath:coremail.properties”)是指定配置文件位置的注解。Spring 可以在類路徑下找到并加載這個(gè)coremail.properties屬性文件。需要再在屬性上面搭配@value注解使用其中定義的屬性值。

在這里插入圖片描述

2.2)指定文件路徑,在屬性上添加@Value注解

在這里插入圖片描述

2.3)使用ApplicationContextAware接口來獲得TConfig中對(duì)應(yīng)properties文件的屬性值

2.3.1)TaskConfig類與TestTaskConfig類

有兩個(gè)配置文件,使用兩個(gè)config類指定properties類文件路徑

在這里插入圖片描述

@Configuration//當(dāng)一個(gè)類被標(biāo)注了@Configuration注解時(shí),Spring會(huì)將這個(gè)類識(shí)別為配置類,
// 用于定義Bean的創(chuàng)建和配置。即使沒有顯式地定義Bean,配置類本身也可以被注入到其他類中

@PropertySource(value = {"classpath:mailSendConfig.properties"},encoding="utf-8")
public class  TaskConfig {
}

@Configuration
@PropertySource(value = {"classpath:testConfig.properties"},encoding="utf-8")
public class TestTaskConfig {
}

在這里插入圖片描述

2.3.2)ConfigLoder類

用來加載config類

@Component
public class ConfigLoder {
    @Autowired
    static TaskConfig taskConfig;

    @Autowired
    static TestTaskConfig testTaskConfig;

    public static TaskConfig getTaskConfig(){
        return taskConfig;
    }
    public static TestTaskConfig getTestTaskConfig(){
        return testTaskConfig;
    }
}

在這里插入圖片描述

2.3.3)啟動(dòng)類:啟動(dòng)只運(yùn)行一次

//@SpringBootApplication
public class LineApplication implements ApplicationRunner {

    @Override
    public  void run(ApplicationArguments args) throws Exception{
        System.out.println("====================");
        XXXXService mailSend =new XXXXService();
        mailSend.run();
    }

    public static void main(String[] args){
        SpringApplication.run(LineApplication.class,args);


    }

}

2.3.4)springUtil類實(shí)現(xiàn)ApplicationContextAware接口

通過實(shí)現(xiàn)ApplicationContextAware接口獲取ApplicationContext對(duì)象,來獲得TaskConfig中對(duì)應(yīng)properties文件的屬性值。

@Component
public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringUtil.applicationContext == null) {
            SpringUtil.applicationContext = applicationContext;
        }
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }

    public static String getProperty(String propertyName) {
        return getApplicationContext().getEnvironment().getProperty(propertyName);
    }

    public static String getProperty(Class clazz, String PropertyName) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(clazz);
        ConfigurableEnvironment configurableEnvironment = context.getEnvironment();
        return configurableEnvironment.getProperty(PropertyName);
    }
}

2.3.5)TestGetProperties測(cè)試類:測(cè)試獲取屬性值

package com.icbc.app.runner;

import com.icbc.app.config.SpringUtil;
import com.icbc.app.config.TaskConfig;
import com.icbc.app.config.TestTaskConfig;
import org.springframework.stereotype.Component;

import java.util.ResourceBundle;


@Component
public class TestGetProperties {

    public void run(String... args) throws Exception {
        //第一種:獲取系統(tǒng)環(huán)境的   systemTime   值
        System.out.println("系統(tǒng)值systemTime=" + System.getenv("CALLTIME"));

        //第二種:ResourceBundle獲取testConfig配置文件的的  name 值
        ResourceBundle bundle = ResourceBundle.getBundle("testConfig");
        String name = bundle.getString("name");
        System.out.println("ResourceBundle獲取name值=" + name);

        //第三種:TaskConfig.properties的sleepTime值
        String sleepTime = SpringUtil.getProperty(TaskConfig.class, "sleepTime");
        System.out.println("TaskConfig.properties的sleepTime值="+sleepTime);

        //第四種:TestTaskConfig.properties文件的Time值
        String time = SpringUtil.getProperty(TestTaskConfig.class, "Time");
        System.out.println("TestTaskConfig.properties文件的Time值="+time);
    }
}

第三章、使用@Configurationproperties注解

@Configurationproperties(prefix=”xxx”)prefix的作用是區(qū)分同名配置,如果不指定,那么會(huì)去配置文件中尋找與該類的屬性名一致的配置文件。
prefix怎么使用呢?

在生產(chǎn)環(huán)境配置文件applicatin-product.properties中,有自定義的三個(gè)school前綴的配置項(xiàng)

在這里插入圖片描述

在核心配置文件applicatin.properties中激活生產(chǎn)環(huán)境,這里的product對(duì)應(yīng)文件名application-produc的橫杠后面的produc

在這里插入圖片描述

當(dāng)指定prefix之后,就不需要再在屬性上面搭配@value注解使用,因?yàn)楫?dāng)指定前綴school之后,屬性就會(huì)自動(dòng)注入

在這里插入圖片描述

第四章、使用Java Properties類

Properties prop = new Properties();
try (InputStream input = new FileInputStream("config.properties")) {
    prop.load(input);
    String dbUrl = prop.getProperty("db.url");
    String dbUser = prop.getProperty("db.user");
    String dbPassword = prop.getProperty("db.password");
    // 使用讀取到的屬性進(jìn)行后續(xù)操作
} catch (IOException ex) {
    ex.printStackTrace();
}

第五章、使用Environment接口

配置文件如圖:

在這里插入圖片描述

獲取配置文件中的屬性值:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {

 	public static String Value1 ;
 	public static String Value2 ;
 	
    @Autowired
    private Environment environment;

    public void getPropertyFromEnvironment() {
        Value1 = environment.getProperty("school.name");
        Value2 = environment.getProperty("school.buile.age");
        System.out.println("Value1: " + Value + "Value1: "+Value2 );
    }
}

第六章、使用ResourceBundle類

新建一個(gè)配置文件:ResourceBunTest.properties,信息如下圖

在這里插入圖片描述

使用ResourceBundle類獲取配置文件內(nèi)容

public class ResourceBundleTest {

    public static void main(String[] args) {
        testResoBund();
    }
    //獲取配置文件信息的方法
    public static void testResoBund(){
    //根據(jù)配置文件名稱獲得ResourceBundle  對(duì)象
        ResourceBundle bundle = ResourceBundle.getBundle("ResourceBunTest");
        String userId = bundle.getString("userId");
        String userName = bundle.getString("userName");
        String age = bundle.getString("age");

        System.out.println(userId+"---"+userName+"---"+age);

    }
}

測(cè)試結(jié)果:獲取成功

在這里插入圖片描述

到此這篇關(guān)于springboot中獲取配置文件中屬性值的幾種方式小結(jié)的文章就介紹到這了,更多相關(guān)springboot獲取配置文件屬性值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • JAVA和C#的語法特性及優(yōu)缺點(diǎn)對(duì)比

    JAVA和C#的語法特性及優(yōu)缺點(diǎn)對(duì)比

    Java和C#是當(dāng)今流行的兩種面向?qū)ο蟮木幊陶Z言,它們都源自C語言的語法風(fēng)格,但各自發(fā)展出了獨(dú)特的特性,這篇文章主要介紹了JAVA和C#的語法特性及優(yōu)缺點(diǎn)對(duì)比的相關(guān)資料,需要的朋友可以參考下
    2025-11-11
  • SpringBoot bean查詢加載順序流程詳解

    SpringBoot bean查詢加載順序流程詳解

    當(dāng)你在項(xiàng)目啟動(dòng)時(shí)需要提前做一個(gè)業(yè)務(wù)的初始化工作時(shí),或者你正在開發(fā)某個(gè)中間件需要完成自動(dòng)裝配時(shí)。你會(huì)聲明自己的Configuration類,但是可能你面對(duì)的是好幾個(gè)有互相依賴的Bean
    2023-03-03
  • Spring boot配置 swagger的示例代碼

    Spring boot配置 swagger的示例代碼

    Swagger是一組開源項(xiàng)目,Spring 基于swagger規(guī)范,可以將基于SpringMVC和Spring Boot項(xiàng)目的項(xiàng)目代碼,自動(dòng)生成JSON格式的描述文件,接下來通過本文給大家介紹Spring boot配置 swagger的示例代碼,一起看看吧
    2021-09-09
  • idea中springboot項(xiàng)目創(chuàng)建后追加依賴

    idea中springboot項(xiàng)目創(chuàng)建后追加依賴

    在項(xiàng)目創(chuàng)建的時(shí)候選擇好依賴創(chuàng)建項(xiàng)目,之后追加依賴不是很方便,本文就來介紹一下idea中springboot項(xiàng)目創(chuàng)建后追加依賴,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03
  • Java HashMap兩種簡(jiǎn)便排序方法解析

    Java HashMap兩種簡(jiǎn)便排序方法解析

    這篇文章主要介紹了Java HashMap兩種簡(jiǎn)便排序方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Java虛擬機(jī)JVM優(yōu)化實(shí)戰(zhàn)的過程全記錄

    Java虛擬機(jī)JVM優(yōu)化實(shí)戰(zhàn)的過程全記錄

    有人說Java之所以能夠崛起,JVM功不可沒。Java虛擬機(jī)最初服務(wù)于讓Java語言凌駕于平臺(tái)之上,實(shí)現(xiàn)“編寫一次,到處運(yùn)行”,那么下面這篇文章主要給大家分享了個(gè)關(guān)于Java虛擬機(jī)JVM優(yōu)化實(shí)戰(zhàn)的過程全記錄,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-08-08
  • 使用Java代碼實(shí)現(xiàn)Redis和數(shù)據(jù)庫(kù)數(shù)據(jù)同步

    使用Java代碼實(shí)現(xiàn)Redis和數(shù)據(jù)庫(kù)數(shù)據(jù)同步

    這篇文章主要介紹了使用Java代碼實(shí)現(xiàn)Redis和數(shù)據(jù)庫(kù)數(shù)據(jù)同步問題,文中通過代碼示例給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-06-06
  • 基于springboot實(shí)現(xiàn)redis分布式鎖的方法

    基于springboot實(shí)現(xiàn)redis分布式鎖的方法

    這篇文章主要介紹了基于springboot實(shí)現(xiàn)redis分布式鎖的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Java 泛型總結(jié)及詳解

    Java 泛型總結(jié)及詳解

    這篇文章主要介紹了Java 泛型的相關(guān)資料,并附簡(jiǎn)單實(shí)例代碼,需要的朋友可以參考下
    2016-09-09
  • Springboot中用 Netty 開啟UDP服務(wù)方式

    Springboot中用 Netty 開啟UDP服務(wù)方式

    這篇文章主要介紹了Springboot中用 Netty 開啟UDP服務(wù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評(píng)論

西安市| 荣成市| 兴仁县| 荆州市| 商洛市| 郁南县| 北宁市| 肇庆市| 于田县| 黑龙江省| 怀远县| 安多县| 靖远县| 东源县| 盈江县| 阿瓦提县| 育儿| 青冈县| 泰兴市| 石柱| 南宫市| 论坛| 西贡区| 静宁县| 鲁山县| 逊克县| 会泽县| 泸西县| 成都市| 台州市| 兴安盟| 象州县| 潮安县| 庐江县| 简阳市| 平湖市| 小金县| 大同县| 大姚县| 革吉县| 云林县|