自定义Yaml解析器替换Properties文件
自定义Yaml解析器替换Properties文件
项目结构
![](https://img2018.cnblogs.com/blog/1005988/201911/1005988-20191101221301980-2099685108.png)
案例代码
配置类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
你所看得到的天才不过是在你看不到的时候还在努力罢了!