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 ...
随机推荐
- 数据库事务隔离级别 - 分析脏读 & 不可重复读 & 幻读
一 数据库事务的隔离级别 数据库事务的隔离级别有4个,由低到高依次为Read uncommitted .Read committed .Repeatable read .Serializable ,这 ...
- json字符串,json对象,java对象互相转换
1.把JSON字符串转换为JAVA 对象 JSONObject jsonobject = JSONObject.fromObject(jsonStr); User user= (User)JSONOb ...
- adb 安装 app/apk链接不上设备和安装出现failed_install_user_restricted的解决方法
1.手机链接电脑,保持网段一致,通过ping 看是否可以ping通 2.如果可以ping通,查看telnet ip 5555 看是否可以连接 3.如果无法连接查看手机是否开启开发者模式中的debug模 ...
- MySQL 军规
MySQL 基础篇 三范式 MySQL 军规 MySQL 配置 MySQL 用户管理和权限设置 MySQL 常用函数介绍 MySQL 字段类型介绍 MySQL 多列排序 MySQL 行转列 列转行 M ...
- vs2019 扩展工具
这里只是做个记录,没啥技术含量 本人代码上有些强迫症,所以我的本地代码一定不可以丢,之前用vs2013开始,就安装了localhistory这个插件,十分方便,觉得不用了,清了即可,也不占地方. 但是 ...
- (十二)SpringBoot之Spring-Data-Jpa(一)
一.Spring-Data-Jpa概念 JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate.TopLink等. Spring D ...
- 解决 Linux grep 不高亮显示
今天偶然发现一个问题,在 grep 日志的过程中,搜出来一大坨但是被 grep 的那一段未高亮显示,属实有些难受,高亮显示是 Linux 的高亮本来就是 Linux 的功能,与连接工具(我用的 xsh ...
- 使用ef core自动生成mysql表和数据编码的问题
mysql默认的编码是不支持中文的,需要改成utf8编码格式. 而我使用的Pomelo.EntityFrameworkCore.MySql组件生成mysql库和表,他是使用默认编码的. 网上大多说修改 ...
- leetcode-3 最长无重复字串
3. Longest Substring Without Repeating Characters 题面 Given a string, find the length of the longest ...
- Java面试容器,collection,list,set
1.容器指的是可以容纳其他对象的对象. 2.collection/set/list的联系和区别? (1)collection是Java集合顶级接口,存储一组不唯一,无序的对象: (2)list接口 ...