Spring ClassPathXmlApplicationContext etc. is multiple as in javadoc If you specify a bean definition, the later bean definition will overwrite the previous one.
src.xml
<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="str" class="java.lang.String">
<constructor-arg value="aaa">
</constructor-arg>
</bean>
</beans>
overwrite.xml
<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="str" class="java.lang.String">
<constructor-arg value="bbb">
</constructor-arg>
</bean>
</beans>
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class XMLConfOverwrite {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("src.xml");
System.out.println(ctx.getBean("str"));//aaa
ClassPathXmlApplicationContext ctx2 = new ClassPathXmlApplicationContext("src.xml", "overwrite.xml");
System.out.println(ctx2.getBean("str"));//bbb
}
}
Recommended Posts