Когда имена компонентов перекрываются, выходные данные журнала различаются в зависимости от типа возвращаемого значения JAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Гость
 Когда имена компонентов перекрываются, выходные данные журнала различаются в зависимости от типа возвращаемого значения

Сообщение Гость »


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 {
}
This is the code for the implementation class. Adding the @Component annotation automatically registers it as a bean.

Код: Выделить всё

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);
}
CASE 1: return type of factory method = interface

Код: Выделить всё

@Configuration
@ComponentScan(basePackageClasses = BeanOverridingTest.class)
static class TestConfiguration{
@Bean
public BeanInterface beanA() {  // register bean (name: "beanA")
return new BeanA();
}
}
In this case, the following overriding-related log is printed.
"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();
}
}
In this case, the overriding-related log is not printed.
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
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»