-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 optional field/method injection #10830
Conversation
Is there something that tests setting the null? |
let me add more tests |
Maybe we can just avoid setting null without a new annotation |
that would be a breaking change in behaviour |
I don’t like the idea of introducing a new inject annotation, maybe we can keep this hidden property for remapping Guava and change the behavior in v5? |
I am fine to put this behind a meta annotation or something but how would you suggest the behaviour change? Currently |
I would keep current behavior and introduce an internal parameter of the inject annotation to skip null fields setting, that parameter will be used only for Guava inject remapper and default to in v5 |
but we can't default to |
For Guava? What is the problem remapping it to nullable and inject? |
Maybe I don’t get something |
The two cases are not semantically the same. I don't see how this case can be added without a new annotation. Let's look at with some examples. Case A (Using
|
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 have split the documentation in sections
Possible Solutions
- Change the behaviour to not inject
null
which is what you suggest, but that will still fail Case C becausefoo
is marked as@NonNull
. It is also a breaking change in behaviour which will have upgrade implications and break many applications that today expectnull
to be injected
We should not do this.
I am not sure about this change. Adding a new annotation @Autowired may add more confusion than value. However, the documentation improvements added in this PR are great, and I have moved them to a separate PR so that we can merge them independently of this discussion. I think we already have two ways to achieve this: Using
|
…cronaut-core into optional-injection
ok I will remove the annotation and make a meta-thing to support the Guice/Spring equivalent and there will be no way to access the behaviour from Micronaut itself. |
Another way is to use the |
Whilst I agree there are ways all of them force you to define an implementation somewhere. There is no way to do it just in code and all of the options you presented add more overhead and complication to the implementation. It also makes the code harder to test because instantiating the type also requires then setting the field to something and if the field is not public that is complicated. However, field injection is not great practise anyway so I am not going to die on this hill trying to argue for this change as I don't care enough about it. |
@sdelamo @dstepanov I have removed the new annotation and left the ability to define it via meta-annotation so the Micronaut Spring and upcoming Micronaut Guice projects can behave correctly if they choose to. |
@@ -0,0 +1,11 @@ | |||
// THIS SECTION IS DISABLED. See https://github.com/micronaut-projects/micronaut-core/pull/10830 |
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.
should not we delete this file if we are not showing it in the documentation?
Co-authored-by: Sergio del Amo <sergio.delamo@softamo.com>
Quality Gate passedIssues Measures |
Currently we don't support optional injection of fields and methods. These are always injected and the only way to prevent a
NoSuchBeanException
is to use@Nullable
but that results in injectingnull
.To resolve this, this PR adds a
@Autowired
annotation which differs in behaviour from@Inject
in that it has arequired
member that can be set tofalse
. This mirrors the behaviour in Spring.Looking elsewhere Guice provides their own
@Inject
annotation which supports anoptional
member https://google.github.io/guice/api-docs/4.2/javadoc/com/google/inject/Inject.htmlI considered defining our own
@Inject
annotation and/or creating a new annotation but frankly having@Autowired
made the most sense as it will also help users who are coming from Spring.This PR also documents the injection types better and explains the possible ways to inject a bean.