【问题标题】:Spring properties loading from properties file从属性文件加载弹簧属性
【发布时间】:2015-12-15 19:40:17
【问题描述】:

我希望这不是重复的,但我找不到答案:

我有一个带有 beans.xml 的 Java Spring 应用程序:

...
<bean id="config" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="locations">
     <list>
       <value>classpath:app.properties</value>
       <value>classpath:app-dev-overrides.properties</value>
     </list>
  </property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
  <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
  <property name="properties" ref="config" />
</bean>
...

还有一个看起来像这样的结构:

src/main/resources/
   beans.xml
   db-beans.xml
   app.properties
   app-dev-overrides.properties
   wrapper.conf
src/main/shell
   app.sh
   custom-script.sh
src/main/wrapper
   wrapper.so
   wrapper
   wrapper.jar

我正在使用 ma​​ven-assembly-plugin 将它组装成一个 DIR,dist.xml 看起来像这样:

<id>dist</id>
<formats>
  <format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
  <dependencySet>
     <outputDirectory>lib</outputDirectory>
     <scope>runtime</scope>
  </dependencySet>
</dependencySets>
<fileSets>
<fileSet>
  <!--Get the generated application jar-->
  <directory>${project.build.directory}</directory>
  <outputDirectory>./</outputDirectory>
  <includes>
    <include>*.jar</include>
  </includes>
</fileSet>
<fileSet>
  <!--Get application prod resources (generally bean XMLs and properties files)-->
  <directory>src/main/resources</directory>
  <excludes>
    <exclude>app-dev-overrides.properties</exclude>
  </excludes>
  <outputDirectory>conf</outputDirectory>
</fileSet>
<fileSet>
  <directory>src/main/shell</directory>
  <outputDirectory>./</outputDirectory>         
  <lineEnding>unix</lineEnding>
  <directoryMode>0755</directoryMode>
</fileSet>
<fileSet>
  <directory>src/main/wrapper</directory>
  <outputDirectory>./bin/</outputDirectory>
  <includes>
    <include>libwrapper.so</include>
    <include>wrapper</include>
  </includes>      
</fileSet>

用相关的pom:

  <plugins>      
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>2.6</version>
      <configuration>
         <finalName>my-app-test</finalName>  
         <archive>
           <manifest>
             <addClasspath>true</addClasspath>
             <mainClass>com.test.myapp.Main</mainClass>
             <classpathPrefix>lib</classpathPrefix>
           </manifest>
           <manifestEntries>
             <Class-Path>conf/</Class-Path>
           </manifestEntries>            
         </archive>
       </configuration>
     </plugin>
     <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.5.5</version>
        <configuration>
          <descriptors>
           <descriptor>${project.basedir}/src/main/assembly/dist.xml</descriptor>
          </descriptors>
         <finalName>dist</finalName>
         <appendAssemblyId>false</appendAssemblyId>
       </configuration>
       <executions>
         <execution>
           <phase>package</phase>
           <goals>
             <goal>directory</goal>
           </goals>
         </execution>
       </executions>
     </plugin>
   </plugins>

这样做的目的是获得结果结构:

dist/
   bin/"Java Service Wrapper Stuff"
   conf/"Properties and XML"
   lib/"Libraries"
   myApp.sh (java service wrapper shell)
   myApp.jar (target built jar)

到目前为止,这对我来说效果很好,除了属性和 XML 文件似乎捆绑在 JAR 中并且更改返回的 conf/app.properties 对生产服务器没有影响,

那么我怎样才能在启动时加载配置而不是捆绑在 JAR 中来实现所需的 dist 结构?

【问题讨论】:

    标签: java spring maven maven-assembly-plugin properties-file


    【解决方案1】:

    您是否正在编写任何脚本来执行您的 JAR 文件?在执行 JAR 文件时,您可以通过在 Classpath 上的 conf 文件夹中添加这两个文件来轻松实现此目的。

    【讨论】:

    • 我正在使用 Java 服务包装器 TanukiSoftware 执行 jar
    • 这也是 POM 中的所有内容,缺少的是标准内容,例如 artifact/group/version/dependencies.... 之类的东西。
    • 来自http://wrapper.tanukisoftware.com/doc/english/prop-java-classpath-n.html。你可以这样做:wrapper.java.classpath.1=../conf/myProps.properties
    • 我将 ./conf/ 添加到清单条目中:在 dist.xml 中,所以我的应用程序属性实际上已加载,@Value 注释有效:例如我有一个 db 选择限制变量:@从 myProps.properties 读取的 Value("${db.limits.selectXXXsize}"),但在生产中,如果您停止应用程序并对其进行更改,它不会在代码内部更改,这意味着实际的属性值是捆绑在 JAR 中,而不是从类路径中读取:myProps.properties
    • 从类路径中读取。将这些值从 &lt;value&gt;classpath:app.properties&lt;/value&gt;&lt;value&gt;classpath:app-dev-overrides.properties&lt;/value&gt; 更正为 &lt;value&gt;app.properties&lt;/value&gt;&lt;value&gt;app-dev-overrides.properties&lt;/value&gt;
    猜你喜欢
    • 2022-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 2015-04-14
    • 1970-01-01
    • 2015-11-12
    相关资源
    最近更新 更多