相关疑难解决方法(0)

根据环境变量设置Spring Boot application.properties

我有一个将在各种环境中运行的Spring Boot应用程序,并且根据它运行的环境,它将连接到不同的数据库.我有几个application.properties文件,每个环境对应一个文件:

application-local.properties:

spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=dbuser
spring.datasource.password=123456789
Run Code Online (Sandbox Code Playgroud)

application-someserver.properties:

spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://someserver:5432/myproddb
spring.datasource.username=produser
spring.datasource.password=productionpass
Run Code Online (Sandbox Code Playgroud)

等等

在我的每个环境中,我都有一个环境变量MYENV,例如,它被设置为环境类型,local或者someserver(application-{env}.properties文件的名称与环境名称完全匹配).

如何让spring boot读取这个环境变量并自动选择正确的.properties文件?我不想做整个-Dspring.profiles.active=someserver因为这个包的部署方式(它不会作为jar运行).

java spring spring-boot

12
推荐指数
1
解决办法
2万
查看次数

Yml使用Spring启动配置文件"继承"

我在网上找不到直接的答案.

Spring Boot的yml文件是否相互"继承"?我的意思是,如果我有: application.yml哪个

server:
  port: 80
  host: foo
Run Code Online (Sandbox Code Playgroud)

而且application-profile1.yml只有

server:
  port: 90
Run Code Online (Sandbox Code Playgroud)

因此,如果我使用profile1活动配置文件启动Spring Boot ,我是否也将server.host属性设置为foo

spring-profiles spring-boot

10
推荐指数
1
解决办法
2630
查看次数

spring - 从application.properties文件中读取环境变量

我在application.properties文件中指定了Spring属性.如何从环境变量中填充这些属性?

这是我尝试过的,但它似乎不起作用:

application.properties

spring.datasource.url=jdbc:postgresql://#{ systemProperties['DATABASE_HOST']}:5432/dbname
spring.datasource.username = postgres
spring.datasource.password = postgres
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot

7
推荐指数
1
解决办法
1万
查看次数

是否可以将Spring Boot应用程序部署到App Engine并连接到数据库?

我觉得我在这里转转,所以,请忍受我。我想将Spring Boot应用程序部署到App Engine,但与Google提供简单示例不同,我需要数据库,这意味着需要凭据。我正在Google App Engine的Standard上运行Java 11。

我设法通过以下方法使我的应用程序成功连接application.properties

spring.datasource.url=jdbc:postgresql://google/recruiters_wtf?cloudSqlInstance=recruiters-wtf:europe-west2:recruiters-wtf&socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=the_user&password=monkey123
Run Code Online (Sandbox Code Playgroud)

问题是我不想将任何凭据提交到存储库,因此,这是不可接受的。我可以使用环境变量,但随后必须在app.yaml文件中定义它们。我要么保留app.yaml部署所需的未提交的文件(这很麻烦),要么提交它,然后回到第一层,向存储库提交凭据。

既然Google App Engine显然无法以任何其他方式定义环境变量与Heroku不同),这是否意味着在不使用某些不安全/麻烦的做法的情况下就不可能将Spring Boot应用程序部署到App Engine并使其连接到数据库?我觉得我在这里缺少什么。

google-app-engine spring-boot

6
推荐指数
1
解决办法
280
查看次数

Spring Boot 2-在初始化bean之前做一些事情

问题陈述

我想在初始化bean之前从类路径或外部位置的属性文件中加载属性。这些属性也是Bean初始化的一部分。我无法从Spring的标准application.properties或其自定义项自动装配属性,因为同一属性文件必须可由多个可部署对象访问。

我尝试过的

我知道Spring应用程序事件;实际上,在初始化Spring Context之后,我已经钩住 ContextRefreshedEvent来执行一些任务(在此阶段还初始化了Bean)。

对于我的问题声明,从Spring Docs的描述来看ApplicationEnvironmentPreparedEvent,它看起来很有希望,但是该挂钩没有用。


@SpringBootApplication
public class App {

    public static void main(String[] args) throws IOException {
        SpringApplication.run(App.class, args);
    }


    @EventListener
    public void onStartUp(ContextRefreshedEvent event) {
        System.out.println("ContextRefreshedEvent");    // WORKS
    }

    @EventListener
    public void onShutDown(ContextClosedEvent event) {
        System.out.println("ContextClosedEvent");   // WORKS
    }

    @EventListener
    public void onEvent6(ApplicationStartedEvent event) {
        System.out.println("ApplicationStartedEvent");  // WORKS BUT AFTER ContextRefreshedEvent
    }


    @EventListener
    public void onEvent3(ApplicationReadyEvent event) {
        System.out.println("ApplicationReadyEvent");    // WORKS WORKS BUT AFTER ContextRefreshedEvent
    }


    public void …
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot

6
推荐指数
2
解决办法
464
查看次数

具有环境变量的Spring Boot配置

我有一个Spring Boot应用程序,它与DB交互以使用Spring Data Rest提供资源。我想从环境变量中获取配置。以下是我的属性文件。

spring.datasource.url=${mysql.url}
spring.datasource.username=${mysql.user}
spring.datasource.password=${mysql.password}
Run Code Online (Sandbox Code Playgroud)

我的环境变量在图像https://ibb.co/cyxsNc中

我什至尝试了以下配置

spring.datasource.url=${MySQL_Url}
spring.datasource.username=${MySQL_User}
spring.datasource.password=${MySQL_Password}
Run Code Online (Sandbox Code Playgroud)

但是我无法连接到数据库并出现以下错误

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalArgumentException: URL must start with 'jdbc'
Run Code Online (Sandbox Code Playgroud)

应用程序文件夹结构

Project
|-- src/main/java
    |-- com.example.app
        |-- DemoApplication.java …
Run Code Online (Sandbox Code Playgroud)

spring spring-boot

5
推荐指数
2
解决办法
1万
查看次数

如何在 appplicaton.properties 中以 12 因子方式设置系统环境变量?

我无法从我的环境变量中获取值。有很多类似的问题。但是NONE他们的工作对我来说

应用程序属性

spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=${SPRING_DATASOURCE_USERNAME}
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}
Run Code Online (Sandbox Code Playgroud)

系统变量

Variable name : SPRING_DATASOURCE_USERNAME
Variable Value : dbuser

Variable name : SPRING_DATASOURCE_PASSWORD
Variable Value : 123456789
Run Code Online (Sandbox Code Playgroud)

错误

invalid username/password; logon denied
Run Code Online (Sandbox Code Playgroud)

但是当我对其进行硬编码时,它工作正常。

更新

environment-variables properties-file spring-boot

3
推荐指数
2
解决办法
2万
查看次数