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

Filter setter method not injected under Spring-boot environment #12078

Closed
NikoTung opened this issue Apr 12, 2023 · 3 comments
Closed

Filter setter method not injected under Spring-boot environment #12078

NikoTung opened this issue Apr 12, 2023 · 3 comments
Labels
type/discussion Everything related with code discussion or question

Comments

@NikoTung
Copy link

I am not sure this is a bug or by-design because I found issue(#9150 , #9736) and pull request #9187 around, so I just post it here.

In Dubbo3.1.3, when using a setter method to auto-inject Spring bean into a custom Filter,the setter method is never invoked.

The Spring bean is actually a Properties,defined by @ConfigurationProperties(prefix = "codes"), which is registered as a bean with name <prefix>-<fqcn>. When Dubbo loads the filters, it will delegate the setter property to SpringExtensionInjector with the property(the bean name) from this code:

 private String getSetterProperty(Method method) {
        return method.getName().length() > 3 ? method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4) : "";
    }

In SpringExtensionInjector, it only lookups a bean by type when the property is empty, under this circumstance, it will only lookup for a bean by name because the name of setter method is always like setXXX() instead of set(). Because the bean name registered into Spring is not the same as it gets from getSetterProperty method so the bean lookup will return null,which cause the setter method in filter never invoke.

private <T> T getOptionalBean(final ListableBeanFactory beanFactory, final String name, final Class<T> type) {
        if (StringUtils.isEmpty(name)) {
            return getOptionalBeanByType(beanFactory, type);
        } 
        if (beanFactory.containsBean(name)) {
            return beanFactory.getBean(name, type);
        }
        return null;
    }

So I wonder:

@NikoTung NikoTung added the type/discussion Everything related with code discussion or question label Apr 12, 2023
@AlbumenJ
Copy link
Member

This should be the expectation of the previous design. For this case, can you help to see how to fix it.

@NikoTung
Copy link
Author

After I searched, the Spring-boot doesn't support specifying a bean name for @EnableConfigurationProperties spring-projects/spring-boot#19390.
Then I think the previous fix #9187 is option to fix it, by adding an annotation to the setter method and let the developer to decide how to inject. Or, by looking up the bean by type once failed to find bean by name in SpringExtensionInjector. Since this issue only happen under Spring-boot with @ConfigurationProperties, I prefer the latter.

@NikoTung
Copy link
Author

Base on #12122 , this shouldn't be an issue, but I still hope there would be documents about it.

Or it's not a good idea to inject Spring bean in Dubbo3 via a setter method?

It would be better to create a Factory class for yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/discussion Everything related with code discussion or question
Projects
None yet
Development

No branches or pull requests

2 participants