自定义Yaml解析器替换Properties文件
自定义Yaml解析器替换Properties文件
项目结构

案例代码
配置类SpringConfiguration
@Configuration
@Import(JdbcCofnig.class)
@PropertySource(value = "classpath:/jdbc.yml",factory = YamlPropertySourceFactory.class)
public class SpringConfiguration {
}
JdbcConfig
package config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
/**
* JDBC配置类
* @Author Helius
* @Create 2019-11-01-19:47
*/
public class JdbcCofnig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean("datasource")
public DataSource createDataSource() {
System.out.println("驱动类是:" + driver);
//1.创建Spring内置数据源
DriverManagerDataSource dataSource = new DriverManagerDataSource();
//2.给数据源填充属性
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
# Yet Another Markup Language 另一种标记语言
# YAML yml
#键和值之间用冒号和空格分隔
jdbc:
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/spring_ioc
username: root
password: admin
解释:
对于properties文件的解析,
spring4.3以后,我们无须在配置
<!-- 数据库配置文件位置 --> <context:property-placeholder location="classpath:jdbc.properties" />
或者
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>jdbc.properties</value>
</property>
<property name="fileEncoding">
<value>UTF-8</value>
</property>
</bean>
默认使用的是
默认使用PropertySourceFactory接口的唯一实现类DefaultPropertySourceFactory来解析properties文件,参见@PropertySource注解
该类也支持xml,但并不支持yml格式。
yml格式是springboot所推荐的配置文件格式。
那如果使用yml格式就需要我们自定义解析器.
- 首先需要入第三方yml解析器
<!--导入yaml文件解析器坐标-->
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.23</version>
</dependency>
- 然后自定义类型解析器
@PropertySource(value = "classpath:/jdbc.yml",factory = YamlPropertySourceFactory.class)
通过配置类的@PropertySource注解的factory属性
- 解析类YamlPropertySourceFactory
/**
* 自定义解析yaml文件的工厂类
*引入yaml文件解析器
* @Author Helius
* @Create 2019-11-01-21:51
*/
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
//1.创建yaml文件解析工厂
YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();
//2.设置要解析的资源内容
factoryBean.setResources(resource.getResource());
//3.把资源解析成解析成properties文件
Properties properties = factoryBean.getObject();
//返回PropertySource对象
return (name != null ? new PropertiesPropertySource(name, properties) : new PropertiesPropertySource(resource.getResource().getFilename(), properties));
}
}
- 测试类
/**
* 测试类
* @Author Helius
* @Create 2019-11-01-19:54
*/
public class SpringPropertySourceTest {
public static void main(String[] args) throws SQLException {
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);
DataSource datasource = ac.getBean("datasource", DataSource.class);
Connection connection = datasource.getConnection();
connection.close();
}
}
控制台输出:
驱动类是:com.mysql.jdbc.Driver
自定义Yaml解析器替换Properties文件的更多相关文章
- SpringBoot系列教程web篇之如何自定义参数解析器
title: 190831-SpringBoot系列教程web篇之如何自定义参数解析器 banner: /spring-blog/imgs/190831/logo.jpg tags: 请求参数 cat ...
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...
- 使用Pull解析器生成XML文件和读取xml文件
有些时候,我们需要生成一个XML文件,生成XML文件的方法有很多,如:可以只使用一个StringBuilder组拼XML内容,然后把内容写入到文件中:或者使用DOM API生成XML文件,或者也可以使 ...
- SpringMVC自动封装List对象 —— 自定义参数解析器
前台传递的参数为集合对象时,后台Controller希望用一个List集合接收数据. 原生SpringMVC是不支持,Controller参数定义为List类型时,接收参数会报如下错误: org.sp ...
- SpringMVC 自定义参数解析器.
一.简述 有没有想过像 @RequestParam.@RequestBody 这些注解的工作原理呢?为什么 form 表单.application/json 的参数能够直接封装进 Bean 对象中呢? ...
- 使用Pull解析器生成XML文件
有些时候,我们需要生成一个XML文件,生成XML文件的方法有很多,如:可以只使用一个StringBuilder组拼XML内容,然后把内容写入到文件中:或者使用DOM API生成XML文件,或者也可以使 ...
- Spring自定义参数解析器
结合redis编写User自定义参数解析器UserArgumentResolver import javax.servlet.http.Cookie; import javax.servlet.htt ...
- 10.AutoMapper 之自定义值解析器(Custom Value Resolvers)
https://www.jianshu.com/p/3e7cf1d1f17d 自定义值解析器(Custom Value Resolvers) 虽然AutoMapper涵盖了相当多的目标成员映射方案,但 ...
- springmvc 源码分析(三) -- 自定义处理器映射器和自定义处理器适配器,以及自定义参数解析器 和错误跳转自定页面
测试环境搭建: 本次搭建是基于springboot来实现的,代码在码云的链接:https://gitee.com/yangxioahui/thymeleaf.git DispatcherServlet ...
随机推荐
- moodle3.7上传中文文件,无法引用,图片不显示
初始安装moodle3.7 上传图片,名称为中文时,无法引用图片,图片不显示.这里采用修改moodle根目录下的config.php文件, 添加了变量$CFG->slasharguments = ...
- ASP.NET MVC自定义Module记录管道事件执行顺序
1. 在Visual Studio 新建项目,模板为空,下面结构选择MVC. 2. 在项目中新建一个类MyModule,实现IHttpModule接口 namespace SimpleApp.Infr ...
- Python连接MongoDB数据库并执行操作
原文:https://blog.51cto.com/1767340368/2092813 环境设置: [root@mongodb ~]# cat /etc/redhat-release CentOS ...
- 实验吧——你真的会PHP吗?(intval范围 php中\00的利用)
题目地址:http://ctf5.shiyanbar.com/web/PHP/index.php 抓包在header中发现提示 访问得到源码 <?php $info = "" ...
- SAP云平台和第三方CRM解决方案(火锅)互联
光看封面配图,这篇文章很容易被误认为在讲成都的美食之一:火锅. SAP成都研究院坐落在被联合国教科文组织授予过"美食之都"称号的成都,所在的天府软件园,半径1公里左右星罗棋布着很多 ...
- 源码解析 || ArrayList源码解析
前言 这篇文章的ArrayList源码是基于jdk1.8版本的源码,如果与前后版本的实现细节出现不一致的地方请自己多加注意.先上一个它的结构图 ArrayList作为一个集合工具,对于我而言它值得我们 ...
- PAT 乙级 1036.跟奥巴马一起编程 C++/Java
题目来源 美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统.2014 年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏 ...
- IDEA中如何导入一个maven项目并配置相关设置
导入一个maven项目参照如下链接 https://jingyan.baidu.com/article/b0b63dbf0c0ac04a49307078.html 要想启动这个导入的项目目前我所接触到 ...
- nginx上传大文件,413错误解决
在nginx里增加了配置. client_max_body_size 500m; proxy_connect_timeout 600; proxy_read_timeout 600; proxy_se ...
- ZJOI2019赛季回顾
退役了. NOIP2018 day1没什么好说的. day2开考后看完题:这个T3 TM不是DDP吗? 考前刚学过这东西,还没去写过 当时不知道在想什么,胡了T1 60和T2 50分保底之后就去刚T3 ...