spring自定义yml文件解析器
spring自定义yml文件解析器
- pom 引入依赖
- yml 文件
- 自定义yml文件解析的工厂 YmlPropertySourceFactory
- JdbcConfig 配置类
- spring 启动类
- 测试
- 结果
srping 配置自定yml解析器
以最简单的获取数据源的代码示例
整体文件结构如下:
pom 引入依赖
<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.6.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.1.6.RELEASE</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.22</version></dependency><!--导入yaml文件解析器坐标--><dependency><groupId>org.yaml</groupId><artifactId>snakeyaml</artifactId><version>1.23</version></dependency></dependencies>
yml 文件
jdbc:driver: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://192.168.33.128:33307/mysqlusername: rootpassword: 123456
自定义yml文件解析的工厂 YmlPropertySourceFactory
package base.support;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.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;import java.io.IOException;
import java.util.Properties;
/*** 自定义yml文件解析的工厂*/
public class YmlPropertySourceFactory implements PropertySourceFactory {@Overridepublic 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();//4.返回spring提供的PropertySource对象return (name != null ? new PropertiesPropertySource(name,properties): new PropertiesPropertySource(resource.getResource().getFilename(),properties));}
}
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的配置类*/
public class JdbcConfig {@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(name="dataSource")public DataSource createDataSource(){System.out.println("获取配置信息: driver : "+ driver + ",url :"+ url + ",username : "+ username + " ,password:"+password );//1.创建Spring内置数据源DriverManagerDataSource dataSource = new DriverManagerDataSource();//2.给数据源填充属性dataSource.setDriverClassName(driver);dataSource.setUrl(url);dataSource.setUsername(username);dataSource.setPassword(password);//3.返回return dataSource;}
}
spring 启动类
package config;import base.support.YmlPropertySourceFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;/*** spring的配置类*/
@Configuration
@Import(JdbcConfig.class)
//@PropertySource("classpath:jdbc.xml")
//@PropertySource("classpath:jdbc.properties")
// factory 指定yml文件解析工厂
@PropertySource(value = "classpath:jdbc.yml",factory = YmlPropertySourceFactory.class)
public class SpringConfiguration {}
测试
package com.itheima.test;import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import javax.sql.DataSource;
import java.sql.Connection;public class SpringPropertySourceTest{public static void main(String[] args) throws Exception{//1.创建容器AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext("config");//2.获取对象DataSource dataSource = ac.getBean("dataSource",DataSource.class);//3.获取连接Connection connection = dataSource.getConnection();System.out.println(connection.getMetaData());connection.close();}
}
结果
获取配置信息: driver : com.mysql.cj.jdbc.Driver,url :jdbc:mysql://192.168.33.128:33307/mysql,username : root ,password:123456
com.mysql.cj.jdbc.DatabaseMetaDataUsingInfoSchema@17d919b6Process finished with exit code 0
可以看到已经正确拿到yml配置文件的值,也获取到了数据连接
spring自定义yml文件解析器相关推荐
- spring MVC使用自定义的参数解析器解析参数
目录 写在前面 编写自定义的参数解析器解析请求参数 项目结构 定义注解 实体类 controller 定义参数解析器 注册参数解析器 启动项目 发起请求查看结果 写在前面 如果还有小伙伴不知道spri ...
- SpringBoot--网上商城项目(自定义的参数解析器、购物车后台前台功能、商品详情页)
目录 一.自定义的参数解析器 关于Mybatis-plus时间字段代码生成问题 报错信息:Caused by: java.lang.IllegalStateException: No typehand ...
- Spring自定义命名空间的解析原理与实现
Spring自定义命名空间的解析原理与实现 原理 由上篇文章refresh() -> obtainFreshBeanFactory()跟踪源码可知Spring在解析除默认命名空间import.a ...
- spring多个视图解析器_在Spring中配置多个View解析器
spring多个视图解析器 1.简介 在Spring中,提供了View Resolver来使用模型中可用的数据来解析视图,而无需与JSP,Velocity或Thymeleaf等View技术紧密绑定. ...
- Restful 风格开发 Spring MVC 的视图解析器---使用 beetl 模板引擎
一.restful 风格 restful 的目的 将用户的行为当成是对数据库中记录的操作: 增加用户:/user post(post方式) 删除用户:/user/2 delete(删除第2条记录) 修 ...
- [翻译]运用文件解析器在任意文件中使用虚拟应用路径(~)
原文出处:http://www.codeproject.com Using the FileResolver to allow virtual application paths ( ~ ) i ...
- 使用springMVC提供的CommonsMultipartResolver文件解析器,实现文件轻松上传
springMVC提供的前端控制器,可以拦截所有请求,指挥调度所有后台逻辑资源. 使用传统方式进行文件上传,需要我们手动解析request对象,获取文件上传项,再进行文件的上传. springMVC框 ...
- Spring boot yml文件的书写格式
Spring boot yml文件的书写格式 使用ide 创建好spring boot文件格式后https://blog.csdn.net/weixin_42292697/article/detail ...
- 自定义字体文件解析成人眼可识别文字
# coding=utf-8 from fontTools.ttLib import TTFont from PIL import Image, ImageDraw, ImageFont #绘制图片 ...
- Glib学习(17) Key-value文件解析器 Key-value file parser
glib源码下载:http://ftp.gnome.org/pub/gnome/sources/glib/ glib帮助文档:https://developer.gnome.org/glib/ 本节主 ...
最新文章
- ubuntu dpkg initramfs-tools错误的解决方法
- windows环境下 curl 安装和使用
- mysql特有语法_MySQL详细的基础语法
- Ubuntu 16.04上安装SkyEye及测试
- redis 设置不过期_面试时 Redis 内存淘汰总被问,但是总答不好,怎么解决?
- ExtJS2.0实用简明教程——可编辑表格EditorGridPanel
- PUMA 560机器人正运动学
- HTML5游戏 看你有多“色” 开发
- 随笔--读书笔记《软技能:代码之外的生存指南》
- GD32F3x0 官方PWM驱动正频宽偏小(定时不准)的问题
- 立法白噪声的时间序列检验Matlab
- 05、Python中转义字符与字符串
- 应用在电视触摸屏中的十四通道智能触摸芯片
- kafka启动异常InconsistentClusterIdException
- 信通院牵头数列科技参与主编的《信息系统稳定性保障能力建设指南》正式发布
- bl wn810a linux驱动下载,bl wn810a驱动下载-BL-WN810A驱动 1.0 官方版 - 河东下载站
- 网易有道自研神经网络翻译上线 质量提升超过去十年总和
- 使用采集工具,轻松获取目标受众的数据,让您的市场营销更加精准
- 一物一码溯源防伪系统
- 完美解决Could not find a version that satisfies the requirement xxx (from versions: )
热门文章
- 系统之家 linux下载,迅雷Linux版下载_迅雷Linux版官方版1.0.0.1 - 系统之家
- 在ubuntu9.10下 安装nvidia GT130M最新驱动190.42版本
- 2.3.1 TextView(文本框)详解
- 网络招聘信息的分析与挖掘
- 鼎立td测试软件窗口参数介绍,鼎立网优参数指标解释
- html图片边框显示不全,css border边框显示不完全
- H264 编解码协议详解
- 彻底了解Windows XP操作系统登录类型
- 常见电容器图片_常用电容器的标识方法(图)
- 中兴捧月——婚姻匹配问题