簡體   English   中英

Spring外部配置位置不起作用

[英]Spring External Config Location doesn't work

我具有以下已部署的結構:

├── bin
│   ├── stop.sh
│   └── start.sh
├── config
│   ├── application-dev.properties
│   ├── application-local.properties
│   ├── application.properties
│   └── logback.xml
├── lib
    ├── myjar.jar
|__ logs

....

我的啟動腳本是這樣的:

jar -jar ../lib/myjar.jar --spring.profiles.active=dev --spring.config.location=file:./../config/

活動配置文件被拾取,但是似乎spring.config.location被忽略了,並且是從打包的jar中獲取的。

我已經在這里閱讀了所有有關外部配置的內容-https: //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html,但是嘗試了一些變化,包括類路徑

--spring.config.location=file:./../config/,classpath:/

但這是行不通的。 我也嘗試過將選項與-D一起使用,但這也不起作用。

謝謝你的幫助

使用jarmetr從jar運行命令:

java -jar -Dfile.location=myfile.properties your_app.jar

在您的應用程序中,您可以獲得價值:

System.getProperty("file.location");

以及完整的示例類配置:

@Configuration
@ComponentScan
public class MyConfig{

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties2() throws IOException {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        PropertiesFactoryBean properties = new PropertiesFactoryBean();

        Resource resource = new FileSystemResource(System.getProperty("file.location"));

        properties.setLocation(resource);
        propertySourcesPlaceholderConfigurer.setProperties(properties.getObject());
        return propertySourcesPlaceholderConfigurer;
    }

}

在類( @Component並對其進行擴展)中,可以使用屬性文件中的變量:

@Value("${myperty.host}")
private String host;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM