CDI uses javax.enterprise.inject.Instance to inject optional beans. I don't particular like this solution, but it works. The downside is that it depends on the CDI API.
Best regards, Emond On Wed, Jun 2, 2021 at 10:41 AM Martin Grigorov <mgrigo...@apache.org> wrote: > > On Wed, Jun 2, 2021 at 11:31 AM Sven Meier <s...@meiers.net> wrote: > > > Hi, > > > > I wasn't aware that Optional actually isn't serializable: > > > > The real problem is that it is `final class`. > Even if it was Serializable we would still wrap it in a proxy that looks up > the real bean on demand / after deserialization. > We could use > https://github.com/nipafx/demo-serialize-optional/blob/master/src/org/codefx/lab/optional/SerializableOptional.java > but it is yet another extra code to maintain. > > > > > > > > https://stackoverflow.com/questions/24547673/why-java-util-optional-is-not-serializable-how-to-serialize-the-object-with-suc#24564612 > > > > "The JSR-335 EG felt fairly strongly that Optional should not be on any > > more than needed to support the optional-return idiom only. (Someone > > suggested maybe even renaming it to OptionalReturn to beat users over > > the head with this design orientation; perhaps we should have taken that > > suggestion.)" > > > > (I wonder whether we follow this usage guideline everywhere.) > > > > We don't. > We pass Optional<AjaxRequestTarget> to AjaxFallback**#onClick() methods. > It works fine so far! > > > > > > So it seems an optional @Inject is not possible with our usecase. > > > > I'm +0 on changing anything here, although @Autowired would be fine too. > > > > Yes, I also wonder whether we should do anything here. There are no > problems with SpringBean at the moment. > > > > > > Regards > > Sven > > > > > > On 02.06.21 10:09, Martin Grigorov wrote: > > > Hi, > > > > > > In my email about ideas what to do for Wicket 10 ( > > > https://markmail.org/message/vpptsmt4ysxbktzc) I have suggested to > > > drop @SpringBean and use jakarta.inject.Inject/Named instead. > > > While working on it I've faced one issue - there is no support for > > > non-required bean: > > > > > > @SpringBean(required = false) > > > private SomeBean someBean; > > > > > > If there is no matching bean in the Spring application context then > > nothing > > > will be injected. Without 'required = false' the instantiation of the > > > Component will fail with an Exception. > > > > > > With jakarta.inject APIs there is no way to say that something is > > optional. > > > I've tried by using java.util.Optional: > > > > > > @Inject > > > private Optional<SomeBean> someBean; > > > > > > but the problem here is that j.u.Optional is a final class and cannot be > > > enhanced by CGLib to create a serializable proxy around it. > > > > > > Another option is to create a custom annotation, e.g. @Optional in > > > wicket-ioc. > > > > > > Do you have other ideas ? > > > Or should we stay with @SpringBean or even use Spring's @Autowired ?! > > > > > > Martin > > > > >