从属性文件加载Spring属性

时间:2015-09-18 15:24:58

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

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

我有一个带有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

我正在使用带有dist.xml的 maven-assembly-plugin 将其组装成DIR,如下所示:

<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中并且更改resuting conf / app.properties对生产服务器没有影响,

那么如何在启动时加载配置而不是捆绑在JAR中来实现所需的dist结构?

1 个答案:

答案 0 :(得分:0)

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