-
Notifications
You must be signed in to change notification settings - Fork 826
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
Add support for client stub constructor injection #749
Conversation
Signed-off-by: xJoeWoo <xjoewoo@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, this feature is on my wish-list since a long time.
I have to think about some subtile implications such as having two beans with the same type but different names. I'm not sure whether spring resolves them correctly.
...utoconfigure/src/main/java/net/devh/boot/grpc/client/inject/GrpcClientBeanPostProcessor.java
Outdated
Show resolved
Hide resolved
...configure/src/main/java/net/devh/boot/grpc/client/inject/GrpcClientConstructorInjection.java
Outdated
Show resolved
Hide resolved
tests/src/test/java/net/devh/boot/grpc/test/inject/GrpcClientBeanInjectionNegativeTest.java
Outdated
Show resolved
Hide resolved
tests/src/test/kotlin/net/devh/boot/grpc/test/kotlin/inject/KotlinGrpcClientInjectionTest.kt
Outdated
Show resolved
Hide resolved
...configure/src/main/java/net/devh/boot/grpc/client/inject/GrpcClientConstructorInjection.java
Outdated
Show resolved
Hide resolved
...va/net/devh/boot/grpc/client/inject/GrpcClientConstructorInjectBeanFactoryPostProcessor.java
Outdated
Show resolved
Hide resolved
Signed-off-by: xJoeWoo <xjoewoo@gmail.com>
I didn't evaluate the full impact to interceptor or other stub processing steps. Maybe some researches need to be made by maintainers. |
Please run |
Signed-off-by: xJoeWoo <xjoewoo@gmail.com>
Did you commit all changes from spotlessApply? |
Signed-off-by: xJoeWoo <xjoewoo@gmail.com>
Yep |
The tests are failing. |
Signed-off-by: xJoeWoo <xjoewoo@gmail.com>
I missed that. Also, I ran |
Signed-off-by: xJoeWoo <xjoewoo@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will have an in depth look at this and some extended testing this weekend.
There are a couple of things that I'm not sure about yet:
- Should we merge the PostProcessors? IMO they are very similar as they work on the same annotation and even exchange data. I'm not sure which implications that might have an the
initGrpClientConstructorInjects()
method though. - I'm not sure whether/how spring determines that the GrpcClient Bean that is created for a constructor parameter is actually intended to be used for that parameter (if there are multiple parameters with the same class).
- whether there is a way to do this without adding a bean name to it
- whether there is a way to support bean factory methods/do this in a more generic way
This a very "technical" PR that challenges my knowledge about Spring's internals and thus it might take a few days to review.
Once again, thanks for this PR.
It's my pleasure to help this project better 😄. I'm not quite an expert on Spring, maybe my ideas are not the perfect solution.
|
I had a look at this and I noticed two shortcomings that needs to be addressed.
I also considered explicitly setting the constructor argument values on the bean definition (there is a setter for the value), maybe this works and it isn't required to create a named bean anymore. I didn't have time to test it, but I would like to evaluate it before we merge this solution. |
…ues instead of creating bean Signed-off-by: xJoeWoo <xjoewoo@gmail.com>
I realized |
Awesome, this looks very promising. I will review this in the next few days. |
...net/devh/boot/grpc/client/inject/GrpcClientConstructorInjectionBeanFactoryPostProcessor.java
Outdated
Show resolved
Hide resolved
...net/devh/boot/grpc/client/inject/GrpcClientConstructorInjectionBeanFactoryPostProcessor.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This starter is lack of support for Constructor Injection of client stubs. CI is quite a common usage in Spring projects(especially projects with Kotlin) to make service class neat and easy to test.
In old ways, multiple
@GrpcClientBean
s must be used on configuration class to register client stubs as Spring Bean. So that client stubs can be injected as a bean.This PR implements a new way for simpler CI. Example
The basic implement mechanism are:
beanName
field was added to@GrpcClient
, and make this annotation able to mark on constructor parameters;@GrpcClient
, byGrpcClientConstructorInjectBeanFactoryPostProcessor
. Collect grpc client config together with parameter type.GrpcClientConstructorInjection
then register it to bean factory;GrpcClientBeanPostProcessor
, byGrpcClientBeanPostProcessor#initGrpClientConstructorInjects()
.Maybe BREAKING CHANGES:
@GrpcClientBean
, even they have same class. This PR changes it to allow registering same name client stub bean if they have same class. Source hereAlso, Kotlin was introduced for Constructor Injection test.