Announcement Announcement Module
Collapse
No announcement yet.
it's ok to define 2 same beans in xml and configuration ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    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?
    Code:
        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:
    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" 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:

    Code:
     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();
            }
        }

  • #2
    there is only 1 bean... One overrides the other... Beans with the same id override each other.

    Comment

    Working...
    X