springboot如何獲取yml里面的屬性值
如何獲取yml里面的屬性值
開發(fā)環(huán)境
- idea
- jdk1.8
項目結(jié)構(gòu)

pom依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot</groupId>
<artifactId>springboot_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--導(dǎo)入配置文件處理器,配置文件綁定數(shù)據(jù)會有數(shù)據(jù)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>springboot啟動類
package com.springboot.springboot_demo;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
@SpringBootApplication
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
/**
* 設(shè)置自定義yml位置
*
* @return
*/
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(
new ClassPathResource[] {
/** 請求url地址 */
new ClassPathResource("config/person.yml")
});
pspc.setProperties(yaml.getObject());
return pspc;
}
}person.yml
person:
lastName: zhangsan
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: v2}
lists:
- lisi
- zhaoliu
dog:
name: 小狗
age: 2person.java
package com.springboot.springboot_demo.pojo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 將配置文件中配置的每一個屬性的值映射到person
* @ConfigurationProperties 告訴springboot 將本類中的值與配置文件中的值綁定
* prefix = "person" 配置文件下的那個屬性下的
* 只有這個組件是容器中的組件才能使用@Component
*/
@Data
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
}Dog.java
package com.springboot.springboot_demo.pojo;
import lombok.Data;
@Data
public class Dog {
private String name;
private String age;
}SpringbootDemoApplicationTests
package com.springboot.springboot_demo;
import com.springboot.springboot_demo.pojo.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* springboot單元測試
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootDemoApplicationTests {
@Autowired
private Person person;
@Value("${person.lastName}")
private String username;
@Test
public void contextLoads() {
System.out.println(person);
}
@Test
public void personCopy() {
System.out.println(username);
}
}控制臺輸出
Person(lastName=zhangsan, age=18, boss=false, birth=Tue Dec 12 00:00:00 CST 2017, maps={k1=v1, k2=v2}, lists=[lisi, zhaoliu], dog=Dog(name=小狗, age=2))
zhangsan
獲取.yml中自定義參數(shù)
需求:根據(jù)不同環(huán)境獲取到不同的參數(shù),放在.yml中方便修改!

在開發(fā)環(huán)境中,回調(diào)的url地址

在測試環(huán)境中,回調(diào)的url地址

在生產(chǎn)環(huán)境中,回調(diào)的url地址

我是通過2種方式來實現(xiàn)的
通過@Value獲取
代碼如下
package com.heque.service.pay;
import org.springframework.beans.factory.annotation.Value;
public abstract class ObtainNotifyUrl {
?? ?@Value("${weixinAndAli.wechatNotifyUrl}")
?? ?private String wechatNotifyUrl;
?? ?@Value("${weixinAndAli.aliNotifyUrl}")
?? ?private String aliNotifyUrl;
?? ?public String getWechatNotifyUrl() {
?? ??? ?return wechatNotifyUrl;
?? ?}
?? ?public void setWechatNotifyUrl(String wechatNotifyUrl) {
?? ??? ?this.wechatNotifyUrl = wechatNotifyUrl;
?? ?}
?? ?public String getAliNotifyUrl() {
?? ??? ?return aliNotifyUrl;
?? ?}
?? ?public void setAliNotifyUrl(String aliNotifyUrl) {
?? ??? ?this.aliNotifyUrl = aliNotifyUrl;
?? ?}
}package com.heque.service.pay;
import org.springframework.stereotype.Component;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = false)
@Component
public class Test extends ObtainNotifyUrl{?? ?
}通過springframework自動包可以完成此功能,需要注意Test繼承對象需要加入@component,把對象交給springContainer去管理,我們需要的時候只需注入資源就好了。
@Autowired?private Test test;?test.getAliNotifyUrl();/test.getWechatNotifyUrl();
通過@ConfigurationProperties(prefix = "weixinAndAli")注解
代碼如下
package com.heque.service.pay;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
?* @author: TimBrian
?* @date: Sep 10, 2018 9:13:40 AM ? ? ?
?*/
@Component
@ConfigurationProperties(prefix = "weixinAndAli")
public class ConfigUtils {?? ?
?? ?private String wechatNotifyUrl;?? ?
?? ?private String aliNotifyUrl;?? ?
?? ?public String getWechatNotifyUrl() {
?? ??? ?return this.wechatNotifyUrl;
?? ?}
?? ?public void setWechatNotifyUrl(String wechatNotifyUrl) {
?? ??? ?this.wechatNotifyUrl = wechatNotifyUrl;
?? ?}
?? ?public String getAliNotifyUrl() {
?? ??? ?return aliNotifyUrl;
?? ?}
?? ?public void setAliNotifyUrl(String aliNotifyUrl) {
?? ??? ?this.aliNotifyUrl = aliNotifyUrl;
?? ?}
}調(diào)用和方法一一樣的
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java如何解決發(fā)送Post請求報Stream?closed問題
這篇文章主要介紹了Java如何解決發(fā)送Post請求報Stream?closed問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
基于Java將Excel科學(xué)計數(shù)法解析成數(shù)字
這篇文章主要介紹了基于Java將Excel科學(xué)計數(shù)法解析成數(shù)字,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09

