简体   繁体   中英

spring.datasource.url not found

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM