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

SpringBoot加載讀取配置文件過(guò)程詳細(xì)分析

 更新時(shí)間:2023年01月18日 15:44:36   作者:FlyLikeButterfly  
在實(shí)際的項(xiàng)目開(kāi)發(fā)過(guò)程中,我們經(jīng)常需要將某些變量從代碼里面抽離出來(lái),放在配置文件里面,以便更加統(tǒng)一、靈活的管理服務(wù)配置信息。所以本文將為大家總結(jié)一下SpringBoot加載配置文件的常用方式,需要的可以參考一下

springboot默認(rèn)讀取的配置文件名字是:“application.properties”和“application.yml”,默認(rèn)讀取四個(gè)位置的文件:根目錄下、根目錄的config目錄下、classpath目錄下、classpath目錄里的config目錄下;

配置文件的讀取順序

  • 根目錄/config/application.properties
  • 根目錄/config/application.yml
  • 根目錄/application.properties
  • 根目錄/application.yml
  • classpath目錄/config/application.properties
  • classpath目錄/config/application.yml
  • classpath目錄/application.properties
  • classpath目錄/application.yml

默認(rèn)可讀取的配置文件全部都會(huì)被讀取合并,按照順序讀取配置,相同的配置項(xiàng)按第一次讀取的值為準(zhǔn),同一個(gè)目錄下properties文件比yml優(yōu)先讀取,通常會(huì)把配置文件放到classpath下,一般是resources里;

多壞境的配置文件

通常可以使用4個(gè)配置文件:(yml也同理)

  • application.properties:默認(rèn)配置文件
  • application-dev.properties:開(kāi)發(fā)環(huán)境配置文件
  • application-prod.properties:生產(chǎn)環(huán)境配置文件
  • application-test.properties:測(cè)試環(huán)境配置文件

在application.properties里配置spring.profiles.active以指定使用哪個(gè)配置文件,可以配置dev、prod、test分別對(duì)應(yīng)以-dev、-prod、-test結(jié)尾的配置文件;(yml配置文件也是同理)

也可以在命令行使用spring.profiles.active指定,例如:java -jarxxxxxx.jar--spring.profiles.active=dev;

個(gè)性化配置

對(duì)于更特殊的個(gè)性化配置可以使用@Profile注解指定;

@Profile標(biāo)簽可以用在@Component或者@Configuration修飾的類(lèi)上,可以標(biāo)記類(lèi)和方法,用來(lái)指定配置名字,然后使用spring.profiles.active指定該配置名字就可生效;

就像這樣:

package testspringboot.test2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("myconfig")
public class MyConfig {
	@Bean("Tom")
	@Profile("A")
	public String a() {
		return "tomtom";
	}
	@Bean("Tom")
	@Profile("B")
	public String b() {
		return "TOMTOM";
	}
	@Bean("Tom")
	public String c() {
		return "ttoomm";
	}
}

然后寫(xiě)一個(gè)controller類(lèi):

package testspringboot.test2;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test2controller")
public class Test2Controller {
	@Resource(name = "Tom")
	public String t;
	@RequestMapping("/test2")
	public String test2() {
		System.out.println(t);
		return "TEST2" + t;
	}
}

啟動(dòng)類(lèi):

package testspringboot.test2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Test2Main {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SpringApplication.run(Test2Main.class, args);
	}
}

配置文件里配置:

server.port=8888
server.servlet.context-path=/testspringboot
spring.profiles.active=myconfig

只指定myconfig配置,則MyConfig類(lèi)里c()的bean生效,訪(fǎng)問(wèn)結(jié)果是:

修改spring.profiles.active=myconfig,A,則MyConfig類(lèi)里標(biāo)記@Profile("A")的bean生效:

修改spring.profiles.active=myconfig,B,則標(biāo)記@Profile("B")的bean生效:

如果去掉spring.profiles.active配置,則就找不到MyConfig里的配置了,啟動(dòng)失敗:

自定義配置文件名稱(chēng)和路徑

可以使用@PropertySource標(biāo)簽指定自定義的配置文件名稱(chēng)和路徑;(默認(rèn)能加載到的配置文件也會(huì)先被加載)

通常只會(huì)用到設(shè)置配置文件的名字,并且配置文件的名字可以隨便定義,可以叫xxxx.properties、a.txt、b.abc等等,但是內(nèi)容格式需要跟.properties一致,即kv格式,所以不能直接加載yml格式的配置文件;

@PropertySource默認(rèn)加載路徑是classpath下,可以使用classpath:xxxx/xxxx/xxxx.properties指定目錄和文件,如果使用根目錄則需要使用file:xxxx/xxxx/xxxx.properties;

可以使用@PropertySource為啟動(dòng)類(lèi)指定springboot的配置文件,能夠做到使用一個(gè)main方法啟動(dòng)兩個(gè)springboot實(shí)例,并各自使用不同的配置文件:

@SpringBootApplication
@PropertySource("classpath:a.properties")
@PropertySource(value = "file:a.properties", ignoreResourceNotFound = true)
public class Test2Main {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SpringApplication.run(Test2Main.class, args);
	}
}

也可以使用@PropertySource配置bean,在使用@Component和@ConfigurationProperties時(shí)也可給bean指定特定配置文件:

放在resources下的配置文件tom.abc:

mybean.name=Tom
mybean.age=12

bean類(lèi)ABC,配置tom.abc文件注入:

package testspringboot.test2;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("mybean")
@PropertySource(value = "classpath:tom.abc")
public class ABC {
	public String name;
	public int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "ABC [name=" + name + ", age=" + age + "]";
	}
}

啟動(dòng)類(lèi)可以直接獲得bean:

package testspringboot.test2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource("classpath:a.properties")
@PropertySource(value = "file:a.properties", ignoreResourceNotFound = true)
public class Test2Main {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ConfigurableApplicationContext ctx = SpringApplication.run(Test2Main.class, args);
		System.out.println(ctx.getBean(ABC.class));
	}
}

啟動(dòng)結(jié)果:

可以直接獲得配置的bean,也可以在代碼里使用@Resource或者@Autowired獲得;

加載yml文件

如果使用@PropertySource配置yml,則需要自定義一個(gè)factory實(shí)現(xiàn):

package testspringboot.test2;
import java.io.IOException;
import java.util.Properties;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
public class YmlPropertiesFactory implements PropertySourceFactory {
	@Override
	public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
		YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();
		factoryBean.setResources(resource.getResource());
		factoryBean.afterPropertiesSet();
		Properties source = factoryBean.getObject();
		return new PropertiesPropertySource("myyml", source);
	}
}

然后在@PropertySource里配置factory和yml文件:@PropertySource(value = "myapplication.yml", factory = YmlPropertiesFactory.class),就可以加載yml配置文件了;

到此這篇關(guān)于SpringBoot加載讀取配置文件過(guò)程詳細(xì)分析的文章就介紹到這了,更多相關(guān)SpringBoot加載配置文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java如何執(zhí)行cmd命令

    Java如何執(zhí)行cmd命令

    這篇文章主要介紹了Java如何執(zhí)行cmd命令問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(7)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(7)

    下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你
    2021-07-07
  • Java線(xiàn)程狀態(tài)變換過(guò)程代碼解析

    Java線(xiàn)程狀態(tài)變換過(guò)程代碼解析

    這篇文章主要介紹了Java線(xiàn)程狀態(tài)變換過(guò)程代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Spring Boot從Controller層進(jìn)行單元測(cè)試的實(shí)現(xiàn)

    Spring Boot從Controller層進(jìn)行單元測(cè)試的實(shí)現(xiàn)

    這篇文章主要介紹了Spring Boot從Controller層進(jìn)行單元測(cè)試的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Jmeter測(cè)試時(shí)遇到的各種亂碼問(wèn)題及解決

    Jmeter測(cè)試時(shí)遇到的各種亂碼問(wèn)題及解決

    這篇文章主要介紹了Jmeter測(cè)試時(shí)遇到的各種亂碼問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • java利用多線(xiàn)程和Socket實(shí)現(xiàn)猜拳游戲

    java利用多線(xiàn)程和Socket實(shí)現(xiàn)猜拳游戲

    這篇文章主要為大家詳細(xì)介紹了java利用多線(xiàn)程和Socket實(shí)現(xiàn)猜拳游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • IDEA找不到database圖標(biāo)的簡(jiǎn)單圖文解決方法

    IDEA找不到database圖標(biāo)的簡(jiǎn)單圖文解決方法

    idea是一個(gè)功能十分強(qiáng)大的IDE,大家在使用他進(jìn)行開(kāi)發(fā)時(shí)候,必不可少的就是連接數(shù)據(jù)庫(kù)了,這篇文章主要給大家介紹了關(guān)于IDEA找不到database圖標(biāo)的解決方法,需要的朋友可以參考下
    2024-07-07
  • java讀取json文件的2種方式例子

    java讀取json文件的2種方式例子

    這篇文章主要給大家介紹了關(guān)于java讀取json文件的2種方式,在開(kāi)發(fā)過(guò)程中有時(shí)會(huì)遇到需要讀取.json文件的需求,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • Java Scanner用法案例詳解

    Java Scanner用法案例詳解

    這篇文章主要介紹了Java Scanner用法案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • springboot jar包外置配置文件的解決方法

    springboot jar包外置配置文件的解決方法

    這篇文章主要給大家介紹了關(guān)于spring boot jar包外置配置文件的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12

最新評(píng)論

红河县| 南昌市| 云南省| 沛县| 古浪县| 鄂伦春自治旗| 惠来县| 旬阳县| 平利县| 祁连县| 册亨县| 兴文县| 鸡东县| 辉南县| 城固县| 金坛市| 信宜市| 扎赉特旗| 高邮市| 临夏县| 新安县| 平湖市| 广元市| 丰顺县| 精河县| 永春县| 西宁市| 平陆县| 思南县| 天台县| 民权县| 旬邑县| 宿松县| 寿光市| 连州市| 古蔺县| 香格里拉县| 阳信县| 勐海县| 大荔县| 万山特区|