Skip to content

kwon37xi/spring-properties-inheritance

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

spring-properties-inheritance

Spring properties inheritance is for Java Spring applications which use properties file(.properties/.xml) for their configurations.

When we have multiple module project, there will be a common configuration and other configurations which are specific for each module.

I thought specific configuration properties file can extend the common configuration properties file. So even though I read only the specific properties file, the common configuration is included automatically.

Features

  1. A Properties file can extend parent properties file, then child properties object includes parent properties.
  2. The parent can have it's parent again.
  3. Child property can override parent's property value.
  4. ${placeholder} will be replaced by the parent property value or System property value.
  5. File location is defined by Spring resource location style like 'claspath:/path/some.properties'.
  6. Support normal properties file(.properties) and xml properties file(.xml)

How to

  1. Copy InheritablePropertiesFactoryBean.java into your project source directory.
  2. Create Spring bean.
  3. I prefer using properties with SpEL like #{configurationProperties['some.key']}.
  4. You can change extendKey ,placeholder prefix/suffix, etc. refer to the source.

Example

bean configuration

<bean id="configurationProperties" class="kr.pe.kwonnam.properties.InheritablePropertiesFactoryBean">
    <property name="location" value="classpath:/child-properties.xml" />
    <property name="extendKey" value="__extends__" /> <!-- actually you don't need this line. It's just an example' -->
</bean>

parent-properties.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <entry key="project.name">Spring Properties Inheritance</entry>
    <entry key="project.home">${java.io.tmpdir}/project</entry>
    <entry key="key.dir">${java.io.tmpdir}/keys</entry>
</properties>

child-properties.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <entry key="__extends__">classpath:/parent-properties.xml</entry>
    <entry key="project.name">Sub project</entry> <!-- override parent's project.name -->
    <entry key="subproject.home">${project.home}/subproject</entry>
    <entry key="userproject.home">${project.home}/users/${user.name}</entry> <!-- user.name from System Properties -->
</properties>

configurationProperties bean will have the following properties

  1. project.name
  2. project.home
  3. key.dir
  4. subproject.home
  5. userproject.home

About

Spring properties inheritance

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published