sialcasa / mvvmFX

an Application Framework for implementing the MVVM Pattern with JavaFX
Apache License 2.0
484 stars 105 forks source link

Injections not working properly #627

Closed gilad7 closed 1 year ago

gilad7 commented 1 year ago

I am trying to use the @InjectedScope on my ViewModel but for some reason it just remains null.

I tried to configure it as shown here https://github.com/sialcasa/mvvmFX/tree/develop/examples/contacts-example/src/main/java/de/saxsys/mvvmfx/examples/contacts/ui but for some reason I don't get anything from the injection.

I have a simple scope class that implements Scope, and in my ViewModel class I just try to inject it to see that it is not null. I there anything else needs to be configured?

manuel-mauky commented 1 year ago

Can you show your code?

At what point in time do you check for the scope to be null? For example, you cannot access injected things in the constructor of the viewModel but instead you have to wait until all initialization is done by the framework. The best way for doing this is to create a method an annotate it with @Initialize, i.e.

public class SomeViewModel implements ViewModel {    
    @InjectScope
    private SomeScope someScope;

    @Initialize
    private void init() {
        someScope.subscribe(...);
    }
}



gilad7 commented 1 year ago

I haven't seen the @Initalize annotation, this might help. I am using spring now for injections so I guess I will keep it that way for now. Thanks :)