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

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. 数位dp踩坑

    前言 数位DP是什么?以前总觉得这个概念很高大上,最近闲的没事,学了一下发现确实挺神奇的. 从一道简单题说起 hdu 2089 "不要62" 一个数字,如果包含'4'或者'62', ...

  2. python多任务基础

    1.多任务:两个程序段同时运行2.为某个函数创建线程并启动: import threading 线程名 = threading.Thread(target = 函数名,args = 参数元组) #创建 ...

  3. chrome中显示DNS_PROBE_FINISHED_NO_INTERNET无法上网,但是IE可以上

    以管理员方式运行cmd,执行如下命令 ipconfig /release ipconfig /all ipconfig /flushdns ipconfig /renew netsh int ip s ...

  4. Python使用datetime来判断近七天

    目录 strptime 使用strptime来格式化字符串 datetime.datetime.strptime("2019-10-02", "%Y-%m-%d" ...

  5. AtCoder Grand Contest 040 A - ><

    传送门 对于某个位置,只要知道这个位置往左最多的连续 $\text{<}$ 的数量 $x$ 和往右最多的连续 $\text{>}$ 的数量 $y$ 那么这个位置最小可能的数即为 $max( ...

  6. redis字符串类型的基本命令

    1.redis字符串类型键的设置 命令名称:SET 语法:set key value [EX seconds] [PX milliseconds] [NX|XX] 功能:给一个key添加字符串类型的值 ...

  7. [Vue]避免 v-if 和 v-for 用在同一个元素上

    一般我们在两种常见的情况下会倾向于这样做: 情形1:为了过滤一个列表中的项目 (比如 v-for="user in users" v-if="user.isActive& ...

  8. Spring 后台方法 重定向 与 转发

    一.重定向:重定向是客户端行为,在使用时,务必使用全路径,否则可能因为外部环境导致错误 1.URL改变为重定向的URL地址 2.前台页面不能使用Ajax请求提交, 应该使用form表单提交 方法一.参 ...

  9. 在sublime3中运行python文件

    1.首先下载Sublime和Python,安装Python环境 注意:如果不想动手亲自配置Python环境安装的时候环境变量,请在安装的界面给Add Python 3.5 To Path前面打上对号. ...

  10. Bminer

    Bminer https://www.bminer.me/zh/ Bminer: When Crypto-mining Made Fast¶ Bminer是一款为NVIDIA和AMD GPU深度优化的 ...