4

I have a Spring Boot application that will use SQL Server. I want to be able to start application without application.properties and after startup to define url, username and password (at this moment i will write everything to application.properties). For instance i start application and from browser populate form with those parameters. I have tried with DataSourceBuilder class but it happend's too early.

DataSourceBuilder
            .create()
            .username(username)
            .password(password)
            .url("jdbc:sqlserver://" + computerName + "\\SQLEXPRESS;DatabaseName=" + dataBaseName)
            .driverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
            .build();

I want to be able to dynamically change data source after application startup.

  • What does it mean that "it happens too early"? DataSourceBuilder can be called at any time, so generally if it is called to early, just postpone this call. – Kamil Oct 10 '18 at 08:52
  • 1
    It happens during startup of application, and i need to call it somehow after application is started. – Alex LeChar Oct 10 '18 at 08:56
  • So maybe you can create some component like DataSourceFactory with single method that wraps a call to DataSourceBuilder? This DataSourceFactory can be injected in every other component and used to create a data source. – Kamil Oct 10 '18 at 09:03
  • You cannot use the default data source in this scenario in my opinion. You will have to define a data source manually using the parameters you need and use the same in JdbcTemplate or anywhere else. Can you tell me if you are using JPA entities or JdbcTemplate? – Pavan Andhukuri Oct 10 '18 at 13:07
  • I am using this dependency spring-boot-starter-data-jpa, and it contains jdbc starter and jpa. – Alex LeChar Oct 10 '18 at 13:10
  • @AlexLeChar Have you find solution to your issue? Actually I have the same problem. Spring-boot initialize datasource at application load. – pillesoft Feb 05 '19 at 11:33
  • @pillesoft Hi pillesoft, no i have not found solution. Any help would be welcome. I am still searching for solution. – Alex LeChar Feb 05 '19 at 15:53

1 Answers1

0

There are multiple ways to solve your problem.

You can use @PostConstruct annotation: Javax’s @PostConstruct annotation can be used for annotating a method that should be run once immediately after the bean’s initialization

Maybe your code could look like something like this:

@Component
public class DatabaseService {
    private DataSource dataSource;

    @PostConstruct
    public void init(){
        this.dataSource = DataSourceBuilder
        .create()
        .username(username)
        .password(password)
        .url("jdbc:sqlserver://" + computerName + "\\SQLEXPRESS;DatabaseName=" + dataBaseName)
        .driverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
        .build();
    }
}

Also you need this dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

Here are more ways: tutorial

vanillaSugar
  • 523
  • 1
  • 5
  • 14
  • 1
    I think that you did not understood fully what i need. I do not need data source handling during startup, i need to handle it after application is started and embedded tomcat up and running. If i use your solution i get error: Parameter 0 of constructor in org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration required a bean of type 'javax.sql.DataSource' that could not be found. – Alex LeChar Oct 10 '18 at 12:39
  • can you please show me your code? Your class where you tried to use my solution – vanillaSugar Oct 10 '18 at 12:45
  • I have used your class, just replaced username, password, computerName and dataBaseName with my values. – Alex LeChar Oct 10 '18 at 12:49
  • Try to add dependency to you application pom which I added to my answer – vanillaSugar Oct 10 '18 at 13:00
  • 1
    I have it already in my pom. That is error with same configuration, and application.properties is empty. Problem is that Spring Boot auto configuration is looking for embedded database if you have that dependency (which i need). I am trying to overcome that and to provide everything after application is up and running. I will try to use Spring Cloud Config or to start application with embedded database and then later to switch to SQL server database. – Alex LeChar Oct 10 '18 at 13:06
  • I guess you checked this question right? https://stackoverflow.com/questions/32074631/spring-boot-application-without-a-datasource – vanillaSugar Oct 12 '18 at 12:36