Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent overriding (and enforcement of non-overriding) between bean definition names and aliases #27866

Closed
levitin opened this issue Dec 29, 2021 · 3 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Milestone

Comments

@levitin
Copy link

levitin commented Dec 29, 2021

When I try to register two beans (one of aliases of the first bean corresponds to method name (= future id) of the second bean, the creation of second bean is ignored. The context starts without any exception.

@Configuration
public class MyConfiguration {

    @Bean({"name1", "name2"})
    public MyInterface myBean() {
        return new MyFirstBean();
    }

    @Bean
    public MyInterface name2() {
        return new MySecondBean();
    }
}

MyMap in the following example contains only one bean, actually: name1 -> MyFirstBean

    @Autowired
    Map<String, MyInterface> myMap;

I'm not sure if it is a bug or a feature. In my opinion NoUniqueBeanDefinitionException should be thrown in this case.

What do you think?

@snicoll
Copy link
Member

snicoll commented Dec 29, 2021

It depends. Given that myBean claims name1 and name2, it sounds logical that MySecondBean isn't created. We need to put a priority somewhere. If we did the reverse, you could equally claim that MyFirstBean isn't used despite it declaring an alias for name2.

Having said that, I was expecting this to fail with Spring Boot as we prevent bean overriding by default. It did not and that felt a little bit more surprising. Flagging to get more feedback from the team.

@snicoll snicoll added this to the Triage Queue milestone Dec 29, 2021
@snicoll snicoll added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 29, 2021
@levitin
Copy link
Author

levitin commented Dec 29, 2021

If we did the reverse, you could equally claim that MyFirstBean isn't used despite it declaring an alias for name2

Yes, another direction would also cause an unpleasant surprise. But it is even worse, if giving any bean a "harmless" alias would suddenly exclude another existing bean in case of name matching. I think, such side effect could be really dangerous, perhaps it can be even a security issue.

In my opinion, in such case as described above, it would be much more safer, if the context would not even start at all.

Is there any reason not to treat every bean name and every alias as unique?

@jhoeller jhoeller added the in: core Issues in core modules (aop, beans, core, context, expression) label Jan 4, 2022
@jhoeller jhoeller self-assigned this Jan 4, 2022
@jhoeller jhoeller changed the title Bean creation is ignored if matching one of existing aliases Inconsistent overriding (and enforcement of non-overriding) between bean definition names and aliases Jan 18, 2022
@jhoeller jhoeller added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 18, 2022
@jhoeller jhoeller modified the milestones: Triage Queue, 6.0.0-M3 Jan 18, 2022
@jhoeller
Copy link
Contributor

There are indeed two inconsistencies in our overriding checks between bean definition names and aliases: Not only is a new bean definition overriding an existing alias not prevented when allowBeanDefinitionOverriding=false (like in Boot by default), the factory also silently registers a new bean definition but does not remove a same-named alias when allowBeanDefinitionOverriding=true.

In other words, bean definition overriding between bean definition names and aliases is a feature, but a later definition should consistently override an existing definition - so in our scenario here, the name2 method should override the existing name2 alias. And with overriding deactivated, it should consistently throw an exception when encountering such an attempted override for an existing alias.

I'll revise this for 6.0 M3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants