栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring:Yml配置文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring:Yml配置文件

 

Properties配置存在一些问题:存在配置繁琐,无法表达数据的联系

无法表达对象

XML:表达数据比较准确,可以表达对象概念

在pom.xml中引入依赖:

 

 配置文件:init.yml

 

name: suns
password: 123456

 创建配置Bean:YmlAutoConfiguration:ProprttySourcePlaceholderConfigure这个类,用来读取配置文件

package com.baizhiedu.yml;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;

import java.util.Properties;

@Configuration
@ComponentScan(basePackages = "com.baizhiedu.yml")
public class YmlAutoConfiguration {

    //创建对象,读取yml,获取properties集合
    @Bean
    public PropertySourcesPlaceholderConfigurer configurer(){
        YamlPropertiesFactoryBean yamlPropertiesFactoryBean=new YamlPropertiesFactoryBean();
        yamlPropertiesFactoryBean.setResources(new ClassPathResource("init.yml"));
        //获取到properties集合
        Properties properties=yamlPropertiesFactoryBean.getObject();

        PropertySourcesPlaceholderConfigurer configurer=new PropertySourcesPlaceholderConfigurer();
        configurer.setProperties(properties);

        return configurer;
    }
}

Account:

package com.baizhiedu.yml;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Account {
    @Value("${name}")
    private String name;
    @Value("${password}")
    private String password;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

TestYml:

package com.baizhiedu;

import com.baizhiedu.yml.Account;
import com.baizhiedu.yml.YmlAutoConfiguration;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestYml {
    @Test
    public void test1(){
        ApplicationContext ctx=new AnnotationConfigApplicationContext(YmlAutoConfiguration.class);
        Account account=(Account)ctx.getBean("account");
        System.out.println("account.getName()="+account.getName());
        System.out.println("account.getPassword()="+account.getPassword());
    }
}

 init.yml:中使用对象的方式:

#name: suns
#password: 123456
account:
  name: xiaohei
  password: 123

Account:进行修改:

package com.baizhiedu.yml;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Account {
    @Value("${account.name}")
    private String name;
    @Value("${account.password}")
    private String password;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

测试结果:

 

 

Yml对 List类型的注入:

#name: suns
#password: 123456
account:
  name: xiaohei
  password: 123
list:
  - 111
  - 222

Account:

package com.baizhiedu.yml;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class Account {
    @Value("${account.name}")
    private String name;
    @Value("${account.password}")
    private String password;
    @Value("${list}")
    private List list;

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

TestYml:

 

package com.baizhiedu;

import com.baizhiedu.yml.Account;
import com.baizhiedu.yml.YmlAutoConfiguration;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.List;

public class TestYml {
    @Test
    public void test1(){
        ApplicationContext ctx=new AnnotationConfigApplicationContext(YmlAutoConfiguration.class);
        Account account=(Account)ctx.getBean("account");
        System.out.println("account.getName()="+account.getName());
        System.out.println("account.getPassword()="+account.getPassword());

        List list=account.getList();
        for (String s : list) {
            System.out.println("s="+s);
        }
    }
}

运行出现错误:是因为YmlPropertiesFactoryBean不能解析List类型的数据,会报错,可以通过SpringEL表达式进行解决

 init.yml:

#name: suns
#password: 123456
account:
  name: xiaohei
  password: 123
  
list: 111,222

Account:

package com.baizhiedu.yml;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class Account {
    @Value("${account.name}")
    private String name;
    @Value("${account.password}")
    private String password;
    //@Value("${list}") 运用springEL进行赋值
    @Value("#{'${list}'.split(',')}") //拿到list数据,再以,进行分隔
    private List list;

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

 完结!!!!!

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/943587.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号