Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Bean with @RootContext Activity in a singleton #674

Description

@tbruyelle

When a bean with a field @RootContext Activity activity is injected in another bean with the singleton scope, the field is null, because by design singleton doesn't store ref to activities, in order to avoid memory leaks.

Having an activity field null leads to hard to find bugs, especially for AA beginners.

I think AA should prevent such case, by throwing an exception like IllegalStateException. If the user want to inject the activity in @RootContext, there must be a good reason for that, and so we must warn him he can't do that if the bean is injected in a signleton bean.

WDYT?

Activity

  1. DayS commented on Jul 25, 2013

    @DayS
    Contributor

    Ho. That one was tricky :)

    Just to clarify this point, let's assume we have the following classes :

    @EActivity
    public class MyActivity extends Activity {
        @Bean
        MySingletonBean mySingletonBean;
    }
    
    @EBean(scope = Scope.Singleton)
    public class MySingletonBean {
        @Bean
        MyBean myBean;
    }
    
    @EBean
    public class MyBean {
        @RootContext
        Activity activity;
    }

    As the context stored in a singleton bean is always the application context, and the @RootContext checks if the instance of context matches the field type before injecting it, we have the following situation :

    1. MyActivity is a context of type Activity
    2. MySingletonBean takes the Activity and stored the ApplicationContext from it
    3. MyBean takes the ApplicationContext and doesn't inject the context because we want an Activity

    Dunno what we should do for this. Throwing an exception will be a pain in the ass if the developer needs to do this. Moreover, it'll break the API, as we just not setting the field right now.

  2. tbruyelle commented on Jul 25, 2013

    @tbruyelle
    ContributorAuthor

    @DayS Yes it is exactly the situation you described. I'm not sure if it's really tricky, that just happened to me multiple times (or maybe a I have strange way to use AA!)

    What I really dislike in this issue is the fact that an annotated field is not injected silently. But you're right it'll break the API to throw an exception. But I don't have a better solution for now...

  3. yDelouis commented on Jul 25, 2013

    @yDelouis
    Contributor

    We could just add a log explaining why the Activity has not been injected.

  4. DayS commented on Jul 25, 2013

    @DayS
    Contributor

    The log might be the best solution.

  5. DayS commented on Sep 23, 2013

    @DayS
    Contributor

    This was merged. I'm closing this issue then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions