I learned that if the names of beans registered through component scanning overlap with those registered through configuration methods, overriding occurs.
However, when I mistakenly specified the return type of a method in the configuration as the implementation class instead of the interface during auto-completion, the log related to overriding was not outputted. I'm curious about the reason for this.
This is the code for the interface.
Код: Выделить всё
package hello.core.bean_overriding;
public interface BeanInterface {
}
Код: Выделить всё
package hello.core.bean_overriding;
import org.springframework.stereotype.Component;
@Component // register bean (name: "beanA")
public class BeanA implements BeanInterface {
public BeanA() {
System.out.println("BeanA.BeanA = " + this);
}
Код: Выделить всё
@Configuration
@ComponentScan(basePackageClasses = BeanOverridingTest.class)
static class TestConfiguration{
@Bean
public BeanInterface beanA() { // register bean (name: "beanA")
return new BeanA();
}
}
"17:30:59.922 [Test worker] DEBUG o.s.b.f.s.DefaultListableBeanFactory --Overriding bean definition for bean 'beanA' with a different definition: ..."
CASE 2: return type of factory method = implementation class
Код: Выделить всё
@Configuration
@ComponentScan(basePackageClasses = BeanOverridingTest.class)
static class TestConfiguration{
@Bean
public BeanA beanA() { // register bean (name: "beanA")
return new BeanA();
}
}
Thank you for reading through my question, and I'd be even more grateful if you could give me some advice. Wishing you a great day.
Источник: https://stackoverflow.com/questions/781 ... -type-of-t