[JAVA] Overwrite bean definition in spring xml configuration file with another xml

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

Overwrite bean definition in spring xml configuration file with another xml
Fitted in Spring Boot using a bean definition file named application.xml
Spring Framework bean definition XML: custom tag
File output bean as JSON in spring
[Note] Configuration file when using Logback with Spring Boot
File upload with Spring Boot
Implement file download with Spring MVC
`SAXReader`: Get the xml file with the default namespace set in XPath format
Decrypt the entire bean in a properties file with some values encrypted
How to bind request parameters in list format with bean property in Spring
Static file access priority in Spring boot
Local file download memorandum in Spring Boot
Read xlsx file in Java with Selenium
DI SessionScope Bean in Spring Boot 2 Filter
Maven configuration problem in Spring pom.xml in Eclipse