[cdi-dev] [JBoss JIRA] (CDI-414) Support for "self" injection

Antonin Stefanutti (JIRA) issues at jboss.org
Tue Aug 12 07:32:30 EDT 2014

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

Antonin Stefanutti commented on CDI-414:

In the [Metrics CDI extension|https://github.com/astefanutti/metrics-cdi], CDI interceptors are used to monitor bean method invocations. In the following use case, I would expect the self-invocation of the annotated method to be intercepted as well:

import com.codahale.metrics.annotation.Timed;

public class TimedMethodBean {

    @Timed(name = "timedMethod")
    public void timedMethod() {

    public void selfInvocationTimedMethod() {

I gave it a try as I knew Weld is using subclassing as proxying technique though it happens that interception of self-invocation bean method isn't working \[1\]. Besides, neither the CDI nor the Java Interceptors specification make that point explicit. The only reference to that question that I've been able to stumble upon is \[2\]:
_Method interception_ by interceptors and decorators applies to _business method invocations_ of a simple Web Bean, enterprise Web Bean or EJB bean.
Self-invocations of a simple Web Bean are considered to be business method invocations. However, self-invocations of an enterprise Web Bean or EJB session, singleton or message driven bean are not considered to be business method invocations.

>From my understanding, valid use cases exist, implementations are capable of supporting it (OWB uses bytecode manipulation \[3\]), yet it has been "disabled" for consistency with EJB which contradicts \[2\]. That'd be valuable to have that cleared explicitly in the upcoming CDI 2.0 specification as Antoine suggested.

\[1\]: https://github.com/astefanutti/metrics-cdi/blob/4625ce1e75b2651935287d1c27964e6af36608a5/envs/se/src/test/java/org/stefanutti/metrics/cdi/se/TimedMethodBeanTest.java#L88
\[2\]: http://docs.jboss.org/webbeans/spec/PDR/html/interceptors.html
\[3\]: https://blogs.apache.org/owb/entry/news_from_openwebbeans_1_2

> Support for "self" injection
> ----------------------------
>                 Key: CDI-414
>                 URL: https://issues.jboss.org/browse/CDI-414
>             Project: CDI Specification Issues
>          Issue Type: Bug
>          Components: Resolution
>            Reporter: arjan tijms
> Many features of CDI and EJB work by means of a proxy that intercepts calls and adds 'aspects'. In Java it's however not possible to decorate the {{this}} pointer, so methods called on the same bean instance from within a method in the bean do not get their 'aspects' applied.
> This is a well known limitation, but in EJB it's possible to work around this by injecting a bean into itself. E.g.
> {code}
> @Stateless
> public class Foo {
>     @EJB
>     private Foo self;
>     // ...
> }
> {code}
> Also see http://adam-bien.com/roller/abien/entry/how_to_self_invoke_ejb
> Unfortunately using CDI and {{@Inject}} this doesn't work. Weld for instance fails the deployment and logs:
> {noformat}
> WELD-001443 Pseudo scoped bean has circular dependencies.
> {noformat}
> See also: http://adam-bien.com/roller/abien/entry/inject_vs_ejb
> Although there are workarounds, it would be great if {{@Inject}} in combination with CDI could support self injection as well.
> With that projects migrating from {{@EJB}} to {{@Inject}} can do so more easily and the capability can be convenient for new projects as well (e.g. calling two separate {{@Transactional}} methods from a single method without being required to create a new bean).

This message was sent by Atlassian JIRA

More information about the cdi-dev mailing list