spring.datasource.url未找到。

huangapple go评论75阅读模式
英文:

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(&quot;dbconnection.txt&quot;));          

	    SpringApplication.run(SpringBootApplicationLuncher.class,args);
	}  

file dbconnection.txt is having below information.

spring.datasource.url=jdbc:mysql://&lt;ip address&gt;:3306/&lt;dbname&gt;
spring.datasource.username=&lt;username&gt;
spring.datasource.password=&lt;password&gt;

When i execute the application, I am getting an error,

Failed to configure a DataSource: &#39;url&#39; 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(&quot;dbconnection.txt&quot;));

        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

huangapple
  • 本文由 发表于 2020年9月22日 04:14:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63999390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定