springboot如何获取yml里面的属性值

网友投稿 284 2022-08-28


springboot如何获取yml里面的属性值

目录如何获取yml里面的属性值开发环境项目结构pom依赖springboot启动类person.ymlperson.javaDog.javaSpringbootDemoApplicationTests控制台输出获取.yml中自定义参数通过@Value获取通过@ConfigurationProperties(prefix = "weixinAndAli")注解

如何获取yml里面的属性值

开发环境

ideajdk1.8

项目结构

pom依赖

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.8.RELEASE

com.springboot

springboot_demo

0.0.1-SNAPSHOT

springboot_demo

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-configuration-processor

true

org.springframework.boot

spring-boot-starter-test

test

<groupId>org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.8.RELEASE

com.springboot

springboot_demo

0.0.1-SNAPSHOT

springboot_demo

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-configuration-processor

true

org.springframework.boot

spring-boot-starter-test

test

<groupId>org.springframework.boot

spring-boot-maven-plugin

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

}

/**

* 设置自定义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: 2

person.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 maps;

private List 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中自定义参数

需求:根据不同环境获取到不同的参数,放在.yml中http://方便修改!

在开发环境中,回调的url地址

在测试环境中,回调的url地址

在生产环境中,回调的url地址

我是通过2种方式来实现的

通过@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继承wXayAY对象需要加入@component,把对象交给springContainer去管理,我们需要的时候只需注入资源就好了。

@Autowiredprivate TeswXayAYt 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;

}

}

调用和方法一一样的

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python基础语法(python基础语法及知识总结)
下一篇:Flask模板与自定义过滤器
相关文章

 发表评论

暂时没有评论,来抢沙发吧~