Skip to content

Implicitly introduced beans and explicitly introduced beans coexist, and the implicitly introduced beans are deleted. #32562

@osenx

Description

@osenx

Hello! As the title suggests, i have found a confusing question and look forward to an official answer

testPopulateBean.xml

<context:component-scan base-package="com.my.populateBean"></context:component-scan>
<bean id="b1" class="com.my.populateBean.Book"></bean>
@Component("book")
public class Book {
    public Book() {}
}

Test Case

ApplicationContext ac = new ClassPathXmlApplicationContext("testPopulateBean.xml");
System.out.println(ac.containsBean("book"));  // true

but if the context:component-scan label is located below. Such as

<bean id="b1" class="com.my.populateBean.Book"></bean>
<context:component-scan base-package="com.my.populateBean"></context:component-scan>

Test Case

ApplicationContext ac = new ClassPathXmlApplicationContext("testPopulateBean.xml");
System.out.println(ac.containsBean("book"));  // false

I posted that it was deleted in this place ConfigurationClassParser#processConfigurationClass

 if (configClass.isScanned()) {
      String beanName = configClass.getBeanName();
      if (StringUtils.hasLength(beanName) && this.registry.containsBeanDefinition(beanName)) {
	      this.registry.removeBeanDefinition(beanName);
      }
      // An implicitly scanned bean definition should not override an explicit import.
      return;
 }

In my understanding, it is to prevent duplicate imports of beans imported by @ Important, but to cause normal @ Component beans to be deleted. Do you know if my understanding is correct? Is this phenomenon expected?

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: stackoverflowA question that's better suited to stackoverflow.com

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions