[cdi-dev] [JBoss JIRA] (CDI-45) Optional Injection Points

Antonin Stefanutti (JIRA) issues at jboss.org
Mon Sep 1 11:35:00 EDT 2014


    [ https://issues.jboss.org/browse/CDI-45?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12997509#comment-12997509 ] 

Antonin Stefanutti commented on CDI-45:
---------------------------------------

>From my understanding, that need is met with [{{javax.enterprise.inject.Instance}}|http://docs.oracle.com/javaee/7/api/javax/enterprise/inject/Instance.html]:
{code}
@Inject
Instance<MyBean> bean;

if (!bean.isUnsatisfied())
    bean.get();
{code}

What would be the added value of the {{@Optional}} approach compared to the {{Instance}} concept. Besides, that approach would lead to having potential NPE or to check against nullity which is not very elegant.

That being said, with the CDI 2.0 / Java 8 enhancement stream, [{{java.util.Optional}}|http://docs.oracle.com/javase/8/docs/api/java/util/Optional.html] could be automatically supported:
{code}
@Inject
Optional<MyBean> bean;

bean.isPresent();
bean.orElse(...);
{code}

> Optional Injection Points
> -------------------------
>
>                 Key: CDI-45
>                 URL: https://issues.jboss.org/browse/CDI-45
>             Project: CDI Specification Issues
>          Issue Type: Feature Request
>          Components: Resolution
>    Affects Versions: 1.0
>            Reporter: Stuart Douglas
>             Fix For: 2.0 (discussion)
>
>
> There are occoasions where it may be useful for some injection points to be optional, e.g.
> @Inject
> @Optional 
> MyBean bean; 
> This would behave the same as a normal injection point, however it will not cause the deployment to fail if it is not satisified. 



--
This message was sent by Atlassian JIRA
(v6.3.1#6329)


More information about the cdi-dev mailing list