poniedziałek, 1 kwietnia 2013

Utilizing System Properties with Jboss 6.x/7.x and Spring


JBoss gives us possibility to set our application properties / configuration as a java system property. It is very useful for configuring enterprise apps per environment. Usage of this feature is different between JBoss versions. I’ll show you how to utilize it on JBoss AS 6 and 7. Then you will see the most valuable thing, which is SystemPropertiesService from Spring Framework.

JBoss 6.x
Create xml file (deleult name is properties-service.xml ) like shown below and copy it to %JBOSS_HOME%/server/default/deploy/ folder
<server>
  <mbean code="org.jboss.varia.property.SystemPropertiesService" 
  name="jboss:type=Service,name=SystemProperties">
    <!-- 
       | Load properties from each of the given comma seperated URLs
    <attribute name="URLList">
      http://somehost/some-location.properties,
      ./conf/somelocal.properties
    </attribute>
    -->
    <attribute name="Properties">
      gkolpu.blog.example1=MY_Property1
      gkolpu.blog.example2=MY_Property2
    </attribute>
  </mbean>
</server>

JBoss 7.x
Add the code attached below to standalone.xml file, it should be added after <extensions> tag.

<system-properties>
<property name="gkolpu.blog.example1" value="MY_Property1"/>
<property name="gkolpu.blog.example2" value="MY_Property2"/>
</system-properties>
Spring Configuration
Add this code to your spring configuration. It will load your file and make your properties visible in the Spring context. There is one parameter to define, example below use SYSTEM_PROPERTIES_MODE_OVERRIDE which means that you can set all needed properties in the file embedded in your project but they will be override by the properties specified in JBoss configuration. You have 3 options here:
static int
SYSTEM_PROPERTIES_MODE_FALLBACK
          Check system properties if not resolvable in the specified properties.
static int
SYSTEM_PROPERTIES_MODE_NEVER
          Never check system properties.
static int
SYSTEM_PROPERTIES_MODE_OVERRIDE
          Check system properties first, before trying the specified properties.
<bean class="org.springframework.beans.factory.
                       config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" 
                 value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="location" 
                 value="classpath:application.properties"/>
</bean>

Brak komentarzy:

Prześlij komentarz