Spring Framework - Bean Overriding Example!

One Context - Multiple Configuration Metadata Files!

Directory Layout

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
 
     <modelVersion>4.0.0</modelVersion>
 
     <groupId>biz.tugay</groupId>
     <artifactId>begsprch01</artifactId>
     <packaging>jar</packaging>
 
     <version>1.0-SNAPSHOT</version>
 
     <name>begsprch01</name>
     <url>http://www.tugay.biz</url>
 
     <dependencies>
        <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-context</artifactId> 
            <version>4.0.5.RELEASE</version>
        </dependency>
    </dependencies>
 
     <build>
        <finalName>begsprch01</finalName>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>biz.tugay.begsprch01.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

Foo.java
package biz.tugay.begsprch01.service;
 
public interface Foo {
    void foo();
    int getCounter();
}

FooImpl.java
package biz.tugay.begsprch01.service;
 
public class FooImpl implements Foo {
 
    private static int counter = 0;
 
    public FooImpl() {
        counter++;
    }
 
    public void foo() {
        System.out.println("FooImpl#foo()");
    }
 
    public int getCounter() {
        return counter;
    }
}

FooDummyImpl.java
package biz.tugay.begsprch01.service;
 
public class FooDummyImpl implements Foo {
    public void foo() {
        System.out.println("Foo Dummy Impl!");
    }
    public int getCounter() {
        return -1;
    }
}

beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans" 
        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">
 
    <bean id="foo" class="biz.tugay.begsprch01.service.FooImpl">
    </bean>
</beans>

beans-override.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans" 
        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">
 
    <bean id="foo" class="biz.tugay.begsprch01.service.FooDummyImpl">
    </bean>
</beans>

App.java
package biz.tugay.begsprch01;
 
import biz.tugay.begsprch01.service.Foo;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App {
    public static void main(String[] args) {
        final ApplicationContext appContx = 
            new ClassPathXmlApplicationContext("beans-service.xml", "beans-override.xml");
        final Foo foo = (Foo) appContx.getBean("foo");
        foo.foo();
    }
}

Maven Build & Execution
MacBook-Pro:begsprch01 koraytugay$ mvn -q clean install
MacBook-Pro:begsprch01 koraytugay$ mvn -q exec:java
Sep 09, 2017 9:39:30 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@44608238: startup date [Sat Sep 09 21:39:30 CEST 2017]; root of context hierarchy
Sep 09, 2017 9:39:30 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans-service.xml]
Sep 09, 2017 9:39:30 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans-override.xml]
Sep 09, 2017 9:39:30 PM org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
INFO: Overriding bean definition for bean 'foo': replacing [Generic bean: class [biz.tugay.begsprch01.service.FooImpl]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [beans-service.xml]] with [Generic bean: class [biz.tugay.begsprch01.service.FooDummyImpl]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [beans-override.xml]]
Foo Dummy Impl!
MacBook-Pro:begsprch01 koraytugay$ 

Moral of the Story?

See how FooDummyImpl overrides FooImpl. This is achieved by using the same bean id.