it's ok to define 2 same beans in xml and configuration

允我心安 提交于 2019-12-08 12:17:51

问题


I created an xml context config, and also an annotation-based context configuration class,each creates a bean with same id, same class.

so I thought that this would create a conflict because spring would say "can't find a unique instance of type ttt.TTT" (I have seen this error elsewhere). but surprisingly, the following code actually runs fine, and the bean chosen is the one from xml. if I cross out either of the bean definitions, it works fine too.

so, why am I not getting a bean definition conflict?

package ttt;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@Configuration
class MyConf {
    @Bean(name="ttt")
    public TTT ttt() {
        return new TTT("bean");
    }
}
class TTT {
    String s;
    public TTT(String s) {this.s = s;}
    public void fun() {
        System.out.println("TTT:" + s);
    }

}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:applicationContext-simple.xml"})
public class TTTTest {
    @Autowired
    TTT ttt;

    @Test
    public void test() {
        ttt.fun();
    }


}

XML context config:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"

    >
    <context:component-scan base-package="ttt" />



    <bean id="ttt"
        class="ttt.TTT">
        <constructor-arg value="xml"/>
        </bean>




</beans>

I added a main code (instead of junit test), shows same behavior:

package ttt;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;



public class BBB {
    public static void main(String args[]) throws Exception {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/applicationContext-simple.xml");
        TTT ttt = (TTT) ctx.getBean("ttt");
        ttt.fun();
    }
}

回答1:


Spring Application Contexts behavior is to 'overwrite' by default. that is, as the beans are being loaded up into the factory, if the definition exists (either by annotation of xml), it will simply overwrite it with the next definition.

(to see which one is getting loaded, try changing a property value to see in which order it's being loaded)

have a look here for a better explanation



来源:https://stackoverflow.com/questions/17866379/its-ok-to-define-2-same-beans-in-xml-and-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!