'How can I do in java spring boot not try a database connection on start up?

I need to know if there is a way in Java Spring boot not to try a connection at startup. What I am trying to do is an integrated SQL server connection. I am trying to generate a .jar with integrated connection and not with SQL credentials. As a developer, I don't have access permissions to the database, but the service account that runs the .jar does. The thing is that if I try to establish a connection string with integrated authentication it will try to log in with my credentials and it will never be able to, so it will not be able to generate the .jar At the moment my app is working fine with sql credentials, but I need to migrate to integrated credentials.

My pom.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>X</groupId>
    <artifactId>X</artifactId>
    <version>1.0.0</version>
    <name>X</name>
    <description>X</description>
    <properties>

        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android-json</artifactId>
            <version>0.0.20131108.vaadin1</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

And my application.properties:

spring.datasource.url=jdbc:sqlserver://X.X.X.X:1433;database=X
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.username=X
spring.datasource.password=X
server.port=8080

I also try adding this line in application.properties with no success:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration


Solution 1:[1]

Is the service account useing a special spring profile when starting the jar? Then you could do this in you application.properties:

#---
spring.config.activate.on-profile=serviceProfile 
spring.datasource.url=jdbc:sqlserver://${DB_HOST};database=${DB_NAME} 
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver 
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASSWORD} 
server.port=8080

The service would start the jar like this:

java -jar -Dspring.profiles.active=serviceProfile -DDB_HOST=X.X.X.X:1433 -DDB_NAME=X -DDB_USER=X -DDB_PASSWORD=X application.jar

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1