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

SpringBoot配置文件加載方法詳細(xì)講解

 更新時(shí)間:2022年10月27日 10:57:57   作者:FlyLikeButterfly  
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里;

多壞境的配置文件

通??梢允褂?個(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生效,訪問(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配置文件加載方法詳細(xì)講解的文章就介紹到這了,更多相關(guān)SpringBoot配置文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring Security代碼實(shí)現(xiàn)JWT接口權(quán)限授予與校驗(yàn)功能

    Spring Security代碼實(shí)現(xiàn)JWT接口權(quán)限授予與校驗(yàn)功能

    本文給大家介紹Spring Security代碼實(shí)現(xiàn)JWT接口權(quán)限授予與校驗(yàn)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2019-12-12
  • 解決zuulGateway網(wǎng)關(guān)添加路由異常熔斷問(wèn)題

    解決zuulGateway網(wǎng)關(guān)添加路由異常熔斷問(wèn)題

    這篇文章主要介紹了解決zuulGateway網(wǎng)關(guān)添加路由異常熔斷問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • Java 深入探究講解抽象工廠模式

    Java 深入探究講解抽象工廠模式

    當(dāng)系統(tǒng)所提供的工廠所需生產(chǎn)的具體產(chǎn)品并不是一個(gè)簡(jiǎn)單的對(duì)象,而是多個(gè)位于不同產(chǎn)品等級(jí)結(jié)構(gòu)中屬于不同類(lèi)型的具體產(chǎn)品時(shí)需要使用抽象工廠模式,抽象工廠模式是所有形式的工廠模式中最為抽象和最具一般性的一種形態(tài)
    2022-04-04
  • 深入淺析Java中的final關(guān)鍵字

    深入淺析Java中的final關(guān)鍵字

    在Java中,final關(guān)鍵字可以用來(lái)修飾類(lèi)、方法和變量(包括成員變量和局部變量),下面通過(guò)本篇文章給大家介紹java中的final關(guān)鍵字,對(duì)java fina關(guān)鍵字相關(guān)知識(shí)感興趣的朋友一起看看吧
    2015-12-12
  • Java實(shí)現(xiàn)HashMap排序方法的示例詳解

    Java實(shí)現(xiàn)HashMap排序方法的示例詳解

    這篇文章主要通過(guò)一些示例為大家介紹了Java對(duì)HashMap進(jìn)行排序的方法,幫助大家更好的理解和使用Java,感興趣的朋友可以了解一下
    2022-05-05
  • IDEA快速部署Spring?Boot?項(xiàng)目到Docker的實(shí)現(xiàn)方法

    IDEA快速部署Spring?Boot?項(xiàng)目到Docker的實(shí)現(xiàn)方法

    本文主要介紹了IDEA快速部署Spring?Boot?項(xiàng)目到Docker的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Go Java 算法之迷你語(yǔ)法分析器示例詳解

    Go Java 算法之迷你語(yǔ)法分析器示例詳解

    這篇文章主要為大家介紹了Go Java 算法之迷你語(yǔ)法分析器示例詳解,
    有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Mybatis_plus基礎(chǔ)教程(總結(jié)篇)

    Mybatis_plus基礎(chǔ)教程(總結(jié)篇)

    這篇文章主要介紹了Mybatis_plus基礎(chǔ)教程(總結(jié)篇),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java實(shí)現(xiàn)Excel轉(zhuǎn)PDF的兩種方法詳解

    Java實(shí)現(xiàn)Excel轉(zhuǎn)PDF的兩種方法詳解

    使用具將Excel轉(zhuǎn)為PDF的方法有很多,在這里我給大家介紹兩種常用的方法:使用spire轉(zhuǎn)化PDF、使用jacob實(shí)現(xiàn)Excel轉(zhuǎn)PDF,分別應(yīng)對(duì)兩種不一樣的使用場(chǎng)景,需要的可以參考一下
    2022-01-01
  • 使用@ControllerAdvice同時(shí)配置過(guò)濾多個(gè)包

    使用@ControllerAdvice同時(shí)配置過(guò)濾多個(gè)包

    這篇文章主要介紹了使用@ControllerAdvice同時(shí)配置過(guò)濾多個(gè)包的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06

最新評(píng)論