【问题标题】:Spring boot how to pick externalized spring properties fileSpring Boot如何选择外部化的弹簧属性文件
【发布时间】:2019-08-20 03:16:23
【问题描述】:

我有这个配置需要用于 Spring Boot 应用程序。

server.port=8085
server.servlet.context-path=/authserver
#data source
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=<url>
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

默认情况下,spring-boot 会选择位于 src/main/resources/ 中的 application.properties 文件

我想更改此路径并将 spring boot 引导到不同的 application.properties 文件

我可以使用

java -jar app.jar --spring.config.location=classpath:/another-location.properties  

是否有任何替代解决方案我可以在不通过命令行传递参数的情况下实现这一点?

我在用这个

   @PropertySource("file:C:\Users\test\.test\test.properties")
    @ConfigurationProperties(prefix = "spring")
    public class Configuration {

        private String ddlAuto;

        private String url;

        private String username;

        private String password;

        private String driverClassName;
    }

在我的主课中

@SpringBootApplication
@EnableConfigurationProperties(Configuration.class)
public class Application {

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

}

在我尝试执行应用程序后,在 src/main/resources/ 下的 application.properties 中注释掉所有数据源属性 但它一直给我下面提到的错误并且应用程序无法启动

我指的是这个教程:https://www.mkyong.com/spring-boot/spring-boot-configurationproperties-example/

但正如我提到的,当我启动 spring boot 应用程序时出现此错误

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

Description:

Binding to target org.springframework.boot.context.properties.bind.BindException: 

对此的任何帮助将不胜感激

【问题讨论】:

  • 可能你的进程没有读取文件的权限?你能粘贴完整的堆栈跟踪吗?
  • 你能把@PropertySource注解放在不同的bean上吗?我不确定它是否像这样正确拾取(因为 Configuration 不会是普通的 bean)。 documentation about externalized configuration 建议将这些放在单独的 @Configuration 类中。
  • 不要在您的代码中嵌入指向外部化文件的路径。这是额外的工作,使实际使用变得更加困难。
  • @Shenali Silva 这可能对你有帮助:callicoder.com/spring-boot-configuration-properties-example

标签: java spring spring-boot


【解决方案1】:

具有外部化属性的推荐方法是使用 spring.config.location 系统属性,通过像这样启动您的应用程序:

java -jar -Dspring.config.location=/path/to/my/file.properties app.jar

这样做的原因是您没有在代码和文件系统层次结构之间添加耦合。

在 Spring Boot 2.0 之前,此属性是附加的,这意味着它将补充默认位置。在 Spring Boot 2.0 之后,spring.config.location 替换了默认位置(例如 classpath src/main/resources/application.properties)。要在 2.0 之后保持附加行为,请改用 spring.config.additional-location。

请在此处查看official documentation 关于此事的信息。

【讨论】:

  • 感谢 spring.config.additional-location 提示
【解决方案2】:

我可以让它在 Spring Boot 2.1.2.RELEASE 上正常工作。这就是我所做的: 我的/tmp 文件夹中有一个test.properties,其内容如下:

test.myprop=hello

我在资源文件夹中也有常用的属性文件:

myprop=world

我已经为自定义属性文件创建了一个类:

@Configuration
@PropertySource("file:/tmp/test.properties")
@ConfigurationProperties(prefix = "test")
public class TestConfig {

    private String myprop;

    public String getMyprop() {
        return myprop;
    }

    public void setMyprop(String myprop) {
        this.myprop = myprop;
    }
}

然后在我的主类中,我启用了配置属性:

@EnableConfigurationProperties(TestConfig.class)
@SpringBootApplication
public class MyApp {

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

}

现在我有了这个测试控制器:

@RestController
public class TestController {

    @Value("${test.myprop}")
    private String externalisedProp;
    @Value("${myprop}")
    private String prop;

    @GetMapping("test")
    public void test() {
        System.out.println("externalised: " + externalisedProp);
        System.out.println("not externalised" + prop);
    }
}

一旦调用,就会正确打印:

externalised: hello
not externalised: world

我的TestConfig 类与MyApp 主类在同一个包中。 我所做的与您的解决方案非常相似,几乎相同,您确定您的路径正确吗?另外,我可以看到您的属性文件的内容与您在配置类中的内容不匹配,前缀不同。也许这就是问题所在?

编辑: 我试图从我的属性类(您也没有)中删除 @Configuration 注释,它不再能够获取外部化属性。虽然错误有所不同,但您应该尝试添加它。

【讨论】:

  • prifix is 'spring' spring.jpa.hibernate.ddl-auto=none spring.datasource.url=
  • Config 类参数为 private String ddlAuto 和 private String url。你在里面发现了什么问题?
  • 如果前缀是 spring 并且类中的字段名为 ddlAuto,那么在属性文件中你应该有 spring.ddl-autospring.url,而你没有。我不确定这是否正确。
  • 检查一下,可能对你有帮助:stackoverflow.com/questions/41538171/…
猜你喜欢
  • 2018-02-21
  • 1970-01-01
  • 2020-07-30
  • 2020-02-16
  • 2020-07-25
  • 2015-06-26
  • 2020-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多