繁体   English   中英

使用 spring boot 配置和加载多个属性

[英]Configuring and loading multiple properties using spring boot

我试图使用 spring boot 配置属性。 我将项目打包为 jar 文件,需要能够根据需要更改属性中配置的值(jar 外部),并且属性文件的位置应相对于 jar 文件。 下面的示例代码演示了我如何使用 java 库做到这一点:

Properties properties = new Properties();
String path = System.getProperty("user.dir")+"/config/linuxScripts.properties";

try(FileInputStream fis = new FileInputStream(path)) {
    prop.load(fis);
}catch (Exception e) {
    e.printStackTrace();
}

现在我想配置位于 jar 文件之外的多个属性。 我使用了@PropertySource注释,并且能够使用 Spring Boot 提供的Environment类来获取属性。 不幸的是,在使用@PropertySource注释时,我无法使用System.getProperty("user.dir")来设置路径值。 我需要将属性文件的路径设置为相对于应用程序,而不是在系统环境变量中配置。 有没有办法解决这个问题并获取用户目录?

我曾尝试在配置类中使用PropertySourcesPlaceholderConfigurer 在这种情况下,我似乎无法使用Environment类获取属性。

所以解决方案是基于我们可以通过在清单文件中指定项目来扩展类路径的事实。 所以我们需要

1) 将属性文件保留在 /src/main/resources 中。

2) 将其从最终 jar 中排除

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
                    <excludes>
                        <exclude>**/*.properties</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <mainClass>com.blabla.daemon.MainListener</mainClass>
                            <classpathPrefix>lib/</classpathPrefix>
                            <addClasspath>true</addClasspath>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>conf/</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>

3)在jar外创建一个文件夹conf

4)使用maven从resources文件夹中复制属性文件

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/conf</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                    <includes>
                                        <include>**/*.properties</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

5)指定指向 conf 文件夹的清单文件(步骤 1 的一部分 - 请参阅那里)

<archive>
    <manifest>
      <mainClass>com.blabla.daemon.MainListener</mainClass>
      <classpathPrefix>lib/</classpathPrefix>
         <addClasspath>true</addClasspath>
     </manifest>
     <manifestEntries>
       <Class-Path>conf/</Class-Path>
     </manifestEntries>
  </archive>

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM