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

Introduce supports() method in BeanPostProcessor #28645

Closed
huifer opened this issue Jun 17, 2022 · 3 comments
Closed

Introduce supports() method in BeanPostProcessor #28645

huifer opened this issue Jun 17, 2022 · 3 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply

Comments

@huifer
Copy link

huifer commented Jun 17, 2022

When writing an implementation class for the BeanPostProcessor interface, you usually have the following code:

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    if (bean instanceof People) {
        // do something
    }
    return bean;
}

The BeanPostProcessor interface implementation class needs to enter this code each time it passes through each if block, and some of these entries don't do anything to the bean object, but return it directly.

Maybe the BeanPostProcessor interface can have a supports() method to judge whether to support processing in the postProcessBeforeInitialization method.

The improved code is as follows.

public interface BeanPostProcessor  {
    @Nullable
    default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Nullable
    default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    default boolean supports(Object bean, String beanName) {
        return true;
    }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 17, 2022
@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement labels Jun 17, 2022
@sbrannen sbrannen changed the title BeanPostProcessor enhance Introduce supports() method in BeanPostProcessor Jun 17, 2022
@sbrannen
Copy link
Member

@huifer
Copy link
Author

huifer commented Jun 17, 2022

Some of my thoughts

#21362 (comment)

This was referenced Jun 18, 2022
@snicoll
Copy link
Member

snicoll commented Oct 2, 2023

Thanks for the suggestion but BeanPostProcessor is a low-level interface and we'd rather not make it more complex at this time. Some improvements are scheduled in a separate issue, see #21362 (comment).

@snicoll snicoll closed this as not planned Won't fix, can't repro, duplicate, stale Oct 2, 2023
@snicoll snicoll added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on type: enhancement A general enhancement labels Oct 2, 2023
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) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants