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