正确阅读外部属性

时间:2019-10-03 10:10:56

标签: maven spring-boot

我如何从override.properties ccr.read.clientSecret值 我在Windows 10上 我正在尝试使用命令行运行Spring Boot应用程序

java -jar -Dspring.profiles.active=local -Dspring.config.location=file:D:/myname/Properties/override.properties myapp-0.0.1-SNAPSHOT.jar

我的jar文件具有application.properties

#tomcat port
server.port=8081

#Spring Batch App
spring.batch.job.enabled=false
spring.profiles.active=

#JPA Properties
spring.jpa.show-sql=false
spring.jpa.hibernae.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
spring.batch.initialize-schema=always 

我的application-local.properties是

accr.read.clientId=JOHN
accr.read.clientSecret=
accr.read.grantType=client_credentials
accr.read.scope=scope1


# Database settings
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@devdb.in.world:1522/DEV01
spring.datasource.username=USER$NAME
spring.datasource.password=password123

override.properties

ccr.read.clientSecret=passwordisthis

当我执行第一个命令以运行Spring Boot应用程序时 我开始出现以下错误

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-10-03 15:18:43.675 ERROR 14828 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   :

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
        If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
        If you have database settings to be loaded from a particular profile you may need to activate it (the profiles local are currently active).

3 个答案:

答案 0 :(得分:0)

解决问题的最简单方法是像这样运行应用程序

java -jar -Dspring.profiles.active=local myapp-0.0.1-SNAPSHOT.jar --ccr.read.clientSecret=passwordisthis

您看到的异常是因为您指定的位置仅具有覆盖属性。您应该包括多个位置(在您的情况下,也包括类路径),以便spring知道要查找的位置。

示例:

java -jar -Dspring.config.location=file:D:/myname/Properties/override.properties,classpath:/ myapp-0.0.1-SNAPSHOT.jar

请务必注意,同时使用配置文件和替代属性文件的方法可能会导致问题(因为设置活动配置文件时,应用程序上的属性-$ {profile} .properties将具有最高优先级并且不会被覆盖(如果存在)。

关于弹簧外部属性https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

的完整文档

答案 1 :(得分:0)

您需要使用@PropertySource创建和注释一个类,如下所示:

@PropertySource("${my.config.location}")
@Component
public class Config {

    @Value("${ccr.read.clientSecret}")
    private String clientSecret;

    public String getClientSecret() {
        return clientSecret;
    }

    public void setClientSecret(String clientSecret) {
        this.clientSecret = clientSecret;
    }

    @Override
    public String toString() {
        return "Config{" +
                "clientSecret='" + clientSecret + '\'' +
                '}';
    }
}

您还可以在配置类上使用@PropertySource

现在使用-Dmy.config.location=file:D:/myname/Properties/override.properties变量运行应用程序。请注意,由于以下原因,我更改了变量名称。

您的方法有什么问题:-Dspring.config.location指向application.properties文件位置,但是您在类路径中已经有一个。通过覆盖application.properties文件位置,您可以用...\override.properties(不是目录)替换classpath中存在的文件。

  

默认情况下,Spring Boot尝试加载application.properties(或   application.yml)从以下位置:   类路径:/,类路径:/ config /,文件:./,文件:./ config /。当然,我们   可能会覆盖它。您可以通过以下方式更改主配置文件的名称   设置环境属性spring.config.name或仅更改   设置属性 spring.config.location 可以搜索整个搜索路径。它   可以包含目录名称以及文件路径。

有关更多详细信息,请参见this post

答案 2 :(得分:0)

根据此规则“如果在spring.config.location中指定了任何文件,则这些文件的特定于配置文件的变体 文件不被考虑。如果您还想使用spring.config.location中的目录 特定于配置文件的属性”将不视为特定于配置文件的文件的使用位置。