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配置文件的值,也获取到了数据连接