一、加载自定义配置文件:

1、新建一个family.yam文件,将上application.yml对象复制进入family

family:
family-name:
dad:
name: levi
age: 30 #${random.int} 随机数的值是不能传递的
mom:
alias:
- yilisha
- alise
age: ${family.dad.age} #妈妈的年龄和爸爸相同,没有则默认为24岁
child:
name: happlyboy
age: 5
friends:
- {hobby: baseball,sex: male}
- {hobby: football,sex: famale}

2、自定义一个配置类:

package com.liyu.helloworld.config;

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.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource; import java.io.IOException;
import java.util.Properties; public class MixPropertySourceFactory extends DefaultPropertySourceFactory { public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
String sourceName = name != null ? name : resource.getResource().getFilename();
if (!resource.getResource().exists()) {
return new PropertiesPropertySource(sourceName, new Properties());
} else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
Properties propertiesFromYaml = loadYml(resource);
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
} else {
return super.createPropertySource(name, resource);
}
} private Properties loadYml(EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
}
}

该配置了除了可以引入springboot默认的application.properties文件,还能引入自定义的yml文件

@PropertySource(value = {"classpath:family.yml"}, factory = MixPropertySourceFactory.class)
public class Family {

在family类上加入上述注解,如果是读取properties配置文件,只需要加@PropertySource(value = {"classpath:family.properties"})即可。不需要定义MixPropertySourceFactory。

application配置文件的优先级是比普通的yml配置文件优先级是要高的,相同属性的配置,只有字application中没有配置才能生效

二、老的文件配置引入(xml文件)

1、自定义一个xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="testBeanService" class="com.liyu.helloworld.service.TestBeanService"></bean>
</beans>

2、在springboot启动时配置装载xml文件

@SpringBootApplication
@ImportResource(locations = {"classpath:beans.xml"})
public class Boot01HelloworldApplication { public static void main(String[] args) {
SpringApplication.run(Boot01HelloworldApplication.class, args);
} }

3、新建一个测试类:

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestBean { @Autowired
private ConfigurableApplicationContext ioc; @Test
public void testHelloService() {
//测试Spring上下文环境中是否有testBeanService这样一个bean,有的话表示xml配置文件生效
boolean testBeanService= ioc.containsBean("testBeanService");
System.out.println(testBeanService);
}
}

4、运行结果:

spring中注入了目标bean

springboot2.0入门(七)-- 自定义配置文件+xml配置文件引入的更多相关文章

  1. springBoot2.0+redis+fastJson+自定义注解实现方法上添加过期时间

    springBoot2.0集成redis实例 一.首先引入项目依赖的maven jar包,主要包括 spring-boot-starter-data-redis包,这个再springBoot2.0之前 ...

  2. springboot2.0 JPA配置自定义repository,并作为基类BaseRepository使用

    springboot2.0 JPA配置自定义repository,并作为基类BaseRepository使用 原文链接:https://www.cnblogs.com/blog5277/p/10661 ...

  3. Spring框架[一]——spring概念和ioc入门(ioc操作xml配置文件)

    Spring概念 spring是开源的轻量级框架(即不需要依赖其他东西,可用直接使用) spring核心主要两部分 aop:面向切面编程,扩展功能不是修改源代码来实现: ioc:控制反转,比如:有一个 ...

  4. Spring3.0 入门进阶(三):基于XML方式的AOP使用

    AOP是一个比较通用的概念,主要关注的内容用一句话来说就是"如何使用一个对象代理另外一个对象",不同的框架会有不同的实现,Aspectj 是在编译期就绑定了代理对象与被代理对象的关 ...

  5. springboot2.0入门(九)-- springboot使用mybatis-generator自动代码生成

    一.配置文件引入 插件引入,引入 <plugin> <groupId>org.mybatis.generator</groupId> <artifactId& ...

  6. springboot2.0入门(一)----springboot 简介

    一.springboot解决了什么? 避免了繁杂的xml配置,框架自动帮我们完成了相关的配置,当我们需要进行相关插件集成的时候,只需要将相关的starter通过相关的maven依赖引进,并可以进行相关 ...

  7. Springboot2.0入门介绍

    Springboot目前已经得到了很广泛的应用,why这么牛逼? Springboot让你更容易上手,简单快捷的构建Spring的应用 Spring Boot让我们的Spring应用变的更轻量化.比如 ...

  8. springboot2.0入门(二)-- 基础项目构建+插件的使用

    一.idea中新建第一个HelloWorld项目 点击next: 下一步 在这里可以选择我们需要依赖的第三方软件类库,包括spring-boot-web,mysql驱动,mybatis等.我们这里暂时 ...

  9. Maven之(七)pom.xml配置文件详解

    setting.xml主要用于配置maven的运行环境等一系列通用的属性,是全局级别的配置文件:而pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和li ...

随机推荐

  1. Oracle常用指令

    /**  为了清晰化的显示:所有固定命令都是用大写格式显示 SQL语法分类:DML,DDL,DCL (1)DML(Data Manipulation Language ,数据库操作语言): 数据:增加 ...

  2. MyEclipse开发第一个java程序HelloWorld

    [学习笔记] 用MyEclipse开发第一个java程序: 我们先看看一个具体例子,给你们有个先入为主的感觉. 步骤一:在Eclipse开发工具中我们New一个java项目, 如图2_1 图2_1 步 ...

  3. c++ string类型成员变量在调用构造函数后未能正确赋值

    struct RelItem{ string segName; Elf32_Rel* rel; string relName; RelItem(string seg, int addr, string ...

  4. ubuntu系统里常用的几个命令

    ### ubuntu系统里常用的几个命令 卸载软件: sudo apt-get --purge remove easy-rsa //最后是包名, --purge是可选参数,加上的话移除配置文件 删除文 ...

  5. go slice切片注意跟数组的区别

    一个 slice 会指向一个序列的值,并且包含了长度信息. []T 是一个元素类型为 T 的 slice. [2]string 这样定义久是字符数组 []string 这样定义就是切片 表面上看切片就 ...

  6. go 结构体2 文法

    结构体文法表示通过结构体字段的值作为列表来新分配一个结构体. 使用 Name: 语法可以仅列出部分字段.(字段名的顺序无关.) 特殊的前缀 & 返回一个指向结构体的指针. //分配的v1结构体 ...

  7. eventFlow 系列 <三> 查询所有

    接着上面的例子,产生2条数据.怎么把这两条数据查询出来呢? var commandBus = resolver.Resolve<ICommandBus>(); , ); var execu ...

  8. 前端关于 superSlide.js 使用,一款基于jquery的前端控件

    1引用jQuery.js 和 jquery.SuperSlide.js 2 编写HTML ** 以下是默认的HTMl结构,分别是 ".hd" 里面包含ul, ".bd&q ...

  9. Asp.Net Core 轻松学系列-1阅读指引目录

    https://www.cnblogs.com/viter/p/10474091.html 目录 前言 1. 从安装到配置 2. 业务实现 3. 日志 4. 测试 5. 缓存使用 6.网络和通讯 7. ...

  10. Vue2.0+elementUI使用echarts插件

    1.npm安装echarts:     $ npm install echarts -S 2.html代码: <template> <div id="chartColumn ...