简体   繁体   English

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

[英]Configuring and loading multiple properties using spring boot

I was trying to configure properties using spring boot.我试图使用 spring boot 配置属性。 I will be packaging the project as a jar file and need to be able to change the value configured in the properties(outside the jar) as required and the location of the properties file should be relative to the jar file.我将项目打包为 jar 文件,需要能够根据需要更改属性中配置的值(jar 外部),并且属性文件的位置应相对于 jar 文件。 The following sample code demonstrates how I did this using java libraries :下面的示例代码演示了我如何使用 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();
}

Now that I want to configure multiple properties that are located outside the jar file.现在我想配置位于 jar 文件之外的多个属性。 I have used @PropertySource annotation and I was able to fetch the properties using Environment class provided by spring boot.我使用了@PropertySource注释,并且能够使用 Spring Boot 提供的Environment类来获取属性。 Unfortunately, I cannot use System.getProperty("user.dir") to set the path value when using @PropertySource annotation.不幸的是,在使用@PropertySource注释时,我无法使用System.getProperty("user.dir")来设置路径值。 I need to set the path of the properties file as relative to the application and not configured in the system environment variable.我需要将属性文件的路径设置为相对于应用程序,而不是在系统环境变量中配置。 Is there a way to workaround the issue and get the user directory?有没有办法解决这个问题并获取用户目录?

I have tried using PropertySourcesPlaceholderConfigurer in the configuration class.我曾尝试在配置类中使用PropertySourcesPlaceholderConfigurer As it seems like I wont be able to get the properties using Environment class in this case.在这种情况下,我似乎无法使用Environment类获取属性。

So the solution is based on fact that we may extend classpath by specifying items in manifest file.所以解决方案是基于我们可以通过在清单文件中指定项目来扩展类路径的事实。 So we need to所以我们需要

1) Leave properties file in /src/main/resources. 1) 将属性文件保留在 /src/main/resources 中。

2) Exclude it from a final jar 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) Make a folder conf outside the jar 3)在jar外创建一个文件夹conf

4) Copy there properties file from resources folder with maven 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) Specify manifest file that points to the conf folder (a part of step 1 -see there) 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