Hi everyone, my name is Mic

A fan who has worked for 3 years sent me a voice message on WeChat at 6:00 a.m., which immediately woke me up.

I thought it was such a big deal, only to find out when I asked.

The interviewer asked him a question that he didn't answer. The question was "In Spring, if two beans with the same id will report an error? If so, at what stage will the error be reported?"

Check out the answers from ordinary people and experts below!

Ordinary people:

Two beans with the same id should report an error because in the Spring IOC container, its id must be unique. If there are two identical ids, there will be no find, that is to say, when we look for a bean, he will look for it by name, or by type.

At what stage. . . It should be when spring starts.

Expert:

Okay, I'll answer this question from a few points.

First of all, in the same XML configuration file, there cannot be two beans with the same id, otherwise an error will be reported when the spring container starts.

carbon_20220512164817

Because the id attribute represents the unique symbol of a bean, Spring will verify the uniqueness of the id at startup, and an error will be reported once a duplicate is found.

This error occurs when Spring parses the XML file into a BeanDefinition stage.

But in two different Spring configuration files, two beans with the same id can exist. When the IOC container loads beans, it will overwrite multiple beans with the same id by default.

After Spring 3.x version, this problem has changed

We know that Spring 3.x provides @Configuration annotation to declare a configuration class, and then uses @Bean annotation to implement Bean declaration, which completely replaces XMl.

In this case, if we declare multiple beans with the same name in the same configuration class, only the instance of the first declared bean will be registered in the Spring IOC container.

Subsequent beans with duplicate names will not be registered again.

For a piece of code like this, in the Spring IOC container, only the instance of UserService01 will be saved, and subsequent instances of the same name will not be loaded.

carbon_20220512164818

If the @Autowired annotation is used to implement dependency injection according to the type, because the IOC container only has an instance of UserService01, it will prompt that the instance of UserService02 cannot be found when it is started.

carbon_20220512164820

If you use the @Resource annotation to implement dependency injection based on nouns, the instance object obtained in the IOC container is UserService01,

So Spring assigns the instance of UserService01 to UserService02, and it will prompt a type mismatch error.

carbon_20220512164819

This error occurs in the dependency injection phase after the bean initialization in the Spring IOC container.

The above is my understanding of the problem.

Summarize

You see, a small interview question involves so many knowledge points.

Some fans will ask, I have already used this, what is the point of asking this question?

In fact, many friends who have just worked for 1-2 years are prone to various abnormalities if they are used improperly.

And have a deep enough understanding of Spring, you can quickly solve various exceptions.

Friends who like my works remember to like, favorite and follow.

file

Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source for Mic带你学架构 !
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!

跟着Mic学架构
810 声望1.1k 粉丝

《Spring Cloud Alibaba 微服务原理与实战》、《Java并发编程深度理解及实战》作者。 咕泡教育联合创始人,12年开发架构经验,对分布式微服务、高并发领域有非常丰富的实战经验。