英文:
spring.datasource.url not found
问题
Sure, here's the translation of the provided content:
我创建了一个 Spring Boot 应用程序,我不想将数据源信息保存在 application.properties 文件中,因为这些信息会经常变化。所以,所有的数据源信息都已经在外部的 .txt 文件中提及。这个文件放在项目的外部。我正在以编程方式读取这个文件。以下是代码,
@SpringBootApplication
public class SpringBootApplicationLuncher {
public static void main(String[] args) throws IOException {
Properties mainProperties = new Properties();
mainProperties.load(new FileInputStream("dbconnection.txt"));
SpringApplication.run(SpringBootApplicationLuncher.class, args);
}
}
文件 dbconnection.txt 包含以下信息。
spring.datasource.url=jdbc:mysql://<ip 地址>:3306/<dbname>
spring.datasource.username=<用户名>
spring.datasource.password=<密码>
当我执行这个应用程序时,我得到了一个错误,
无法配置 DataSource:未指定 'url' 属性且无法配置嵌入式数据源。
有谁可以帮忙吗?
英文:
I have created one spring boot application, I do not want to keep datasource information in application.properties file as this information will keep on changing. So, All data source information have mentioned in external .txt file. This file is placed outside of project. I am reading this file programmatically. Here is the code,
@SpringBootApplication
public class SpringBootApplicationLuncher {
public static void main(String[] args) throws IOException {
Properties mainProperties = new Properties();
mainProperties.load(new FileInputStream("dbconnection.txt"));
SpringApplication.run(SpringBootApplicationLuncher.class,args);
}
file dbconnection.txt is having below information.
spring.datasource.url=jdbc:mysql://<ip address>:3306/<dbname>
spring.datasource.username=<username>
spring.datasource.password=<password>
When i execute the application, I am getting an error,
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Can anyone please help ?
答案1
得分: 1
如果您想将 mainProperties
传递给 Spring Boot 应用程序,您可以使用 SpringApplicationBuilder
来实现:
@SpringBootApplication
public class SpringBootApplicationLauncher {
public static void main(String[] args) throws Exception {
Properties mainProperties = new Properties();
mainProperties.load(new FileInputStream("dbconnection.txt"));
new SpringApplicationBuilder()
.sources(SpringBootApplicationLauncher.class)
.properties(mainProperties)
.run(args);
}
}
然而,Spring Boot 已经提供了一种将配置外部化的方法,详细信息请查阅这份文档。
英文:
If you want to pass mainProperties
into Spring Boot application, you can use SpringApplicationBuilder
for this:
@SpringBootApplication
public class SpringBootApplicationLuncher {
public static void main(String[] args) throws Exception {
Properties mainProperties = new Properties();
mainProperties.load(new FileInputStream("dbconnection.txt"));
new SpringApplicationBuilder()
.sources(SpringBootApplicationLuncher.class)
.properties(mainProperties)
.run(args);
}
}
However Spring Boot already provides a way to externiliaze your configuration, check this documentation for more details
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论