Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

same bean id on different xml file, will it merge?

Tags:

spring

liferay

i'm reading liferay source code and found out that 2 xml files using same bean-id. will all the properties merge together if using this way?

dynamic-data-spring
----------------------
    <bean id="liferayDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
        <property name="targetDataSource">
            <bean class="org.springframework.aop.framework.ProxyFactoryBean">
                <property name="targetSource" ref="dynamicDataSourceTargetSource" />
            </bean>
        </property>
    </bean>

infrastructure-spring.xml
----------------------
<bean id="liferayDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
        <property name="targetDataSource">
            <bean class="com.liferay.portal.dao.jdbc.util.DataSourceFactoryBean">
                <property name="propertyPrefix" value="jdbc.default." />
            </bean>
        </property>
    </bean>
like image 784
cometta Avatar asked Sep 18 '09 07:09

cometta


People also ask

Can we have 2 beans with same id in Spring?

Here, @Bean instantiates two beans with ids the same as the method names and registers them within the BeanFactory (Spring container) interface. Next, we can initialize the Spring container and request any of the beans from the Spring container. This strategy also makes it simple to achieve dependency injection.

Can two beans have same name?

Spring beans are identified by their names within an ApplicationContext. Therefore, bean overriding is a default behavior that happens when we define a bean within an ApplicationContext that has the same name as another bean. It works by simply replacing the former bean in case of a name conflict.

How do I reference a bean of another XML file in Spring?

Just import the xml defining the bean with <import resource="otherXml. xml"> and you will be able to use the bean definition.

What happens if you define both the id and name attributes in a bean's XML definition?

you will experience same effect when id or name is used on a <bean> tag . How? Both id and name attributes are giving us a means to provide identifier value to a bean (For this moment, think id means id but not identifier). In both the cases, you will see same result if you call applicationContext.


1 Answers

No, the Spring context will select one bean definition over the other. Which one it chooses depends on what order the files are fed into the context during initialization.

Logging should indicate that one bean definition is overriding another.

like image 52
skaffman Avatar answered Nov 10 '22 07:11

skaffman