Spring-boot中利用外部配置文件生成数据源
来源:互联网 发布:mac上可以玩什么网游 编辑:程序博客网 时间:2024/06/05 11:31
Spring data 提供了一种很强大的JPA(主要是不需要给方法写implements)
Spring boot则提供了方便的自动配置。 netgloo 的例子 如何只利用一个单一的配置文件 application.properties 数据访问功能。
不过有时,你需要的是datasource是外部可配置的,而不是写死在project中的。
所以,这里博主便提供一种通过外部配置文件产生数据源来替换Spring boot自动生成的数据源。
第一步
在工程中生成一个Spring boot默认需要的配置文件: src/main/resources/application.properties
spring.datasource.url = jdbc:postgresql://localhost:5432/bmsc spring.datasource.username = aere spring.datasource.password = aerexu spring.datasource.testWhileIdle = true spring.datasource.validationQuery = SELECT 1 spring.jpa.properties.datasource.driver-class-name=org.postgresql.Driver spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQL9Dialect # Show or not log for each sql query spring.jpa.show-sql = true # Hibernate ddl auto (create, create-drop, update) spring.jpa.hibernate.ddl-auto = update # Naming strategy spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
第二步
在其他路径生成一个需要的配置文件C:\Users\test\Workplace\config\SpringAll\datasource.properties
spring.datasource.url = jdbc:postgresql://192.168.99.100:5432/bmscspring.datasource.username = aerespring.datasource.password = aerexu
最后一步
生成一个Spring的配置类 PersistenceJPAConfig
package com.aere.spring.all.config.jpa; // Imports ... @Configuration @ComponentScan @PropertySource(value = {"classpath:/application.properties", "file:/C:\\Users\\test\\Workplace\\config\\SpringAll\\datasource.properties"}, ignoreResourceNotFound = true) public class PersistenceJPAConfig { @Bean @ConfigurationProperties(prefix="spring.datasource") public DataSource dataSource() { return new DriverManagerDataSource(); } }
你可能会注意到在@PropertySource
中有两个配置文件。当然,实际有限的只会是一个。如果两个文件都存在,则后面一个文件有效;如果只有一个文件存在,当然是存在的那个有限。这么做的目的的便于在开发中使用工程中的默认的配置文件,而在测试、部署中利用外部的配置文件。
好啦,这就是所有步骤了。是不是很简单,只用了一些小技巧?无论如何,简洁有效的代码才是最好的。
另外:
如果你需要显示出来的密码是加密过的,那你可以自定义一个Datasource类继承上面的 DriverManagerDataSource. 如下所示:
public class EncryptedDriverManagerDataSource extends DriverManagerDataSource{ ... public EncryptedDriverManagerDataSource(String url, String username, String password) { setUrl(url); setUsername(username); String decryptedPass = someDecryptMethod(password); setPassword(decryptedPass); } ...}
把上面类 PersistenceJPAConfig 中 DriverManagerDataSource 替换成 EncryptedDriverManagerDataSource
- Spring-boot中利用外部配置文件生成数据源
- Spring-boot中利用外部配置文件生成数据源
- spring boot 使用外部配置文件
- Spring Boot入门教程-数据源外部配置
- Spring Boot 将配置文件放到jar外部
- Spring Boot配置文件放在jar外部
- [Idea]DataBase通过Spring数据源配置文件生成数据源
- 在Spring中利用Hibernate的配置文件生成数据库表
- Spring-Boot初学之配置文件application.properties(数据源配置)
- Spring-Boot根据配置文件生成Bean
- Spring配置文件中配置数据源(dataSource)
- Spring Boot教程 - 3. 将配置文件放到jar外部
- Spring boot利用hibernate生成数据库表
- spring配置文件中读取外部参数
- Spring中使用外部的properties配置文件
- Log4j配置文件位置+Spring中数据源配置文件位置
- Log4j配置文件位置+Spring中数据源配置文件位置
- spring boot之多数据源
- 自定义UISlider
- 组合数学 随记
- 我理解的webservice是这样的
- MVC——第一个界面的数据传到第二个界面
- 剑指Offer--008-旋转数组的最小数字
- Spring-boot中利用外部配置文件生成数据源
- spring(13)------全面深入解析spring的AOP
- java位操作总结
- Claris and XOR
- raspberry pi使用vsftpd搭建ftp服务器
- Atitit.跨语言异常转换机制 java c# php到js的异常转换
- 【Asp.net】初识
- CSS3之选择器2(伪类)
- 设计模式-3-工厂方法和抽象工厂模式