springboot2.0入门(七)-- 自定义配置文件+xml配置文件引入
一、加载自定义配置文件:
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配置文件引入的更多相关文章
- springBoot2.0+redis+fastJson+自定义注解实现方法上添加过期时间
springBoot2.0集成redis实例 一.首先引入项目依赖的maven jar包,主要包括 spring-boot-starter-data-redis包,这个再springBoot2.0之前 ...
- springboot2.0 JPA配置自定义repository,并作为基类BaseRepository使用
springboot2.0 JPA配置自定义repository,并作为基类BaseRepository使用 原文链接:https://www.cnblogs.com/blog5277/p/10661 ...
- Spring框架[一]——spring概念和ioc入门(ioc操作xml配置文件)
Spring概念 spring是开源的轻量级框架(即不需要依赖其他东西,可用直接使用) spring核心主要两部分 aop:面向切面编程,扩展功能不是修改源代码来实现: ioc:控制反转,比如:有一个 ...
- Spring3.0 入门进阶(三):基于XML方式的AOP使用
AOP是一个比较通用的概念,主要关注的内容用一句话来说就是"如何使用一个对象代理另外一个对象",不同的框架会有不同的实现,Aspectj 是在编译期就绑定了代理对象与被代理对象的关 ...
- springboot2.0入门(九)-- springboot使用mybatis-generator自动代码生成
一.配置文件引入 插件引入,引入 <plugin> <groupId>org.mybatis.generator</groupId> <artifactId& ...
- springboot2.0入门(一)----springboot 简介
一.springboot解决了什么? 避免了繁杂的xml配置,框架自动帮我们完成了相关的配置,当我们需要进行相关插件集成的时候,只需要将相关的starter通过相关的maven依赖引进,并可以进行相关 ...
- Springboot2.0入门介绍
Springboot目前已经得到了很广泛的应用,why这么牛逼? Springboot让你更容易上手,简单快捷的构建Spring的应用 Spring Boot让我们的Spring应用变的更轻量化.比如 ...
- springboot2.0入门(二)-- 基础项目构建+插件的使用
一.idea中新建第一个HelloWorld项目 点击next: 下一步 在这里可以选择我们需要依赖的第三方软件类库,包括spring-boot-web,mysql驱动,mybatis等.我们这里暂时 ...
- Maven之(七)pom.xml配置文件详解
setting.xml主要用于配置maven的运行环境等一系列通用的属性,是全局级别的配置文件:而pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和li ...
随机推荐
- Ubuntu将自带的python3升级
一.这里演示的是将python3.5升级到python3.6 1.添加安装源,在命令行输入如下命令: sudo add-apt-repository ppa:jonathonf/python-3.6 ...
- 【Polya计数】Buildings II
Buildings II 题目描述 As a traveling salesman in a globalized world, Alan has always moved a lot. He alm ...
- IMPDPORA-27046,dump文件损坏
客户提出导入报错 一.报错如下 SYMPTOMS DataPump Import (IMPDP) fails with the following errors: ORA-: invalid oper ...
- python练习:函数2
习题: 定义一个方法get_num(num),num参数是列表类型,判断列表里面的元素为数字类型.其他类型则报错,并且返回一个偶数列表:(注:列表里面的元素为偶数). def get_num(num) ...
- IdentityServer4同时使用多个GrantType进行授权和IdentityModel.Client部分源码解析
首先,介绍一下问题. 由于项目中用户分了三个角色:管理员.代理.会员.其中,代理又分为一级代理.二级代理等,会员也可以相互之间进行推荐. 将用户表分为了两个,管理员和代理都属于后台,在同一张表,会员单 ...
- Nature Biotechnology:人类基因研究走近平民 数据是基础解读更重要
Nature Biotechnology:人类基因研究走近平民 数据是基础解读更重要 5万美元可以做什么?最近,美国斯坦福大学教授斯蒂芬·夸克在国际著名学术期刊<自然·生物技术>发表论文宣 ...
- koa-router学习笔记
koa-router 是koa框架的一个路由处理级别的中间件. 目录结构 ├── app.js ├── middleware │ ├── m1.js │ └── m2.js ├── package-l ...
- JQuery的事件处理、Jason
事件的处理: <body> <div id="aa" style="width:100px; height:100px; background-colo ...
- 一步一步教你实现iOS音频频谱动画(二)
如果你想先看看最终效果再决定看不看文章 -> bilibili 示例代码下载 第一篇:一步一步教你实现iOS音频频谱动画(一) 本文是系列文章中的第二篇,上篇讲述了音频播放和频谱数据计算,本篇讲 ...
- sql根据一个表查询的数据作为条件查询另一个表
代码格式如下: ) 要注意的是:in后面的查询语句必须是查询一个字段跟前面的表相对应的.比如要根据订单号orderID,OpenBills 这个表就需要查询到orderID这个字段,BillConsu ...