简体   繁体   English

部署应用程序后如何在 spring 启动应用程序中提供数据源属性

[英]How to provide DataSource properties in spring boot application after deployment of the application

I am developing a server that is configured to a PostgreSQL database, I want that application takes data source properties ( username, password, port, hostname etc.) from the client specific to his database so how can I take input from the client and pass that configuration after deployment because I want to distribute only WAR file of the server我正在开发一个配置为 PostgreSQL 数据库的服务器,我希望该应用程序从特定于他的数据库的客户端获取数据源属性(用户名、密码、端口、主机名等),所以我如何从客户端获取输入并传递部署后的配置,因为我只想分发服务器的 WAR 文件

Yes You can Add configuration Dynamically from the Client side.是的,您可以从客户端动态添加配置。 We can achieve this concept by spring-boot hibernate.我们可以通过spring-boot hibernate来实现这个概念。

--> You have to create an class to Auto congifure the propertise. --> 你必须创建一个 class 来自动配置属性。 I am sharing my sample code to configure the propertise.我正在共享我的示例代码来配置属性。

    package com.spring.hibernate.config;

import java.util.Properties;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class Configurations {
    
    @Value("${db.url}")
    private String URL;
    
    @Value("${db.username}")
    private String USERNAME;
    
    @Value("${db.password}")
    private String PASSWORD;
    
    @Value("${hibernate.hbm2ddl.auto}")
    private String HBM2DDL;
    
    @Value("${hibernate.show_sql}")
    private String SHOW_SQL;
    
    @Value("${hibernate.dialect}")
    private String DIALECT;
    
    @Value("${spring.entitymanager.packagestoscan}")
    private String packagesToscan;
    
    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl(URL);
        dataSource.setUsername(USERNAME);
        dataSource.setPassword(PASSWORD);
        return dataSource;
    }
    
    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(packagesToscan);
        Properties hibernatePropertise = new Properties();
        hibernatePropertise.put("hibernate.dialect", DIALECT);
        hibernatePropertise.put("hibernate.hbm2ddl.auto", HBM2DDL);
        hibernatePropertise.put("hibernate.show_sql", SHOW_SQL);
        sessionFactory.setHibernateProperties(hibernatePropertise);
        return sessionFactory;
    }
}

Have a configuration class in the above structure to configure the application Properties file.在上面的结构中有一个配置class来配置应用Properties文件。 And have your application properties file as follow.并让您的应用程序属性文件如下。

db.url: jdbc:mysql://localhost:3306/data_db
db.username: root
db.password: 8452

    hibernate.hbm2ddl.auto=update
    hibernate.show_sql=true
    hibernate.dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    spring.entitymanager.packagestoscan: com.spring.hibernate

Use the above sample code.使用上面的示例代码。 Still any logics needed have a command for me...Thank You..仍然需要任何逻辑对我有命令......谢谢..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 访问spring启动应用程序.properties中提到的配置文件,在创建.jar后用于App引擎部署 - Access configuration file mentioned in spring boot application.properties after creating the .jar for App engine deployment 定义e如何在spring boot项目上的application.properties中选择第二个数据源 - How define e chose a second datasource from application.properties on spring boot project Spring 引导应用程序属性 - Spring boot application Properties "Spring Boot 中 application.properties 中的 datasource.url 和 datasource.driverClassName 是什么" - What is datasource.url and datasource.driverClassName in application.properties in Spring Boot Spring 引导应用程序部署问题 - Spring Boot Application Deployment Issue Spring 启动应用的部署图 - Deployment Diagram for Spring Boot application 如何阅读Spring Boot application.properties? - How to read Spring Boot application.properties? 如何在Spring Boot 1应用程序中查找未使用的属性 - How to find unused properties in a spring boot 1 application 如何在 Spring 引导 2 应用程序中指定 GZip 属性 - How to specify GZip properties in Spring boot 2 application 如何将日志记录属性添加到Spring启动应用程序 - How to add logging properties to spring boot application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM