Java 如何从文件中设置spring.datasource.properties?

Java 如何从文件中设置spring.datasource.properties?,java,spring-boot,Java,Spring Boot,因此,我有一个application.properties文件,其中配置了我的基本数据源配置: #数据源配置 spring.datasource.url=jdbc:mysql://host:3306/schema?useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC spring.datasource.username=username spring.datasource.passwor

因此,我有一个application.properties文件,其中配置了我的基本数据源配置:

#数据源配置
spring.datasource.url=jdbc:mysql://host:3306/schema?useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver类名=com.mysql.cj.jdbc.driver
但对于生产,我使用的是一个.xml文件,它具有所有这些属性和更多属性:


登台
贝宝沙箱
条纹沙箱
instamojo沙盒
jdbc:mysql://host:3306/schema
用户名
密码
https://subdomain.domain.com
jdbc:mysql://localhost:3306/schema
根
根
https://subdomain.domain.com
我正在使用
javax.xml.parsers.DocumentBuilder
org.w3c.dom.Node
从.xml文件中获取数据,因此没有问题。有人能建议我如何设置从文件中提取的数据源属性,并告诉Spring Boot不要显式查看application.properties吗

更新1 我创建了一个ProjectConfigurations.java文件来动态设置数据源:

package com.papertrue.util;
导入java.io.File;
导入java.io.IOException;
导入java.util.HashMap;
导入javax.sql.DataSource;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.parserConfiguration异常;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.boot.jdbc.DataSourceBuilder;
导入org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.jdbc.core.jdbc模板;
导入org.w3c.dom.Document;
导入org.w3c.dom.Element;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
导入org.xml.sax.SAXException;
@配置
公共类项目配置{
公共静态字符串PAYPAL_MODE=“”;
私有静态字符串STRIPE_MODE=“”;
公共静态字符串INSTAMOJO_MODE=“”;
public static HashMap configMap=new HashMap();
公共静态void readConfiguration(){
试一试{
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(新文件(“/etc/../configurationsFile”);
doc.getDocumentElement().normalize();
...
NodeList NodeList=doc.getElementsByTagName(配置);
for(int i=0;i
但是应用程序没有处理该文件。我不知道原因是什么,但这是我的主要应用程序文件:

package com.papertrue.oms;
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.springboot应用程序;
导入org.springframework.boot.autoconfigure.domain.EntityScan;
导入org.springframework.context.annotation.ComponentScan;
导入org.springframework.data.jpa.repository.config.EnableJpaRepositories;
导入org.springframework.web.bind.annotation.GetMapping;
导入org.springframework.web.bind.annotation.ResponseBody;
导入org.springframework.web.bind.annotation.RestController;
导入com.papertrue.student.student;
导入com.papertrue.student.StudentService;
@SpringBootApplication(scanBasePackages=“com.papertrue.student”)
@EnableJParepositions(“com.papertrue.student”)
@组件扫描(“com.papertrue.student”)
@EntityScan(“com.papertrue.student”)
@RestController
公共类OMSV2应用程序{
...
公共静态void main(字符串[]args){
run(OmsV2Application.class,args);
}
}
更新2 将
dataSource()
更改为
getDataSource()
,并将项目配置移动到
package com.papertrue.student。它正在工作,随着这个演示项目设置的成功,我现在终于可以开始实际的项目了。谢谢