springboot的版本:v2.2.1.RELEASE

因为springboot在项目启动的时候是不会自动将自定义(名字不是application*.yml)的配置文件加载到spring容器的,而使用@PropertySource(value = "classpath:test.yml")这样的方式是不能加载.yml文件的。所以我们需要使用另外的方式。

1.将yaml文件加载到spring容器

在springboot启动类(Application.java)中将自定义的yaml文件加载到spring容器

package com.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class YamlApplication {
    public static void main(String[] args) {
        SpringApplication.run(YamlApplication .class, args);
    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer configurer = new         
        PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
	    yaml.setResources(new ClassPathResource("test1.yml"),new ClassPathResource("test2.yml"));
        configurer.setProperties(yaml.getObject());
        return configurer;

这样就将自定义的test1.yml和test2.yml加载到了spring容器中。

2.yaml文件实例

具体的yaml文件的用法请参考:https://www.ruanyifeng.com/blog/2016/07/yaml.html

test1.yml

#test1 config
test1:
  test1List:
    - testId: admin
      testName: admin
    - testId: User
      testName: User

test2.yml

#test2 config
test2:
  test2List:
    - testId: User
      apis:
        api1: [get,post]
    - testId: admin
      apis:
        api1: [get,post]
        api2: [get,post]
        api3: [get,put]

3.将yml文件的内容注入到实体Bean

在定义了yaml文件后,我们需要定义与之数据结构相对应的实体bean(实在不知道可以先定义为Object然后调试看映射的数据皆结构,前提是定义的yaml文件的结构是正确的)。

将test1.yml注入到Test1Bean中

package com.springboot.common.beans;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Data
@Component
@ConfigurationProperties(prefix = "test1") //指定首节点的名字然后自动匹配注入
public class Test1Bean {
    //test1.yml中test1为第一层节点下面是test1List
    private List<Test1IdBean> test1List;

Test1IdBean结构

package com.springboot.common.beans;
import lombok.Data;
@Data
public class Test1IdBean {
    private String testId;
    private String testName;

将test2.yml注入到Test2Bean中

package com.springboot.common.beans;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Data
@Component
@ConfigurationProperties(prefix = "test2")
public class Test2Bean {
    private List<Test2ApiBean> test2List;

Test2ApiBean结构

package com.springboot.common.beans;
import lombok.Data;
import java.util.List;
import java.util.Map;
@Data
public class Test2ApiBean {
    private String testId;
    private Map<String, List<String>> apis;

至此已经大功告成,在springboot项目启动的收就会成功的将我们自定义yaml文件加载到spring容器,并自动注入我们定义的实体Bean中去。

@Log4j2
@Component
public class SchemaYmlTest {
    @Autowired
    private Test1Bean test1Bean;
    @Autowired
    private Test2Bean test2Bean;
    public void testYml() {
        test1Bean.getTest1List().forEach(a -> System.out.println(a));
        test2Bean.getTest2List().forEach(a -> {System.out.printLn(a.getTestId)})
                    springboot的版本:v2.2.1.RELEASE因为springboot在项目启动的时候是不会自动将自定义(名字不是application*.yml)的配置文件加载到spring容器的,而使用@PropertySource(value = "classpath:test.yml")这样的方式是不能加载.yml文件的。所以我们需要使用另外的方式。1.将yaml文件加载到spring容...
				
YAML介绍 YAML是”YAML Ain’t markup language"(YAML不是一种标记语言)的缩写,是一种对人类设计友好(方便读写)的数据序列化语言,可以很好地与其它编程语言协同完成日常任务。 它是JSON的一个严格超集,在语法上增加了类似Python的换行和缩进。不过,与Python不同,YAML不允许使用Tab缩进。 YAML有一些基本的规则,用来避免与各种编程语言和编辑器相关的歧义问题,这些基本的规则使得无论哪个应用程序或软件库都能一致地解析YAML。 1.文件名以
支持多种语言:python、js、golang、java、c、c++ YAML 语言(发音 /ˈjæməl/ )的设计目标,就是方便人类读写。它实质上是一种通用的数据串行化格式。 它的基本语法规则如下。 大小写敏感 使用缩进表示层级关系 缩进时不允许使用Tab键,只允许使用空格。 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可 # 表示注释,从这个字...
如何在Spring Boot中从YAML文件注入到Map。 首先,将对Spring框架中的YAML文件有一些了解。 然后,通过一个实际示例展示如何将YAML属性绑定到Map。 2.Spring框架中的YAML文件 使用YAML文件存储外部配置数据是一种常见的做法。 基本上,Spring支持使用YAML文档作为属性的替代方法,并在后台使用SnakeYAML对其进行解析。 看看典型的YAML文件是什么样的: server: port: 8090 application: name: m **配置文件的作用 :**修改SpringBoot自动配置的默认值,因为SpringBoot在底层都给我们自动配置好了; 比如我们可以在配置文件中修改Tomcat 默认启动的端口号!测试一下! server.port=80 yaml格式现在已经很通用了,yaml有简介已读的格式,在容器环境或者微服务被大量使用,只是使用的过程还是properties的key的方式存储使用,可能跟Spring的发展历程有关,Spring使用PropertySource存储配置数据,而原生的yaml使用还有很多不便利的地方。 1. yaml yml properties 实际上yaml yml都是一种格式,Spring仅定义了Yaml的类,properties是HashTable包装。 在Spring的存储中是用PropertySou
如何使用SnakeYAML来读取YAMLYML为其简写)配置文件; 读取后,装载成Map,而Map如何存储的配置文件的数据。 Snake POM引入 <dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.27</version> </dependency> Timmer丿: <artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-configuration-processor</artifactId> <artifactId>gremlin-core</artifactId> <artifactId>gremlin-driver</artifactId> <artifactId>neo4j-gremlin</artifactId> <artifactId>neo4j-tinkerpop-api-impl</artifactId> <artifactId>fastjson</artifactId> <artifactId>lombok</artifactId> Gremlin+neo4j+完整实例 qq_25958999: org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jGraph这个类,来自哪个依赖呢 Gremlin+neo4j+完整实例 qq_25958999: 依赖哪些jar包,博主能发一下么 springboot集成shiro+jwt详解+完整实例 zhaolyoing: 问一下这个JWT在哪定义得