Skip to content

@Priority(LOWEST_PRECEDENCE) still higher than no priority #31544

Closed as not planned
@FyiurAmron

Description

@FyiurAmron

Affects: all Spring Framework versions (spring-beans module)


@Priority(LOWEST_PRECEDENCE) is still higher than no priority - which makes it impossible to lower the priority of selected @Component via this annotation.

(see #26241 for a similar issue, although this one proposes a simpler solution for a particular subcase)

Let's assume this:

@Component
@Priority(HIGHEST_PRECEDENCE)
class ComponentHighestPrecedence extends SomeComponent {}

@Component
@Priority(LOWEST_PRECEDENCE)
class ComponentLowestPrecedence extends SomeComponent {}

@Component
class ComponentNoExplicitPrecedence extends SomeComponent {}

In this case, ComponentHighestPrecedence will be always selected, as expected. If we remove it, however, ComponentLowestPrecedence will be always selected, due to how https://github.com/spring-projects/spring-framework/blob/main/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java#L1787 is currently implemented.

Although that's probably by design, there is a much bigger issue here: there is no way to say that a component should have priority lower than or equal to "no explicit priority". LOWEST_PRECEDENCE is thus a misnomer in this case, it's "lower than any explicit but higher than all implicit".

The use case when this is problematic is very simple: let's say we create a library that provides a bean that needs to be used only if the library consumer doesn't provide his own implementation. The workaround is to force the consumer to use @Primary, but that forces maintenance on the consumer. OTOH, if we would have a default reasonable priority value for components with no @Priority, we could just declare @Priority(DEFAULT_PRECEDENCE - 1) on the library component, and be done with it.

Changing this is obviously a breaking change, but maybe there is some reasonable way to provide this as a configuration option somewhere, that would make e.g. getPriority return 0 (or even better, some DEFAULT_PRECEDENCE value) instead of null for the beans that have no priority given explicitly?

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions