Spring Override

Namespace handler that allows for overriding, extending or modifying Spring beans in an modular environment.

License

License

Categories

Categories

IDE Development Tools
GroupId

GroupId

com.apporiented
ArtifactId

ArtifactId

spring-override
Last Version

Last Version

1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Spring Override
Namespace handler that allows for overriding, extending or modifying Spring beans in an modular environment.
Project URL

Project URL

https://github.com/lbehnke/spring-override
Project Organization

Project Organization

Lars Behnke
Source Code Management

Source Code Management

https://github.com/lbehnke/spring-override

Download spring-override

How to add to project

<!-- https://jarcasting.com/artifacts/com.apporiented/spring-override/ -->
<dependency>
    <groupId>com.apporiented</groupId>
    <artifactId>spring-override</artifactId>
    <version>1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.apporiented/spring-override/
implementation 'com.apporiented:spring-override:1.0'
// https://jarcasting.com/artifacts/com.apporiented/spring-override/
implementation ("com.apporiented:spring-override:1.0")
'com.apporiented:spring-override:jar:1.0'
<dependency org="com.apporiented" name="spring-override" rev="1.0">
  <artifact name="spring-override" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.apporiented', module='spring-override', version='1.0')
)
libraryDependencies += "com.apporiented" % "spring-override" % "1.0"
[com.apporiented/spring-override "1.0"]

Dependencies

compile (2)

Group / Artifact Type Version
org.springframework : spring-context jar 4.1.4.RELEASE
ch.qos.logback : logback-classic jar 1.1.2

test (3)

Group / Artifact Type Version
junit : junit jar 4.11
org.mockito : mockito-all jar 1.10.19
org.springframework : spring-test jar 4.1.4.RELEASE

Project Modules

There are no modules declared in this project.

Spring Override

This library provides an XML namespace handler that allows for overriding, extending or modifying beans in a modular Spring environment. The code was originally written by Felix Gnass and Carsten Woelk back in 2007 as part of the riot content management system. So all credits go to them.

As Pivotal has abandoned the Spring DM project I figured that riot's pragmatic override feature is still valuable to many developers who try to modularize their Spring projects. So I decoupled the feature and moved it into its own project. Although this library comes with a Spring 4.1 dependency, it should also work with older version. Here is a very simple example how to use it:

If you are using Maven, include the dependency into your pom.xml

<dependency>
    <groupId>com.apporiented</groupId>
    <artifactId>spring-override</artifactId>
    <version>${spring-override.version}</version>
</dependency>

Make sure you include the application context files of all your modules. A web project could specify the context locations like this:

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath*:META-INF/spring/application-context-*.xml</param-value>
</context-param>

Let's assume you have the following configuration in your main project (application-context-main.xml):

<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:override="http://www.apporiented.com/schema/spring/override"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.apporiented.com/schema/spring/override
        http://www.apporiented.com/schema/spring/override.xsd">
        
    <bean id="bean" class="com.apporiented.spring.override.OverrideTestBean">
        <property name="string" value="Original" />
        <property name="list">
            <list>
                <value>string1</value>
                <value>string2</value>
                <value>string3</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="key" value="original" />
            </map>
        </property>
    </bean>
</beans>

Now a submodule can change application's behavior by providing a configuration such as (application-context-sub.xml)

<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:override="http://www.apporiented.com/schema/spring/override"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.apporiented.com/schema/spring/override
        http://www.apporiented.com/schema/spring/override.xsd">

    <override:properties ref="bean">
        <property name="string" value="overridden" />
    </override:properties>

    <override:add ref="bean" property="list" append="true">
        <value>string4</value>
        <value>string5</value>
    </override:add>

    <override:put ref="bean" property="map">
        <entry key="key" value="overridden" />
    </override:put>

</beans>

The bean overriding occurs at a very early stage of the Spring initialization process. Hence, all autowired dependencies to bean will see the modifications made by the sub module.

Versions

Version
1.0