Give this example a try. You can comment out one of the various beans in the context and see what it does. Doesn't this solve your problem?
BeanOverrideExample.java
Code:
package example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanOverrideExample
{
public static void main ( String [] args )
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext ( new String [] { "applicationContextOne.xml", "applicationContextTwo.xml" } );
SimpleBean simpleBean = ( SimpleBean ) applicationContext.getBean ( "simpleBean" );
simpleBean.sayHello ();
}
}
SimpleBean.java
Code:
package example;
public class SimpleBean
{
private String someOtherProperty;
private AnotherSimpleBean anotherSimpleBean;
public void setSomeOtherProperty ( String someOtherProperty )
{
this.someOtherProperty = someOtherProperty;
}
public void setAnotherSimpleBean ( AnotherSimpleBean anotherSimpleBean )
{
this.anotherSimpleBean = anotherSimpleBean;
}
public void sayHello ()
{
System.out.println ( "Hello World! " + someOtherProperty );
anotherSimpleBean.sayHelloAlso ();
}
}
AnotherSimpleBean.java
Code:
package example;
interface AnotherSimpleBean
{
void sayHelloAlso ();
}
AnotherSimpleBeanOne.java
Code:
package example;
public class AnotherSimpleBeanOne implements AnotherSimpleBean
{
public void sayHelloAlso ()
{
System.out.println ( "Hello World from another simple bean one" );
}
}
AnotherSimpleBeanTwo.java
Code:
package example;
public class AnotherSimpleBeanTwo implements AnotherSimpleBean
{
public void sayHelloAlso ()
{
System.out.println ( "Hello World from another simple bean two" );
}
}
applicationContextOne.xml
Code:
<?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-2.0.xsd">
<bean id="anotherSimpleBean" class="example.AnotherSimpleBeanOne"/>
<bean id="simpleBean" class="example.SimpleBean">
<property name="someOtherProperty" value="applicatioContextOne.xml set this"/>
<property name="anotherSimpleBean" ref="anotherSimpleBean"/>
</bean>
</beans>
applicationContextTwo.xml
Code:
<?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-2.0.xsd">
<bean id="anotherSimpleBean" class="example.AnotherSimpleBeanTwo"/>
<!--
<bean id="simpleBean" class="example.SimpleBean">
<property name="someOtherProperty" value="applicatioContextTwo.xml set this"/>
<property name="anotherSimpleBean" ref="anotherSimpleBean"/>
</bean>
-->
</beans>