简体   繁体   English

从属性文件加载Spring属性

[英]Spring properties loading from properties file

I hope this is not a duplicate, but I'm failing to find an answer: 我希望这不是重复的,但是我找不到答案:

I have a Java Spring application with beans.xml: 我有一个带bean.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>
...

And a structure looking like this: 以及一个看起来像这样的结构:

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

And I'm assembling this into a DIR using maven-assembly-plugin with dist.xml looking like this: 我使用带有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>

With relevant pom: 与相关的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>

The objective of this is to have resulting structure: 这样做的目的是要得到结构:

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

This works fine for me so far, except that properties and XML files seem to be bundled inside the JAR and changing the resuting conf/app.properties doesn't have an effect on the production server, 到目前为止,这对我来说还算不错,除了属性和XML文件似乎捆绑在JAR内并且更改重新启动的conf / app.properties对生产服务器没有影响之外,

So how can I achieve the desired dist structure with configuration loaded on startup rather than being bundled inside the JAR? 那么,如何在启动时加载配置而不是将其捆绑在JAR中来实现所需的dist结构?

Are you writing any script for executing your JAR File? 您是否正在编写用于执行JAR文件的脚本? You can easily achieve this by adding the two files from your conf folder on your Classpath while executing the JAR File. 您可以在执行JAR文件时,通过从Classpath的conf文件夹中添加两个文件来轻松实现此目的。

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

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