TheMP
TheMP

Reputation: 8427

Forcing creation of new instance of object while injecting it

I'm writing an application in which I have defined a custom ComboBox (Let's call it MyComboBox). I want to use it separately in two different panel (Let's say APanel and BPanel ). The injection hierarchy goes as follows:

BPanel has MyComboBox injected
MyWindow has BPanel injected
APanel has MyWindow AND MyComboBox injected

The problem is I have different listeners/actions added to MyComboBox in BPanel and different added to MyComboBox in APanel, but because it is within one "injection spree" only ONE instance of MyComboBox is created and all the listeners are shared.

I have already tried changin scope from @Dependant (default one) to @RequestScoped, but it did not help.

I have found by myself two ways to solve this issue:

  1. Create the combobox manually, using standard Java constructors.
  2. Make MyComboBox abstract and create two classes extending it, say:

    MyAPanelComboBox extends MyCombobox { // nothing here needed }

    MyBPanelComboBox extends MyCombobox { // nothing here needed }

Neither of the above satisfy me though and I wonder if there exists an easy and clean way to deal with my problem.

Upvotes: 2

Views: 616

Answers (1)

palacsint
palacsint

Reputation: 28865

I can't try it now but if I'm right annotating the MyComboBox class with @Dependant (or without any annotation) should work.

Anyway, a workaround could be injecting

@Inject
private Instance<MyCombobox> comboxboxInstance;

and getting new instances with

comboxboxInstance.get();

Upvotes: 2

Related Questions