Generate data source using external configuration file in Spring-boot __java

Source: Internet
Author: User
Tags postgresql

Spring data provides a powerful JPA (largely without the need to write implements to the method)
Spring Boot provides a convenient automatic configuration. Netgloo example how to use only a single configuration file application.properties data access functionality.
But sometimes what you need is that DataSource is externally configurable, not written dead in project.
So, bloggers here provide a data source that replaces spring boot automatically with an external profile to generate a data source. First Step

Create a configuration file in the project that the spring boot default requires: Src/main/resources/application.properties

    Spring.datasource.url = jdbc:postgresql://localhost:5432/bmsc
    spring.datasource.username = Aere
    Spring.datasource.password = Aerexu

    Spring.datasource.testWhileIdle = True
    Spring.datasource.validationQuery = SELECT 1

    spring.jpa.properties.datasource.driver-class-name= Org.postgresql.Driver
    spring.jpa.properties.hibernate.dialect = Org.hibernate.dialect.PostgreSQL9Dialect

    # Show or don't log for all SQL query
    Spring.jpa.show-sql = True

    # Hibernate DDL auto (Create, Create-drop, up Date)
    Spring.jpa.hibernate.ddl-auto = Update

    # naming strategy
    Spring.jpa.hibernate.naming-strategy = Org.hibernate.cfg.ImprovedNamingStrategy
Second Step

Generate a required configuration file on a different path C:\Users\test\Workplace\config\SpringAll\datasource.properties

Spring.datasource.url = jdbc:postgresql://192.168.99.100:5432/bmsc
spring.datasource.username = Aere
Spring.datasource.password = Aerexu
One last step

Generate a Spring configuration class Persistencejpaconfig

    Package COM.AERE.SPRING.ALL.CONFIG.JPA;

    Imports ...

    @Configuration
    @ComponentScan
    @PropertySource (value = {"Classpath:/application.properties",
            "file:/c : \\Users\\test\\Workplace\\config\\SpringAll\\datasource.properties "},
            Ignoreresourcenotfound = True)
    public class Persistencejpaconfig {

        @Bean
        @ConfigurationProperties (prefix= "Spring.datasource")
        Public DataSource DataSource () {return
            new Drivermanagerdatasource ();
        }
    }

You may notice that there are two configuration files in the @propertysource. Of course, the actual limited will only be one. If all two files exist, then one file is valid, and if only one file exists, it is of course the limited existence. The purpose of this is to facilitate the use of the default configuration files in the project in development, and to leverage external configuration files in testing and deployment.

Well, that's all the steps. Is it simple, with only a few tips. In any case, simple and effective code is the best. In addition:

If you need to display the password is encrypted, then you can customize a DataSource class to inherit the above Drivermanagerdatasource. As shown below:

public class Encrypteddrivermanagerdatasource extends drivermanagerdatasource{...
    Public encrypteddrivermanagerdatasource (string URL, string username, string password) {
        seturl (URL);
        Setusername (username);
        String decryptedpass = somedecryptmethod (password);
        SetPassword (Decryptedpass);
    }
    ...
}

Replace the Drivermanagerdatasource in the above class Persistencejpaconfig with Encrypteddrivermanagerdatasource

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.