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

SpringBoot加載讀取配置文件過程詳細分析

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

springboot默認讀取的配置文件名字是:“application.properties”和“application.yml”,默認讀取四個位置的文件:根目錄下、根目錄的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

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

多壞境的配置文件

通??梢允褂?個配置文件:(yml也同理)

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

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

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

個性化配置

對于更特殊的個性化配置可以使用@Profile注解指定;

@Profile標簽可以用在@Component或者@Configuration修飾的類上,可以標記類和方法,用來指定配置名字,然后使用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";
	}
}

然后寫一個controller類:

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;
	}
}

啟動類:

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類里c()的bean生效,訪問結(jié)果是:

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

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

如果去掉spring.profiles.active配置,則就找不到MyConfig里的配置了,啟動失?。?/p>

自定義配置文件名稱和路徑

可以使用@PropertySource標簽指定自定義的配置文件名稱和路徑;(默認能加載到的配置文件也會先被加載)

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

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

可以使用@PropertySource為啟動類指定springboot的配置文件,能夠做到使用一個main方法啟動兩個springboot實例,并各自使用不同的配置文件:

@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時也可給bean指定特定配置文件:

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

mybean.name=Tom
mybean.age=12

bean類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 + "]";
	}
}

啟動類可以直接獲得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));
	}
}

啟動結(jié)果:

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

加載yml文件

如果使用@PropertySource配置yml,則需要自定義一個factory實現(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ān)SpringBoot加載配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

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

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

    Java日常練習題,每天進步一點點(7)

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

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

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

    Spring Boot從Controller層進行單元測試的實現(xiàn)

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

    Jmeter測試時遇到的各種亂碼問題及解決

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

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

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

    IDEA找不到database圖標的簡單圖文解決方法

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

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

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

    Java Scanner用法案例詳解

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

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

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

最新評論

饶阳县| 临桂县| 鄄城县| 柘城县| 繁峙县| 龙南县| 昌乐县| 上虞市| 苍山县| 舟山市| 辽宁省| 沙湾县| 沁水县| 沐川县| 图木舒克市| 尚志市| 株洲市| 葫芦岛市| 济源市| 海城市| 吉隆县| 崇州市| 滕州市| 福清市| 广汉市| 枞阳县| 武安市| 苍溪县| 安仁县| 安陆市| 营山县| 体育| 荔波县| 神农架林区| 鄂尔多斯市| 彭州市| 通州市| 周至县| 绍兴市| 武城县| 嘉鱼县|